Last active Aug 14, 2018

The Ultimate Git Alias Setup

If you use git on the command-line, you'll eventually find yourself wanting aliases for your most commonly-used commands. It's incredibly useful to be able to explore your repos with only a few keystrokes that eventually get hardcoded into muscle memory.

Some people don't add aliases because they don't want to have to adjust to not having them on a remote server. Personally, I find that having aliases doesn't mean I that forget the underlying commands, and aliases provide such a massive improvement to my workflow that it would be crazy not to have them.

The simplest way to add an alias for a specific git command is to use a standard bash alias.

# .bashrcalias s="git status -s"

The disadvantage of this is that it isn't integrated with git's own alias system, which lets you define git commands or external shell commands that you call with git <alias>. This has some nice advantages:

ability to see all your aliases and their corresponding commands using git config

If you add the following code to your .bashrc on a system with the default git bash completion scripts installed, it will automatically create completion-aware g<alias> bash aliases for each of your git aliases.

The main downside to this approach is that it will make your terminal take a little longer to load.

My aliases

Here are the aliases I use constantly in my workflow. I'm lazy about remembering many other aliases that I've decided I should be using, which this setup is great for because I can always list them all using gla.

This comment has been minimized.

This one removes all the branches that have been merged to the current branch that you are in.
If you use this from master then all branches merged to master are deleted. This reading git branch easier.clean-merged = !git branch --merged | grep -v \"\\*\" | xargs -n 1 git branch -d

This one removes all the branches that have been merged to the current branch that you are in.
If you use this from master then all branches merged to master are deleted. This reading git branch easier.clean-merged = !git branch --merged | grep -v \"\\*\" | xargs -n 1 git branch -d

This comment has been minimized.

I´ve git an issue when add this to the .bashrc. I gotbash: __git_aliases: command not found
the problem is the __git_aliases command will be loaded after first use of git auto-completion - after first git <TAB><TAB>. So you will need to force include this command. On my system this will be in /usr/share/bash-completion/completions/git
Just add include this file to the .bashrc like this:

I´ve git an issue when add this to the .bashrc. I gotbash: __git_aliases: command not found
the problem is the __git_aliases command will be loaded after first use of git auto-completion - after first git <TAB><TAB>. So you will need to force include this command. On my system this will be in /usr/share/bash-completion/completions/git
Just add include this file to the .bashrc like this:

Interactive rebase onto the branch point between the current branch and master, no worries about conflicts for a merge onto a future master, you can clean up your branch history and then decide what to do with it after.

Interactive rebase onto the branch point between the current branch and master, no worries about conflicts for a merge onto a future master, you can clean up your branch history and then decide what to do with it after.

This comment has been minimized.

# branch delete: This checks out your local master branch and deletes all local branches# that have already been merged to master
brd = !sh -c \"git checkout master && git branch --merged | grep -v '\\*'| xargs -n 1 git branch -d\"# branch delete here: Deletes all local branches that have already been merged to the branch# that you're currently on
brdhere = !sh -c \"git branch --merged | grep -v '\\*'| xargs -n 1 git branch -d\"# diff status: A git diff, but with only the filenames (which reminds me of git status)
diffst = diff --name-only
# forced pull: You have a local branch (e.g. for reviewing), but someone else did a forced push# update on the remote branch. A regular git pull will fail, but this will just set# the local branch to match the remote branch. BEWARE: this will overwrite any local# commits you have made on this branch that haven't been pushed.
pullf = !sh -c \"git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)\"# quick amend: Amend my staged changes to the last commit, keeping the same commit message
amend = commit --amend -C HEAD
# history: This is pretty much the only way I look at my log. Aside from providing one-line logs,# it also shows the branching in/out
hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

# branch delete: This checks out your local master branch and deletes all local branches# that have already been merged to master
brd = !sh -c \"git checkout master && git branch --merged | grep -v '\\*'| xargs -n 1 git branch -d\"# branch delete here: Deletes all local branches that have already been merged to the branch# that you're currently on
brdhere = !sh -c \"git branch --merged | grep -v '\\*'| xargs -n 1 git branch -d\"# diff status: A git diff, but with only the filenames (which reminds me of git status)
diffst = diff --name-only
# forced pull: You have a local branch (e.g. for reviewing), but someone else did a forced push# update on the remote branch. A regular git pull will fail, but this will just set# the local branch to match the remote branch. BEWARE: this will overwrite any local# commits you have made on this branch that haven't been pushed.
pullf = !sh -c \"git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)\"# quick amend: Amend my staged changes to the last commit, keeping the same commit message
amend = commit --amend -C HEAD
# history: This is pretty much the only way I look at my log. Aside from providing one-line logs,# it also shows the branching in/out
hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short