Skip to content

Instantly share code, notes, and snippets.

@robmiller
Created July 17, 2013 07:52
Star You must be signed in to star a gist
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"
@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