8 Unique git commands
Shasheesh Purohit
Posted on January 7, 2023
git cherry-pick <commit>
:
Allows you to apply the changes introduced by a specific commit from one branch to another. For example, suppose you have a branch called "feature" with the following commit history:
* abcdefg (HEAD -> feature) Add new feature
* hijklmn Fix bug
* opqrstu Add tests
* vwxyzab Initial commit
To apply the changes introduced by the commit "hijklmn" to the current branch, you can use the following command:
git cherry-pick hijklmn
This would apply the changes introduced by the commit "hijklmn" on top of the current branch, resulting in a new commit with the changes from "hijklmn."
git bisect start
:
Begins a binary search algorithm to find the commit that introduced a bug in your code. To use git bisect, you must first mark a commit as "good" (i.e., a commit that does not contain the bug) and a commit as "bad" (i.e., a commit that does contain the bug). Git will then automatically perform a bisection search, checking out commits and asking you to mark them as good or bad, until it finds the commit that introduced the bug.
For example, suppose you have the following commit history:
* abcdefg (HEAD) Add new feature
* hijklmn Fix bug
* opqrstu Add tests
* vwxyzab Initial commit
And you know that the commit "opqrstu" is good (i.e., it does not contain the bug), and the commit "abcdefg" is bad (i.e., it does contain the bug). You can start the git bisect process by running the following commands:
git bisect start
git bisect good opqrstu
git bisect bad abcdefg
Git will then automatically check out and test intermediate commits, until it finds the commit that introduced the bug.
git stash save "message"
:
Temporarily saves changes that you are not yet ready to commit. For example, suppose you are working on a branch called "feature," and you have made some changes to a file called "main.js." If you want to save these changes without committing them, you can use the following command:
git stash save "Save changes to main.js"
This will save your changes to a temporary stash, allowing you to switch branches or perform other operations without committing your changes. To apply the stashed changes later, you can use the git stash pop command.
git diff <commit> <commit>
:
Shows the differences between two commits. For example, to see the differences between the last two commits on the current branch, you can use the following command:
git diff HEAD~1 HEAD
This will show you a list of the changes introduced by the last commit.
git blame <file>
:
Shows you the last commit that modified each line of a file. For example, to see the commit history for a file called "main.js," you can use the following command:
git blame main.js
This will show you the commit hash, author, and date for each line of the file.
git log
:
Shows you a history of all the commits made to a repository. This is useful for understanding the history of a project and seeing who made what changes. For example, you can use git log to see a list of all the commits, or git log -p
to see the changes introduced by each commit.
git reset
:
Allows you to undo commits by "resetting" the current branch to a previous commit. This is useful if you made a mistake and want to revert back to an earlier version of your code. For example, you can use git reset HEAD~1 to revert the last commit, or git reset abc123
to revert to a specific commit.
git rebase
:
Allows you to reapply commits on top of another base tip. This is useful for modifying the commit history of a branch, or for merging multiple branches together. For example, you can use git rebase master
to reapply the commits on your current branch on top of the master branch.
Posted on January 7, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.