Essential Git Commands for Beginners
must know Git Commands for Developers.
What is Git ??
Git, a distributed version control system, is an essential tool for developers to manage their codebase efficiently. Whether you're working on personal projects or contributing to open-source, mastering Git is crucial. In this blog, we'll cover the most useful Git commands for beginners to help you kickstart your version control journey. Let's dive in!
Setting up Git
Before using Git, you need to install it on your system and configure your user details. you can install Git from here.
Next, configure your user details:
# Set your username
git config --global user.name "Your Name"
# Set your email
git config --global user.email "youremail@example.com"
Initializing a Git repository:
To start using Git for version control, you need to initialize a Git repository in your project folder:
# Navigate to your project folder
cd /path/to/your/project
# Initialize Git repository
git init
Cloning a repository:
If you want to work with an existing repository hosted on a remote server (like GitHub or GitLab), you can clone it to your local machine:
git clone <repository_url>
Replace <repository_url>
with the URL of the remote repository you want to clone.
Checking status:
Always know the status of your repository to see what has changed and needs to be committed:
git status
Staging changes:
Before committing changes, you need to stage them:
# Stage all changes
git add .
# Stage specific files
git add file1 file2
Committing Changes
Committing creates a snapshot of your changes in the repository:
git commit -m "Your commit message here"
Write a meaningful commit message that describes the changes you made.
Viewing commit history
To see the history of commits and their messages:
git log
Creating branches
Branches are used to work on new features or bug fixes without affecting the main codebase:
NOTE ๐ : NEVER ๐ซ COMMIT ON MAIN/MASTER BRANCH DIRECTLY!!
# Create a new branch
git branch new-feature
# Switch to the new branch
git checkout new-feature
Shortcut for creating and switching to a new branch in one step:
git checkout -b new-feature
Merging branches
After finishing work on a branch, merge it into the main branch (usually 'master' or 'main'):
# First, switch to the main branch
git checkout main
# Merge the feature branch into the main branch
git merge new-feature
Pulling and pushing changes
To update your local repository with changes from the remote repository:
# Fetch the latest changes
git fetch
# Merge the changes into your current branch
git merge origin/main
# Replace 'main' with the remote branch name
# Push your changes to the remote repository
git push origin main
# Replace 'main' with your branch name if not on the main branch
Conclusion
Congratulations!โจ You've learned some essential Git commands for beginners. Git can be overwhelming at first, but with practice, it becomes a powerful ally in your development journey. Remember to commit regularly, write meaningful commit messages, and use branches effectively.
also Like โฅ and Share ๐ this Blog to your colleagues and friends if you got value from this Blog, and connect with me : https://linktr.ee/hirentimbadiya