DEV Community

loading...

Git alias trick: Comparing your local branch to the upstream version

Rose
Dev by profession and hobby. Not a ton of time for hobbies these days though! Mom of two young kids. Also really into dog training (agility, nose-work, advanced obedience, etc) & reading fiction. 😃
・2 min read

Hello! I would like to share a small Git alias that has made my life so much better: The ability to compare my local branch to its upstream equivalent. I often want to see exactly what changes I’m about to push before I pull the trigger, so I use this a lot.

In your .gitconfig’s [alias] section:

[alias]
  du = "diff @{upstream}"

And then you can type in

git du

or even better

g du

if you have g aliased to git

🤓 But Rose I am a bit of a Git newbie and I have no idea what any of those words mean

💁‍♀️ A bit of information on remote repos

Let’s say you have a repository on GitHub. When you work locally you clone that repo. The GitHub one is saved as a remote which can have a name of your choosing (since you can define multiple remotes.) When you use git clone the remote that you cloned from is by default named origin.

👁 A bit of information on upstream branches

Individual branches that you name can be set to “track” an upstream branch, which means that when you use things like git push and git pull Git will know that your local master needs to pull from origin/master, or your local bug_fixes needs to pull from origin/bug_fixes

If you have a local bug_fixes branch already and you want to set it up to start tracking an upstream branch, you can do so by checking out your local bug_fixes (git checkout bug_fixes) and then typing git branch -u origin/bug_fixes

ℹ️ Elaborating on the diff-ing of upstream branches

If you type in git diff origin/master when you have master checked out locally, you can see the difference between your local master and the master on origin.

But having to type out origin/branch_name_here every time is a bit tedious. Another option is to use this handy built-in Git shorthand: @{upstream} which then can be shortened even further to @{u}, so git diff @{u}

💅 However, I am even lazier than that, hence the even shorter alias du

🤔 OK but what are aliases?

Don’t know what aliases are? You totally should, because they are awesome. Here is a how-to on Git aliases. I have a handful that I love - g du which I just talked about here, or g co instead of git checkout or g br instead of git branch It might seem silly, but saving a little bit of effort on the number of characters I have to type is very satisfying 🤤

…Other hints

If you use oh-my-zsh there are already some cool plugins available that let you quickly set up a ton of Git aliases, which is worth checking out if you are just starting your Git hacking journey.

If you have a pretty minimal .gitconfig right now, it is worth searching through the dotfiles of notable developers on GitHub (many devs set up a repo for their dotfiles - here’s an example) and seeing what cool tricks they are using.


OK that’s it! Thanks for reading all the way to the end 👋

Discussion (0)