Skip to content

Instantly share code, notes, and snippets.

@robmiller
Created July 17, 2013 07:52
Show Gist options
  • Save robmiller/6018582 to your computer and use it in GitHub Desktop.
Save robmiller/6018582 to your computer and use it in GitHub Desktop.
Some useful Git aliases that I use every day
#
# Working with branches
#
# Get the current branch name (not so useful in itself, but used in
# other aliases)
branch-name = "!git rev-parse --abbrev-ref HEAD"
# Push the current branch to the remote "origin", and set it to track
# the upstream branch
publish = "!git push -u origin $(git branch-name)"
# Delete the remote version of the current branch
unpublish = "!git push origin :$(git branch-name)"
# Delete a branch and recreate it from master — useful if you have, say,
# a development branch and a master branch and they could conceivably go
# out of sync
recreate = "!f() { [[ -n $@ ]] && git checkout \"$@\" && git unpublish && git checkout master && git branch -D \"$@\" && git checkout -b \"$@\" && git publish; }; f"
# Fire up your difftool (e.g. Kaleidescope) with all the changes that
# are on the current branch.
code-review = difftool origin/master...
# Given a merge commit, find the span of commits that exist(ed) on that
# branch. Again, not so useful in itself, but used by other aliases.
merge-span = "!f() { echo $(git log -1 $2 --merges --pretty=format:%P | cut -d' ' -f1)$1$(git log -1 $2 --merges --pretty=format:%P | cut -d' ' -f2); }; f"
# Find the commits that were introduced by a merge
merge-log = "!git log `git merge-span .. $1`"
# Show the changes that were introduced by a merge
merge-diff = "!git diff `git merge-span ... $1`"
# As above, but in your difftool
merge-difftool = "!git difftool `git merge-span ... $1`"
# Interactively rebase all the commits on the current branch
rebase-branch = "!git rebase -i `git merge-base master HEAD`"
#
# Working with files
#
# Unstage any files that have been added to the staging area
unstage = reset HEAD
# Show changes that have been staged
diffc = diff --cached
# Mark a file as "assume unchanged", which means that Git will treat it
# as though there are no changes to it even if there are. Useful for
# temporary changes to tracked files
assume = update-index --assume-unchanged
# Reverse the above
unassume = update-index --no-assume-unchanged
# Show the files that are currently assume-unchanged
assumed = "!git ls-files -v | grep ^h | cut -c 3-"
# Checkout our version of a file and add it
ours = "!f() { git checkout --ours $@ && git add $@; }; f"
# Checkout their version of a file and add it
theirs = "!f() { git checkout --theirs $@ && git add $@; }; f"
# Delete any branches that have been merged into master
# See also: https://gist.github.com/robmiller/5133264
delete-merged-branches = "!git co master && git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d"
Copy link

ghost commented Dec 30, 2015

this is nice, thanks

@is-andrade
Copy link

Nice!

@felipear89
Copy link

You need to define the alias git co because it is used on delete-merged-branches ;)

@paul1994
Copy link

paul1994 commented May 6, 2016

Thanks.. I was looking on a shortcut to push to origin.

@ZeroDragon
Copy link

ZeroDragon commented May 17, 2016

how about:

git-fire = "git add .;git commit -am \"FIRE FIRE FIRE\";git push origin HEAD:fire-branch"

Will create a commit in case of fire in the building and push it to a "fire-branch" to avoid merge conflicts

@NinoDLC
Copy link

NinoDLC commented Jun 28, 2016

@ZeroDragon :
It'd be

# Save all work into a commit "FIRE"
fire = "!git add -A && git commit -m 'FIRE FIRE FIRE' && git push origin fire-branch"

@bantic
Copy link

bantic commented Jul 8, 2016

Thank you for this!
I changed the delete-merged-branches alias, adding --no-color to the git branch --merged command. When I had ui colors set to display, git branch --merged adds some unprintable characters to the output that cause the git branch -d command to fail:

"!git co master && git branch --no-color --merged | grep -v '\\*' | xargs -n 1 git branch -d"

@mpictor
Copy link

mpictor commented Oct 12, 2016

some I use a lot are

  • pu, "push upstream" (sets upstream if unset, pushes) - a more reliable version of an alias I found on stack overflow:

    pu=![[ $(git config branch.$(git symbolic-ref --short HEAD).merge) = '' ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push
    
  • po, "prune origin" (deletes merged branches):

    po=remote prune origin
    
  • bc, or "branch commit" - like git commit -m, but prefixes commit message with the branch name in brackets. Reads commit message from stdin, and limited to a single line. Aborts if nothing is added to the message.

    bc = "!f() { bname=\"[$(git symbolic-ref --short HEAD)] \"; read -i \"$bname\" -e && [[ ${#bname} -lt ${#REPLY} ]] && git commit -m \"$REPLY\" || echo aborted; }; f; unset f bname"
    

@rex
Copy link

rex commented Dec 23, 2016

@mpictor Although I came here for the publish alias that is going to make my life easier, that bc alias you provided is going to seriously streamline things locally for me. Like many teams, we use the Jira ticket number as the branch name and prefix each commit with it. Can't tell you how excited I am to just be able to run

git bc "Fooed some bars"

and it will commit

[XYZ-1234] Fooed some bars

❤️

@sureshshrestha
Copy link

sureshshrestha commented Jan 30, 2017

Great compilation. This one is missing:
Run merge test to check for any conflicts beforehand.
mergetest = "!f(){ git merge --no-commit --no-ff \"$1\"; git merge --abort; echo \"Merge aborted\"; };f "

@nocke
Copy link

nocke commented Mar 12, 2017

How is this nesting thing working, i.e. git merge-base... inside a git rebase alias...?

"!git rebase -i git merge-base master HEAD"

@erikhuizinga
Copy link

erikhuizinga commented Mar 29, 2017

Awesome gist! Especially publish (which I'd call pub, i.e., even shorter, or pu (like @mpictor) for push upstream) and diffc (which I'd call ds, shorter again and it stands for diff --staged, which is easier for me to remember than diff --cached) are nice. There are some very powerful aliases among these, so be careful using them!

@mlopezgva
Copy link

Pretty nice collection. :-)

@edloidas
Copy link

edloidas commented Jun 9, 2017

@nocke Just like it did in shell scripts. To capture the output of the nested command you can use backticks:
"!git rebase -i `git merge-base master HEAD`"
Works well in legacy Bourne shell. Or you can use $() syntax:
"!git rebase -i $(git merge-base master HEAD)"
You probably saw a similar approach in publish and unpublish aliases.
Both examples will retrieve merge base commit hash first (e.g. 6488bc910...) and then do something like git rebase -i 6488bc910.... So it's all the matter of readability.

@Nslkh
Copy link

Nslkh commented Jan 29, 2021

Useful and cool

@awendt
Copy link

awendt commented Dec 9, 2021

There is git branch --show-current which may be a worthy replacement for your alias git branch-name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment