Some handy git tricks

A few little git tricks to make dev work a little easier

Update

I have updated both the git pr and gh aliases to be more succinct and work better with the different ways that you can clone git repos.


At RightScale we use git as our version control system. On the whole it’s pretty great, but there are always ways to improve tooling. I have included a few useful tricks.

Command line tricks

If you’re anything like me you are constantly jumping between branches. cd - allows you to move back to your previous directory. You can use git co - command in a similar way to navigate back to your previous branch. So if I was on the master branch and moved to another branch, I can navigate back very easily.

cloud_analytics master
❯ git checkout temp_branch
Switched to branch 'temp_branch'

cloud_analytics temp_branch
❯ git checkout -
Switched to branch 'master'

cloud_analytics master
❯

Git aliases

Git aliases are increadibly powerful. They act like regular aliases that you would set up in your .bash_profile but they always run under the git namespace and can be configured on a per repository basis.

General aliases

I have a few pretty basic aliases setup that are nothing special. (More info on aliases ).

[alias]
  st    = status
  ci    = commit
  co    = checkout
  br    = branch
  graph = log --graph --pretty=format:'%Cred%h%Creset %C(yellow)%an%d%Creset %s %Cgreen(%cr)%Creset' --date=relative

On the left is the alias and on the right is the command that it actually runs.

cloud_analytics master
❯ git checkout temp_branch
Switched to branch 'temp_branch'

❯ git co temp_branch
Switched to branch 'temp_branch'

Branch aliases

Now some more interesting aliases.

[alias]
  # Set Upstream  =====
  su  = "!git branch -u origin/\"$(git rev-parse --abbrev-ref HEAD)\""
  # Push branch =====
  pb  = "!git push origin \"$(git rev-parse --abbrev-ref HEAD)\""
  # pull upstream =====
  up  = "!git pull origin \"$(git rev-parse --abbrev-ref HEAD)\""

These aliases are all using git rev-parse --abbrev-ref HEAD to work out the name of the branch. It then uses this to push/pull or set upstream. (This will only work if you are working with a branch named the same locally and remotely)

cloud_analytics temp_branch
❯ git rev-parse --abbrev-ref HEAD
temp_branch

Other aliases

[alias]
  # Create pull request for current branch  =====
  pr = "!open \"http://github.com/$(git remote -v|sed -e '/^origin/!d' -e '/(push)$/!d' -e 's|^.*github\\.com[:/]\\([^. ]*\\).*$|\\1|')/pull/new/$(git rev-parse --abbrev-ref HEAD)\""
  # Go to github webpage  =====
  gh = "!open \"http://github.com/$(git remote -v|sed -e '/^origin/!d' -e '/(push)$/!d' -e 's|^.*github\\.com[:/]\\([^. ]*\\).*$|\\1|')\""
  # log =====
  lg  = log --pretty=format:'%Cred%h%Creset -%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

git pr is one of the most useful aliases I use. It opens a compare view (on GitHub) between master and your current branch. So if I was on temp_branch it would open a comparison between master and temp_branch.

GitHub.com tricks

Task lists

In GitHub markdown you can create task lists.

- [x] task 1
- [ ] task 2
- [ ] task 3

If you embed this task list in the initial comment section of your PR then it can be seen on the GitHub pull request page

Replying to comments

You can quote a particular part of a comment in a reply by highlighting it and pressing r

More resources