Introduction
Git is an essential tool for developers, allowing them to track changes in their code, collaborate with team members, and maintain version control.
While Git offers a wide array of commands, there are some that every developer should have in their toolkit.
Whether you’re new to Git or looking to brush up on the basics, here are the Top 10 Git commands every developer should know.
GIT Config Commands
GIT config commands are those commands which are used to configure GIT in your local machine.
Some of the commonly used GIT config commands are:
git config --global user.name "Your Name"
This above command – Sets the name associated with your Git commits.
Example: `git config –global user.name “Chinmaya Rath”`
git config --global user.email "your_email@example.com"
This above command – sets the email address will be associated with your Git commits and will appear the UI of the systems like GitHub, GitLab and etc.. Example: `git config –global user.email “chinmayarath@example.com”`
GIT Essential Commands
After you have configured GIT in your local machine, you need to understand the essential commands for using it.
1. git init
The git init command is used to initialise a new Git repository in your project.
Running this command creates a hidden .git folder, which Git uses to track your project files.
git init
When to use it?
- When starting a new project from scratch.
- To convert an existing project into a Git repository.
2. git clone
The git clone command copies an existing Git repository to your local machine.
It’s most commonly used to download a project from a remote repository, such as GitHub.
git clone https://github.com/ username/repository.git
When to use it?
- When you want to contribute to or work on an existing project.
- To set up a local version of a project hosted on a remote server.
3. git add
The git add command is used to stage changes before committing them.
It tells Git which files to track for changes in the next commit.
git add # Add a specific file
git add . # Add all files in the current directory
- Before committing changes to the repository.
- To stage new, modified, or deleted files for commit.
4. git commit
The git commit command saves your changes to the local repository.
Each commit records a snapshot of the project’s current state.
You should always include a meaningful message to describe what the commit does.
git commit -m "Describe the changes made"
When to use it?
- After you’ve staged your changes using git add.
- To save a checkpoint in the project history.
5. git status
The git status command shows the current state of the working directory and staging area.
It tells you which files are staged for commit, which files have been modified, and which files are untracked by Git.
git status
When to use it?
- To check the status of your files before committing.
- To see which files need to be added, committed, or removed.
6. git pull
The git pull command fetches changes from a remote repository and merges them into your current branch.
It’s commonly used to update your local repository with the latest changes from the remote repository.
git pull origin main
When to use it?
- When you want to get the latest changes from the remote repository.
- Before starting new work to ensure your local copy is up to date.
7. git push
The git push command uploads your local commits to a remote repository.
This is how you share your work with others after you’ve made and committed changes.
git push origin main
When to use it?
- After committing changes locally, to share your updates with a remote repository.
- When collaborating with a team and needing to sync work.
8. git branch
The git branch command is used to manage branches in your Git repository.
Branches are essential for isolating different tasks, features, or bug fixes. You can list all branches, create new ones, or delete old ones.
git branch # List all branches
git branch # Create a new branch
git branch -d # Delete a branch
When to use it?
- When you want to create a new branch to work on a feature or bug fix.
- To list existing branches and switch between them.
9. git checkout
The git checkout command is used to switch between branches or restore a specific commit.
You can use it to move from one branch to another or revert changes in your project.
git checkout # Switch to a different branch
git checkout # Checkout a specific commit
When to use it?
- When you want to switch to a different branch.
- To check out a previous state of your project using a commit hash.
10. git merge
The git merge command combines changes from one branch into another.
It’s commonly used to merge feature branches into the main branch once the feature is complete.
git merge
When to use it?
- When you’re done working on a feature and want to merge it into the main branch.
- To combine different lines of development in your project.
GIT Cheat Sheet
Conclusion
These top 10 Git commands form the foundation for working effectively with Git. By mastering these commands, you’ll be able to manage your code, collaborate with others, and track the evolution of your projects with confidence.
Whether you’re working solo or as part of a team, these commands will become a regular part of your development workflow.