10 Advanced Git workflows that will come in handy
Dinesh S
Posted on November 7, 2022
We all know the common Git commands which we use everyday, like commit
, push
, pull
etc.
Here are 10 less known commands which can be very useful.
1⃣ Undo a git rebase
You did a rebase & now you want to undo it. Do git reflog and find the point where you want to go. (Ex: HEAD@{10}) You will find something like the below.
git reflog
<sha> HEAD@{9}: rebase: action
<sha> HEAD@{10}: commit: action
HEAD@{9}
is the point where rebase was done. So to revert that, we have to do a hard reset to the previous point.
Use the value to do a hard reset.
git reset --hard HEAD@{10}
The rebase you did is undone.
2⃣ Hunt a mysterious bug in your git history
This command uses a binary search algorithm to find which commit in your project’s history introduced a bug. You use it by first choosing a "bad" commit that is known to contain the bug, and a "good" commit that is known to be clean, before the bug was introduced. Git bisect picks a commit between those two points and asks you whether the selected commit is "good" or "bad". It continues narrowing down the range until it finds the exact commit that introduced the change.
git bisect
3⃣ Modify the commit message of a specific commit
git rebase -i head~2
The above command display's interactive rebase for the last 2 commits from head. Choose the number based on where your commit lies from head. If it is the 2th commit, choose head~2. You will get something like below
pick <sha> <commit message>
pick <sha> <commit message>
Change the command to edit
instead of pick
from the above, corresponding to the commit you want to modify
pick <sha> <commit message>
edit <sha> <commit message>
Now go ahead & save and the rebase will stop at the 2nd commit. You can amend the commit to change the commit message. Do git rebase --continue to finish the rebase.
git commit --amend
git rebase --continue
4⃣ Amend last commit
This comes in very handy if you want to change the commit message of the last commit. It can also be used to add new changes to the previous commit, instead of creating a new commit.
git commit --amend
5⃣ Recover a dropped stash
You have dropped a stash, but now you want it back. If you know the hash of the dropped stash you can do the below to retrieve it.
git stash apply $stash_hash
When you drop a stash, a hash of it is printed on the screen. If you haven't closed your terminal, you can locate it by scrolling to the top of your terminal window. If you can't find it, execute the below command.
gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
This will open gitk (git gui). To spot stash commits, look for commit messages that look like this.
WIP on somebranch: commithash Some old commit message
Once you know the hash of the commit you want, you can apply it as a stash
git stash apply $stash_hash
6⃣ Copy a file from one branch to another branch
If you want to copy an entire file from one branch into another branch, do the below
git checkout bugfix README.md
bugfix
is the source branch where the file README.md
is located.
Execute the above command from the branch (destination), where you want the file to be copied.
7⃣ Display the changes in a particular commit or a stash
git show <sha>
git show stash{0}
8⃣ Get the number of commits (Commit count)
To get the count for a specific revision
git rev-list --count <revision>
For example git rev-list --count main
will display the commit count in main
branch.
To get the count across all branches
git rev-list --all --count
9⃣ Revert a specific commit
Get the of the commit you want to revert and do this.
git revert <sha>
🔟 Rename a local branch
If you want to rename a branch that is different from the current branch you are in, do this
git branch -m <oldname> <newname>
If you want to rename the current branch you are in, you can do
git branch -m <newname>
Posted on November 7, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.