Skip to content

GitAlias/gitalias

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Git Alias

This project provides many git alias commands that you can use as you like.

Contents:

Introduction

What is Git Alias?

Git Alias is a collection of git version control shortcuts, functions, and commands:

  • Shortcuts such as s for status.

  • Improvements such as optimize to do a prune and repack with recommended settings.

  • Workflows such as topic-start to create a new topic branch for a new feature.

  • Visualizations such as graphviz to show logs and charts using third-party tools.

Where is the code?

To see the complete code, view this file:

Why use this?

We are creating this alias list because we type these commands many times daily, and we want the commands to be fast and also accurate.

We often work on teams, across many companies and organizations, and using multiple shells. We want to count on a set of aliases. For shorter commands, such as s for status, fast speed is nice. For longer commands, such as repacker, accurate settings are important.

Install

Install with typical usage

Download the file gitalias.txt and include it:

curl https://raw.githubusercontent.com/GitAlias/gitalias/master/gitalias.txt -o ~/.gitalias 
git config --global include.path ~/.gitalias

Install with custom usage

Download the file gitalias.txt any way you want, such as:

curl -O https://raw.githubusercontent.com/GitAlias/gitalias/master/gitalias.txt

Manually edit your git config dot file any way you want, such as:

vi ~/.gitconfig

Include the path to this file:

[include]
path = ~/.gitalias

Examples

Shortcut examples

Letters:

a = add

b = branch

c = commit

Letters with options:

ap = add --patch

bm = branch --merged

ci = commit --interactive

Popular examples

Summarizing:

dd = diff --check --dirstat --find-copies --find-renames --histogram --color

ll = log --graph --topo-order --abbrev-commit --date=short --decorate --all --boundary --pretty=format:'%Cgreen%ad %Cred%h%Creset -%C(yellow)%d%Creset %s %Cblue[%cn]%Creset %Cblue%G?%Creset'

Committing:

cam = commit --amend --message

rbi = rebase --interactive @{upstream}

Accelerator examples

Logging:

log-graph = log --graph --all  --decorate --oneline

log-my-week = !git log --author $(git config user.email) --since "1 week ago"

Searching:

grep-group =  grep --break --heading --line-number

grep-all = !"git rev-list --all | xargs git grep '$1'"

Recovery examples

Backtracking:

uncommit = reset --soft HEAD~1

cleanout = !git clean -df && git checkout -- .

Preserving:

snapshot = !git stash push "snapshot: $(date)" && git stash apply "stash@{0}"

archive = !"f() { top=$(rev-parse --show-toplevel); cd $top; tar cvf $top.tar $top ; }; f"

Traversal examples

Find the branch's previous commit hash:

git branch-commit-prev

Find the branch's next commit hash:

git branch-commit-next

Coordination examples

Combining:

ours   = !"f() { git checkout --ours $@ && git add $@; }; f"

theirs = !"f() { git checkout --theirs $@ && git add $@; }; f"

Comparing:

incoming = !git remote update --prune; git log ..@{upstream}

outgoing = log @{upstream}..

Workflow examples

Synchronizing:

get = !git fetch --prune && git pull --rebase=preserve && git submodule update --init --recursive

put = !git commit --all && git push

Publishing:

publish = "!git push -u origin $(git current-branch)"

unpublish = "!git push origin :$(git current-branch)"

Branching:

topic-start = "!f(){ b=$1; git checkout master; git fetch; git rebase; git checkout -b "$b" master; };f"

topic-stop = "!f(){ b=$1; git checkout master; git branch -d "$b"; git push origin ":$b"; };f"

Optimization examples

Naming:

top-name = rev-parse --show-toplevel

branch-name = rev-parse --abbrev-ref HEAD

upstream-name = !git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)

Pluralizing:

branches = branch -a

stashes = stash list

tags = tag -n1 --list

Streamlining:

pruner = !git prune --expire=now; git reflog expire --expire-unreachable=now --rewrite --all

repacker = !git repack -a -d -f --depth=300 --window=300 --window-memory=1g

expunge = !"f() { git filter-branch --force --index-filter \"git rm --cached --ignore-unmatch $1\" --prune-empty --tag-name-filter cat -- --all }; f"

Customization

If you want to use this file, and also want to change some of the items, then one way is to use your git config file to include this gitalias file, and also define your own alias items; a later alias takes precedence.

This section has examples that include this file, then add a customization.

Status

To do your own custom terse status messages:

[include]
path = ~/.gitalias
[alias]
s = status -sb

Log

To do your own custom log summaries:

[include]
path = ~/.gitalias

[alias]
l = log --graph --oneline

Format

To do your own custom pretty formatting:

[include]
path = ~/.gitalias

[format]
pretty = "%H %ci %ce %ae %d %s"

Contributor advice

Typically a short alias for a command and its options is in the same order as the command and option words:

  • Right: fab = foo --alpha --bravo

  • Wrong: baf = foo --alpha --bravo

  • Wrong: abf = foo --alpha --bravo

Typically a short alias for a command and its options uses the first letter of each option word:

  • Right: fab = foo --alpha-bravo

  • Wrong: fa = foo --alpha-bravo

  • Wrong: fb = foo --alpha-bravo

Epilog

See also

More ideas for git improvements:

  • If you want to alias the git command, then use your shell, such as alias g=git.

  • If you want history views, see git-recall

  • If you use oh-my-zsh, then you may like the git plugin

  • If you use vim then see Fugitive

  • If you use emacs then see Magit

  • If you use git shell scripting then see SCM Breeze

  • If you use node then see git-alias

For more git config ideas, and for credit for many of the aliases here, please see these excelent resources:

References:

Thanks

Thanks to all the contributors, including all the creators of the projects mentioned above.

Thanks to these people for extra help, in alphabetic order: