1. Checkout a single file from another branch
Have you ever destroyed a file and just wished you could have a fresh start? Or needed the changes you made in one file in another branch? This command lets you grab just one file from another branch.
git checkout some-other-branch -- yarn.lock
You can use the same trick to checkout one file from a specific commit.
git checkout 9146367 -- yarn.lock
This is an effective trick if cherry-pick
would pick up other files that you don't need.
2. View the log without merge commits
Merge commits annoy some people. In fact, some people would rather never use the merge command because they are so annoyed by merge commits.
Personally, I think they are an important part of the history of a project, and you shouldn't try to circumvent them in your workflow.
That being said, if you want to look at a project's history in at a glance, you can use this flag to filter out merge commits.
git log --oneline --no-merges
3. Rewrite your last commit message
This one comes in handy when you accidentally commit something with a typo or misleading commit message.
git commit -v --amend
The -v
is optional, but I like it because it shows a lot of information about the changes which helps me to write a more descriptive commit message.
4. Get rid of all untracked changes
Pretty self-explanatory, but in case you're not familiar with the idea:
If you create a new file that didn't previously exist in the git history, you've made an untracked change. To start tracking that file, you'd need to commit it to the repo.
Sometimes, you change your mind halfway through a commit and really just want to start over without all the changes you've got. Well, git checkout .
will get rid of all the tracked changes you've made, but your untracked changes will still be floating around. To remedy that, we've got git clean
.
git clean -f -d
5. Print out a cool visualization of your log
This one mostly just makes you look cool. It can be useful, though, to visualize all of your long standing branches.
git log --pretty=oneline --graph --decorate --all
Try it out.
6. Ask git for a changelog
If you're looking for a condensed explanation of what changed, and who changed it, you can ask git to give you something that looks a lot like a changelog.
git shortlog <commit>..HEAD
In this example <commit>
should be replaced with the commit you want to target for the beginning of your log. Basically with git shortlog eafbc3c..HEAD
you're saying, "Show me what changed between commit eafbc3c
and right now."
The shortlog is grouped by commit author and shows the first line of each commit message. If your commit messages are well-written this should give you a solid idea of what each commit actually did.
You can do cool tricks like git shortlog HEAD~20..
to get the shortlog for the last 20 commits.
7. View the log for a specific date range
In a similar vein of thinking, you might need to see what changed in a repo between two days.
Thankfully, git has your back. The git log
command accepts --since
and --until
as flags.
So if I wanted to see what happened in Solidus between February 10th, 2016 and February 19th, 2016 I could run:
git log --since='FEB 10 2016' --until='FEB 19 2016'
Now, I can see that Murphy was pretty active in mid February.
8. List all git aliases
Sometimes you might alias a few commands and forget them later, or maybe there are some aliases defined by a shared config you use.
This is a trick I found somewhere, and even though it's not exclusively a feature of git, we are taking advantage of the git config
command.
git config -l | grep alias | sed 's/^alias\.//g'
Try it out, see if you have any forgotten aliases!
9. Search for commits that include a keyword
If you know exactly what piece of code you are looking for, or exactly what keyword you need to find changes on, you can search the log by code.
This will give you a list of commits that somehow affected a line of code or text containing your search string.
git log -S"config.menu_items"
In this example, I'll find a list of commits that somehow manipulated the string config.menu_items
.
10. Super secret list of git tutorials
Not that secret, but pretty super:
git help -g
Try it out and see. 🤠
git is a powerful tool, and is full of neat tricks. This list is in no way comprehensive or complete, so you should do some exploring and read the git documentation. Feel free to share any cool tricks you find on this thread, I love learning about the tools that help me write code!
If you liked this article, I wrote another one with more tricks: 10 More Git Tricks That You Should Know. 🤖