Everyday Git Aliases
Git gives you as much flexibility in how you construct your VCS workflow as it does for the commands you use on your local repo. In your gitconfig file you can add alises for your favourite commands, in this article I'll talk about mine. You can see my gitconfig on github.
git standup
alias for git log ––since yesterday ––author joe ––all
Just about to head to a standup but you can't remember everything that you did yesterday? This command will come to your rescue. It only lists what you did in the last 24 hours.
git purr
alias for git pull ––rebase
In Git you have public and private branches, public branches are the ones on github (or your own git host), private branches are in your local git repo and are setup to be tracking or topic. A tracking branch is linked to a public branch and a topic is only in your local git repo.
You have two options when you want to integrate changes between branches, either you merge or rebase. By default when you do a pull on a tracked branch it performs a fetch then a merge. If you've made changes locally and someone else has pushed changes to your git host then git will automatically merge these together with a merge commit.
On an active project with other colleagues using pull will generate a load of these noisy commits in your projects history. I only like merge commits to be in the history when a topic branch has been reintegrated. Tracking branches should have a linear history.
When you do a git pull ––rebase, git fetches the changes from your remote repo and then perform a rebase rather than the default merge. A rebase resets the HEAD of your local branch to be the same as the remote HEAD, then replays your local commits back into repo. This means you don't get any noisy merge messages in your history. As well as giving a linear history, this also helps when using bisect.
git st
alias for git status -sbGit gives a verbose output when you perform a status which is excellent when you are getting started with git. As you become used to the output you want a shorter version. The output of this alias shows a single letter that represents the change type and reports how far ahead of the remote branch you are.
git ready
alias for git rebase -i @{u}Once you've committed a few local changes you'll want to share them with your team by pushing to your git host. Before I push I always run the git ready alias to see what's going to be pushed so I can reword commit messages and squash related commits together. git ready performs an interactive rebase on your unpushed commits.
Let's say I've pushed two commits that are related to a new feature and I have another where I made a spelling mistake in the commit message. When I run git ready I get dropped into vim with this input.
I want to squash the two whizzbang feature commits together. So I change pick to say s to squash the two together into a single commit. I also want to reword the commit with the spelling mistake. To do this I make the file look like:
This gives me two new commit messages to edit, which I update. Now when I push the remote repo host receives two commits
git lg
alias for git log ––pretty=format:'%Cred%h%Creset -%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'When I'm viewing history I just want to see the SHA, the commit message, who made the commit and how long ago. This custom log output gives me:
I'd be interested to hear about your favourite git aliases too.