My Git Aliases

January 9, 2021 6 By addshore

Overtime key presses really add up, especially when you use certain tools all throughout the day.

Here are the bash and git aliases that I use for git to avoid doing some of these keypresses.

I’m not sure how many times a day I use these shortcuts, or how many key presses I skip, but I’m fairly sure my keyboard is thanking me.

bash alias for git (g)

Even typing git for me seems like a bit much at times. So for years I have has the alias g to avoid the 2 extra keypresses.

For this in your all you need is the following like in your .bashrc file or .bash_aliases file if you have one.

alias g='git'Code language: JavaScript (javascript)

auto completion

This stackoverflow answer gives the context and steps around the solution for auto completion of such aliases.

The installation includes creating a ~/.bash_completion.d/complete_alias file which is then loaded into your .bashrc or .bash_aliases file as follows.

# Auto completion for bash aliases
# https://unix.stackexchange.com/a/332522
source ~/.bash_completion.d/complete_aliasCode language: PHP (php)

Once loaded you can turn on completion for an alias with the following in your your .bashrc or .bash_aliases file.

complete -F _complete_alias g

git aliases

Git aliases live in your .gitconfig file under the [alias] section, such as:

[alias]
	d = diff

Command shortcuts

As already expressed in this post, I want to minimize key presses, so I have very short aliases for some frequent normalish git tasks.

d = diff
c = commit -a
pu = pull
co = checkout
mt = mergetool
cp = cherry-pick
cpc = cherry-pick --continue
com = "!f() { git checkout $(git main); }; f"
hash = rev-parse HEAD
setx = update-index --chmod=+x
amm = commit -a --amend
amn = commit -a --amend --no-edit
claim = commit --amend --no-edit --reset-author
ro = "!f() { git reset --hard origin/$(git main); }; f"Code language: JavaScript (javascript)

Utility shortcuts

Some commands end up needing to know what the main branch is called. This could be master, main or something else. So there is an alias for that too that is used in multiple commands.

main = "!git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'"Code language: JavaScript (javascript)

Mop

I’m a big fan of simplicity when it comes to keeping things up to date cleanly, without losing things (always stash)

g mop does this for me by stashing any changes, checking out the main branch, resetting incase its referring to the wrong place and doing a pull.

mop = !git stash save "mop" && git com && git ro && git pu Code language: JavaScript (javascript)

Some projects that I work on end up with hundreds of git repos checked out in a single directory.

I make use of sem, a command that allows multiple processes to be queued up and run asynchronously to get this done quickly.

mop-all = "!find $GIT_PREFIX -maxdepth 1 -type d -print -exec sem --bg -j 4 git -C {} mop \\; && sem --wait; echo mop-all done; "Code language: JavaScript (javascript)

This will run the mop command in all directories contained in the current directory, running at most 4 a time.

Gerrit helpers

Lot’s of my time in git is spent interacting with Gerrit, which has a different interaction pattern in terms of submitting code for review.

Many people use cli tools such as git-review, but I prefer to keep it simple. Reading the docs, this is all you need. One command to push a change to be reviewed, another to push a draft.

p = "!f() { git push origin HEAD:refs/for/$(git main)%ready; }; f"
pd = "!f() { git push origin HEAD:refs/for/$(git main)%wip; }; f"
Code language: JavaScript (javascript)

Others

These are cool too:

hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
coauthor-line = "!f() { echo \"Co-authored-by: $(git config user.name) <$(git config user.email)>"; }; f"Code language: PHP (php)