The Cool Git: 5 Not so Well Known Tricks

Git clone, git commit, git push. That’s part of the bread and butter of any developer nowadays. 

But why stop there? Git is a much more powerful software and it would be boring to only do such. Here are 5 not so well known tricks that have saved me hours in my life as a software developer. 

Let’s look at the 5 tricks using Watermelon’s repo as an example.

git reflog

Run this when you screwed everything and need a time machine

This is the one I use the most. A lot of times you want to go back in time to when stuff worked. Sometimes you accidentally delete stuff, or merge bad code. Reflog fixes this. 

You will see the list of all things you’ve done across branches. With reset, you will be able to go back to that moment.

git log

Run this when when you want to see how a file has changed throughout time

This one is basically a movie rewind. I run this when I want to understand how a file has changed. It might be that I want to understand how those changes are intertwined with changes in some other file of the repo. It might also be that I want to understand who has made certain changes in a given lapse of time.

We are passing these flags to get a list of commit hashes and authors grouped with their respective code diffs.

git pull –-rebase

Run this when you don’t have many local changes and those don’t deserve to be in a new branch

When learning gitflow you are taught to branch and merge. This makes a lot of sense because you usually don’t push frequently and you accumulate a bunch of changes on the remote branch before pushing.

However, if you’re only making small changes within a short time lapse, this command will help you keep the commit history organized. Using it is pretty straightforward.

git cherry-pick

Run this when you accidentally committed changes to a wrong branch

Let’s say that I have a branch called dev and another one called git/cherry-pick. Let’s say that I added a file commited_by_mistake.js to the dev branch and accidentally committed it. This command will help us move such commits to git/cherry-pick.

git blame

Run this when you want to get the historical context of your code

I use git blame when I want to know who wrote changes in a range of lines of code. This allows me to later ping that person on Slack and ask very specific questions. It’s also very simple to use. Just tell the command which file and range of lines you want to get that context from.

In fact, Watermelon is a UI for git blame with superpowers. Highlight a piece of code, and within the VS Code UI we tell you who made those changes. We also give you the relevant PR comments around that snippet of code.

Go ahead and download our VS Code extension, and please star us on GitHub to give visibility to the project so that we can continue developing it.