In this post, we're going discuss some very useful tricks on Git which literally can save your ass if you screwed up things on Git. Now, without any further ado, letβs get started ππ»ββοΈ
Fix last commit message
Have this ever happened to you that you actually want to commit "Final comment" but what you actually typed is "Final commment". Well, it's a shame if other people found out that your comment consists of three m's.
Thankfully there's a fix for this issue.
git commit --amend
Just open your project directory on your terminal and type the above command. This will open up your editor and allow you to make a change to that last commit message. This is a real life saver for me.
Change branch name
Let's suppose that, you want to create a branch named release but somehow you named your branch relaese. Don't panic, there's a solution for this too.
git branch -m relaese release
This will save your butt. But if you have already pushed this branch, then there are a couple of extra steps required. The solution for then will be, you need to delete the old branch from the remote and push up the new one.
git push origin --delete relaese
git push origin release
Added a wrong file in the repo
If you ever commit something in your repo that you shouldn't suppose to then you know how bad the situation is. It could be a rogue ENV file, a build directory, a picture of your dog (suppose π) that you accidentally saved to the wrong folder? Itβs all fixable.
If you haven't commited it yet then you only have to reset the file.
git reset /assets/img/unknown.jpg
If you have gone committing that change, no need to worry. You just need to run an extra step before:
git reset --soft HEAD~1
git reset /assets/img/unknown.jpg
rm /assets/img/unknown.jpg
git commit
This will undo the commit, remove the image, then add a new commit in its place.
Everything went wrong
This is your ace of spades! When whatever you do go wrong and you have no clue what to do then this is your solution. For example, when you have copy-pasted one too many solutions from Stack Overflow and your repo is in a worse state than it was when you started, then this is your lifesaver.
git reflog
It shows you a list of all the things you've done so far. It then allows you to use Git's time-traveling skills to go back to any point in the past.
When you run this command, it shows something like this:-
4gg9702 (HEAD -> release) HEAD@{0}: Branch: renamed refs/heads/relaese to refs/heads/release
4gg9702 (HEAD -> relaese) HEAD@{2}: checkout: moving from master to release
3c8f619 (master) HEAD@{3}: reset: moving to HEAD~
4gg9702 (HEAD -> feature-branch) HEAD@{4}: commit: Adds the client logo
3c8f619 (master) HEAD@{5}: reset: moving to HEAD~1
48b743e HEAD@{6}: commit: Adds the client logo to the project
3c8f619 (master) HEAD@{7}: reset: moving to HEAD
3c8f619 (master) HEAD@{8}: commit (amend): Added contributing info to the site
egb38b3 HEAD@{9}: reset: moving to HEAD
egb38b3 HEAD@{10}: commit (amend): Added contributing info to the site
811e1c6 HEAD@{11}: commit: Addded contributing info to the site
fgcb806 HEAD@{12}: commit (initial): Initial commit
Remember the left-side column, represents the index. If you want to go back to any point in the history, run the below command, replacing {index} with that reference, e.g. egb38b3.
git reset HEAD@{index}
Have some Git tricks your own? Let me know in the comments below.
Thanks for reading! If you found this helpful, donβt forget to share this with your friends and followers!
Latest comments (54)
The non-intuitive nature of Git seems to be an opportunity for writing a full-featured GUI on top of the arcane command line wizardry required to do most anything other than a commit.
git commit --amend
will add anything in the staging area (or index; the thing files get to when usinggit add
), and let you change the commit message. If you only want to change the commit message leaving the index alone, you can usegit commit --amend --only
.When you want to remove a file from the latest commit, after using
git reset --soft
and co. you can usegit commit -C ORIG_HEAD
so you don't have to enter the same commit message.ππ»
An easier way to remove a file that has been added to the last commit by mistake:
This doesn't delete the file, though. Dropping '--cached' would delete it.
Great ππ»
There is a short hand for renaming the current branch which isn't mentioned:
git branch -m new_branch
Thanks for sharing π
git bisect
but that needs a whole article in itself. If you haven't heard of it, it's a way of quickly pinpointing at which point in history a commit was made, eg. you just noticed a bug, and want to find which code change caused it.Wow, thanks for sharing π
git commit --amend
can be use to add new modifications to the previous commit.You can edit a file and mark it to commit
and then
to insert the modifications in the previous commit.
"Hacks" is pretty inaccurate, more like "commands"
You got me π
Sure, DM me on Twitter
I knew git amend but git reflog that's a cool one ... and git checkout - is a really handy one
ππ»
Reflog saved me recently. Such a good one
ππ»
Some comments may only be visible to logged-in visitors. Sign in to view all comments.