Hello and welcome to the ultimate guide on Git commands! Git is now a must-have tool for developers worldwide. For both new and experienced developers, understanding Git commands makes version control easy.
So here in this article, we’ll discuss the 100 must know Git commands to ease your coding journey. Let’s get started!
Top 100 Git Commands
Before we jump into the commands, let’s set up the basics. Git is a distributed version control system that tracks changes in your code. It’s a must-have for collaborative projects.
1. git init
Imagine you have a folder full of files and you want to start tracking their changes. This is where git init
comes into play. Run this command in your project directory, and Git will set up a new repository for you.
git init
2. git clone
Found a project on GitHub that you want to work on? Use git clone
to copy the repository to your local machine.
git clone https://github.com/user/repo.git
3. git add
Before committing changes, you need to add them to the staging area. This is done with git add
. You can add specific files or use .
to add everything.
git add file.txt
git add .
4. git commit
Committing is like saving a snapshot of your project. The -m
flag lets you add a message describing what you’ve done.
git commit -m "Initial commit"
5. git status
Not sure what’s going on in your repo? git status
will tell you. It shows which files are staged, unstaged, and untracked.
git status
6. git log
Want to see your project’s history? git log
shows a list of all the commits made.
git log
7. git branch
Branches are like parallel universes for your project. Use git branch
to see all branches or create new ones.
git branch
git branch new-branch
8. git checkout
Switch between branches or revert files with git checkout
.
git checkout new-branch
git checkout file.txt
9. git merge
Merging combines changes from different branches. It’s how you bring your work together.
git merge new-branch
10. git pull
git pull
fetches changes from a remote repository and merges them into your current branch.
git pull origin main
11. git push
Ready to share your changes? git push
sends them to a remote repository.
git push origin main
12. git remote
Manage the remote connections with git remote
. You can add, remove, and view remotes.
git remote -v
git remote add origin https://github.com/user/repo.git
13. git fetch
Unlike git pull
, git fetch
only downloads changes without merging them.
git fetch origin
14. git diff
See differences between commits or your working directory with git diff
.
git diff
15. git reset
Need to undo some changes? git reset
can help. It can be a bit tricky, so use it carefully.
git reset --hard HEAD
16. git rebase
Rebasing is another way to integrate changes. It moves or combines a sequence of commits.
git rebase main
17. git stash
Stash your changes temporarily if you need to switch branches without committing.
git stash
18. git stash apply
Retrieve your stashed changes with git stash apply
.
git stash apply
19. git tag
Tags are great for marking specific points in your history as important.
git tag v1.0
20. git show
Show details about commits, tags, or other Git objects.
git show v1.0
21. git ls-files
List all files in the repository.
git ls-files
22. git rm
Remove files from the repository.
git rm file.txt
23. git mv
Move or rename files.
git mv oldname.txt newname.txt
24. git blame
Find out who made changes to a file.
git blame file.txt
25. git bisect
Use binary search to find the commit that introduced a bug.
git bisect start
git bisect bad
git bisect good
26. git grep
Search for text in tracked files.
git grep "search-term"
27. git reflog
Manage the reference logs, or reflogs, to see changes in the tip of branches.
git reflog
28. git shortlog
Summarize git log
output.
git shortlog
29. git config
Customize Git settings.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
30. git clean
Clean the working directory by removing untracked files.
git clean -f
31. git archive
Create an archive of files from a named tree.
git archive --format=tar HEAD | tar -x -C /path/to/extract
32. git cherry-pick
Apply the changes from a specific commit.
git cherry-pick commit-hash
33. git describe
Give a human-readable name to a commit.
git describe
34. git diff –staged
Show differences between the staging area and the last commit.
git diff --staged
35. git log –oneline
Condense the log output to one line per commit.
git log --oneline
36. git log –graph
Visualize the branch structure with a graph.
git log --graph
37. git rebase –interactive
Rebase with an interactive interface to edit commits.
git rebase -i HEAD~3
38. git reset –soft
Reset the current branch to a specific commit, but keep changes staged.
git reset --soft HEAD~1
39. git reset –mixed
Reset the current branch to a specific commit, but keep changes unstaged.
git reset --mixed HEAD~1
40. git show-branch
Show branches and their commits.
git show-branch
41. git whatchanged
Show logs with differences introduced in each commit.
git whatchanged
42. git help
Get help for any Git command.
git help <command>
43. git instaweb
Instantly browse your working repository in a web browser.
git instaweb
44. git verify-pack
Validate packed Git archive files.
git verify-pack -v pack-file
45. git fsck
Check the filesystem for integrity.
git fsck
46. git gc
Clean up unnecessary files and optimize the local repository.
git gc
47. git prune
Remove unreachable objects from the database.
git prune
48. git commit –amend
Amend the last commit.
git commit --amend
49. git blame -L
Limit the output to a specific range of lines.
git blame -L 10,20 file.txt
50. git fetch –all
Fetch all remotes.
git fetch --all
51. git submodule
Initialize, update, or inspect submodules.
git submodule add https://github.com/user/repo.git
git submodule update --init --recursive
52. git rev-parse
Parse revision (or other objects).
git rev-parse HEAD
53. git tag -a
Create an annotated tag.
git tag -a v1.0 -m "Version 1.0"
54. git archive –prefix
Create a tar or zip archive of the repository with a specific prefix.
git archive --format=zip --prefix=project/ HEAD > project.zip
55. git cherry
Find commits that are in one branch but not another.
git cherry
56. git diff –cached
Show changes between the index and a named commit.
git diff --cached
57. git diff HEAD
Show changes between the working directory and the latest commit.
git diff HEAD
58. git gc –aggressive
Perform an aggressive garbage collection.
git gc --aggressive
59. git grep -n
Show line numbers when searching with git grep
.
git grep -n "search-term"
60. git ls-remote
List references in a remote repository.
git ls-remote origin
61. git merge –no-ff
Merge with a commit, not a fast-forward.
git merge --no-ff new-branch
62. git pull –rebase
Rebase instead of merge when pulling.
git pull --rebase origin main
63. git push –tags
Push all tags to a remote repository.
git push origin --tags
64. git rebase –onto
Rebase on a different branch.
git rebase --onto newbase oldbase branch
65. git reflog expire
Expire old entries in the reflog.
git reflog expire --expire=now --all
66. git remote rename
Rename a remote.
git remote rename origin upstream
67. git remote rm
Remove a remote.
git remote rm origin
68. git rev-list
List commit objects in reverse chronological order.
git rev-list HEAD
69. git show-ref
List references in the repository.
git show-ref
70. git tag -d
Delete a tag.
git tag -d v1.0
71. git pull –prune
Fetch and remove remote-tracking references that no longer exist on the remote.
git pull --prune
72. git update-index
Update the index manually.
git update-index --assume-unchanged file.txt
73. git diff-tree
Compare the content and mode of blobs found via two tree objects.
git diff-tree -p commit-hash
74. git format-patch
Generate a patch file for email submission.
git format-patch -1 HEAD
75. git mv -f
Force move or rename a file.
git mv -f oldname.txt newname.txt
76. git replace
Create, list, delete references to replace objects.
git replace commit-hash
77. git rerere
Reuse recorded resolution of conflicted merges.
git rerere
78. git show –name-status
Show commit details with file status.
git show --name-status
79. git verify-commit
Check the GPG signature of commits.
git verify-commit commit-hash
80. git verify-tag
Check the GPG signature of tags.
git verify-tag v1.0
81. git clean -d
Remove untracked directories as well.
git clean -fd
82. git diff –check
Warn if changes introduce trailing whitespace.
git diff --check
83. git init –bare
Create a bare repository.
git init --bare
84. git rebase –skip
Skip the current patch when rebasing.
git rebase --skip
85. git commit –squash
Squash commits when merging.
git commit --squash commit-hash
86. git merge –abort
Abort a merge in progress.
git merge --abort
87. git blame –show-email
Show email addresses in the blame output.
git blame --show-email file.txt
88. git commit –signoff
Add a Signed-off-by line at the end of the commit message.
git commit --signoff -m "Commit message"
89. git diff –name-only
Show only names of changed files.
git diff --name-only
90. git log –stat
Include stats in the log output.
git log --stat
91. git merge –squash
Merge changes, but do not commit them.
git merge --squash branch
92. git pull –tags
Fetch all tags from a remote.
git pull origin --tags
93. git reset –merge
Reset the index and update files in the working tree.
git reset --merge
94. git revert
Create a new commit that undoes a previous commit.
git revert commit-hash
95. git log –author
Filter the log by author.
git log --author="Your Name"
96. git shortlog -s
Summarize the number of commits per author.
git shortlog -s
97. git reflog show
Show the reflog for a reference.
git reflog show HEAD
98. git reset –hard ORIG_HEAD
Reset to the original state after a merge or pull.
git reset --hard ORIG_HEAD
99. git pull –no-commit
Pull changes, but do not commit them automatically.
git pull --no-commit origin main
100. git config –list
List all configuration settings.
git config --list
There you have it, 100 Git commands. Some are everyday essentials, others more niche. Keep this guide handy, and soon you’ll navigate Git like a pro.
Also Read: