Commonly used Git commands
We’ll take a look at some of the most commonly used Git commands and provide examples of how they are used.
Git Commands
- git init
This command initializes a new Git repository in the current directory. For example:
1
$ git init
- git add
This command adds changes to the staging area, preparing them for commit. For example:
1
$ git add file.txt
- git commit
This command creates a new commit with changes from the staging area. For example:
1
$ git commit -m "Added new feature"
- git status
This command shows the current status of the repository, including changes that have been made and the current branch. For example:
1
$ git status
- git diff
This command shows the differences between the current state of the repository and the last committed state. For example:
1
$ git diff
- git branch
This command shows a list of all branches in the repository, as well as the current branch. For example:
1
$ git branch
- git checkout
This command switches to a different branch in the repository. For example:
1
$ git checkout feature-branch
- git merge
This command merges changes from one branch into another branch. For example:
1
$ git merge feature-branch
- git pull
This command fetches changes from a remote repository and merges them into the current branch. For example:
1
$ git pull origin master
- git push
This command pushes changes to a remote repository. For example:
1
$ git push origin feature-branch
- git clone
This command creates a copy of a remote repository on your local machine. For example:
1
$ git clone https://github.com/user/repo.git
- git fetch
This command fetches changes from a remote repository without merging them into your local branch. For example:
1
$ git fetch origin
- git log
This command shows a list of all commits in the repository, including commit messages and authors. For example:
1
$ git log
- git reset
This command resets the current branch to a specific commit, removing any changes that were made after that commit. For example:
1
$ git reset HEAD~1
- git revert
This command creates a new commit that undoes the changes from a previous commit. For example:
1
$ git revert HEAD
- git stash
This command saves changes to a “stash” so that they can be applied later. For example:
1
$ git stash save "Work in progress"
- git tag
This command creates a tag for a specific commit, which can be useful for marking important milestones or releases. For example:
1
$ git tag v1.0.0
- git remote
This command shows a list of remote repositories that are connected to the local repository. For example:
1
$ git remote -v
- git submodule
This command manages submodules, which are separate repositories that are included within a main repository. For example:
1
git submodule add https://github.com/user/repo.git
- git cherry-pick
This command applies changes from a specific commit to the current branch. For example:
1
$ git cherry-pick abc123
Conclusion
In conclusion, Git provides a wide range of commands for managing changes to software projects. By understanding these commands and how they are used, developers can work more efficiently with Git and collaborate more effectively with their team members.