Created
July 17, 2013 07:52
-
-
Save robmiller/6018582 to your computer and use it in GitHub Desktop.
Some useful Git aliases that I use every day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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" |
Pretty nice collection. :-)
@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.
Useful and cool
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
Awesome gist! Especially
publish
(which I'd callpub
, i.e., even shorter, orpu
(like @mpictor) for push upstream) anddiffc
(which I'd callds
, shorter again and it stands fordiff --staged
, which is easier for me to remember thandiff --cached
) are nice. There are some very powerful aliases among these, so be careful using them!