Git has got a lot of new features over the years and most(if not all) of the obscure git checkout
variations have been extracted to more sensible global commands. Today, I’ll list out a few git tricks which can make your life easier with a focus on these new “commands”
Sometimes, you just want to go back to the previous commit. Maybe you just realized that you forgot to add a file or maybe you just want to go back to the previous commit to see what you were doing. Whatever the reason, you can do that with just one command.
git reset --hard HEAD~1
If you don’t want to change the state of your working directory, just omit the --hard
flag.
git reset HEAD~1
This changes the HEAD pointer without altering the files in your working directory.
If you just want to look at the previous commit’s contents though, you don’t need to change the state of your working directory. You can just do this:
git show HEAD~5
You can even pass it a commit hash. The result will look something like this
And finally, if you want to move that commit to a different branch, you can use cherry-pick
.
git switch branch-you-want-to-move-the-commit-to
git cherry-pick commit-hash
git log
is one of the most simplest commands. Yet it has so many flags that you can use for pretty much anything. I will be going over a few of them.
You could manually invoke grep
but that wouldn’t really treat multiple lines as a single commit. So, you can use the --grep
flag to search for a commit by message.
git log --grep="some text"
You can even invert the search to find commits which don’t have the text you’re searching for.
git log --invert-grep --grep="some text"
I generally use git log
with the --oneline
flag. It makes the output much more readable. If you want something more fancy, you can use the --graph
flag.
git log --oneline --graph
Another improvement is hiding merge commits from the output. Most of the time, merge commits are just noise and its unlikely you want to see them(of course, in case of conflicts, that probably doesn’t hold true)
git log --oneline --no-merges
Lastly, there’s the -p
flag which shows the changes made by the commit in the log output itself.
git log --oneline -p
Say you unzipped a huge zip file in your git repo and now its messing up your git status
. Instead of manually deleting each file, you can use git clean
git clean -i
This will show you a list of untracked files and you can either delete them all in one go or by patterns, or by manually selecting the files to delete.
If you want to live dangerously, you can use the -f
flag to delete all untracked files without a prompt.
git clean -f
However, this only works for untracked files - that is, files that haven’t been committed yet and aren’t in the staging area.
If you want to restore an uncommitted(and changed) file to its previous state, you can use git restore
git restore file-name
and if its staged, you need to remove it from the staging area first.
git restore --staged file-name
and then do the same as above. If you accidentally did a git add .
and want to remove all the files from the staging area, you can do this:
git restore --staged .
And that’s about it! I hope you learned something new today. If you found this article interesting, you might want to check out How to undo (almost) anything with Git - Github.Blog
And as always, thanks for reading!