Using git has completely integrated itself into the development workflow. It is a extremely powerful tool for managing your source code. It has also proven itself as a great review, OTAP and deployment tool.
When using git in the CLI you will have to write a lot of long git commands to get things done. Most git commands are simple but some of them are quite complex and bothersome to type. Since we developers are of course taught to be lazy (for the sake of being the stereotype), and to write as little as possible.
Because the above applies to me, I use git aliases to shorten my git commands that I use most, and improve the information they return. In this blog post I will give a list of simple but powerful aliases that you might grow to depend on an love.
Simple git aliases for often used git commands
Some git aliases are really straightforward, they just replace the original git command and are meant to make you write less. Here is a list of these aliases:
co = checkout # Checkout a branch cob = checkout -b # Checkout a new not yet existing branch f = fetch -p # Fetch from a repository and prune any remote-tracking references that no longer exist on the remote. c = commit # Commit you changes p = push # Push you changes to a remote ba = branch -a # List both remote-tracking branches and local branches. bd = branch -d # Delete a branch only if it has been merged bD = branch -D # Force delete a branch dc = diff --cached # Display the staged changes
Checking the status of your git commit
Checking the status of the commit you’re working on belongs to the daily routine of most developers. We make changes to something we check what files we changed before staging our changes to see if we didn’t change a file that we were not supposed to change.
Making the git command shorter while trying to make it more informational.
st = status -sb
Stage your git changes in patches
I really like to review my changes before adding them, maybe you do too. Git diff is a really good tool for this, but what if you only want to add parts from the changes you made, so you can spread them over multiple commits, or if you want to review every small step.
In this case
git add -p comes to our rescue!
a = add -p
More helpfull git logs
Git logs help us to see what happened to the code we’re working on. They are a history of the changes made but also when it was branched and when that branch has been merged back. When we write good commit messages the git log becomes an extremely important tool to see the changes made to the code and why these changes were made.
Improving the output of the git log will make it easier to spot these changes.
plog = log --graph --pretty='format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%aN%C(reset) %s'
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
tlog = log --stat --since='1 Day Ago' --graph --pretty=oneline --abbrev-commit --date=relative
Contributors ordered by number of merges
Finding out the top contributors of the project can come in handy when you want to find out who to speak to when you have a question. Or it is just a plain fun tool who is working the “hardest” ;).
rank = shortlog -sn --no-merges
Remove all merged branches
Before making a pull-request we need to make a branch locally where we can commit our work to. After a while you will end up with a lot of branches that are already finished and merged into you master branch. Deleting these one by one can be a real hassle.
When these merged branches become big in number, deleting all those with a single alias becomes really handy. This alias will remove all branches merged into the branch you’re on.
bdm = "!git branch --merged | grep -v '*' | xargs -n 1 git branch -d"
[alias] # Shortening aliases co = checkout cob = checkout -b f = fetch -p c = commit p = push ba = branch -a bd = branch -d bD = branch -D dc = diff --cached # Feature improving aliases st = status -sb a = add -p # Complex aliases plog = log --graph --pretty='format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%aN%C(reset) %s' tlog = log --stat --since='1 Day Ago' --graph --pretty=oneline --abbrev-commit --date=relative lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit rank = shortlog -sn --no-merges bdm = "!git branch --merged | grep -v '*' | xargs -n 1 git branch -d"
Robert-Jan de Dreuhttp://google.com
Hey, I'm Robert-Jan de Dreu. A programmer mostly focusing on the web. While using PHP and MVC I will make sure the program I'm building is well documented and structured. I use this blog to show what I made and keep reminding myself of little tasks that I have done and maybe need to do again.
I live in Utrecht, The Netherlands and can be found in Ibuildings where my job is at. I'm available for freelancing if you want me to do a project for you. Just send me a message.