DEV Community

Cover image for My Git Aliases
Nick Taylor
Nick Taylor Subscriber

Posted on • Originally published at iamdeveloper.com on

My Git Aliases

Alright, so @philnash roped me into this one.

This post’s birth comes from a gist which is essentially a copy paste of my git aliases.

I’m going to provide my list of git aliases and explain what each alias does, plain and simple. Let’s get started! 🏁 For those new to git aliases, please see the defacto docs on aliases. In a nutshell though, to create your own aliases, use the following git command.

git config --global alias.somealias some-git-command
Enter fullscreen mode Exit fullscreen mode

Before we get started, why git aliases? Well for one thing, I don’t know about you, but some git commands are hard to remember and also, we’re programmers, which means we’re lazy by default to be efficient. 🐢 —> 🐇

  • a = add . — Running git add will add all files that have changed as staged.
  • b = branch — Lists all branches for your repository on your local machine.
  • bi = bisect — Running git bi will run git’s bisect to help you figure out which commit has a bug.
  • ci = commit -m — This will commit a file with the message you specify, e.g. git ci "awesome commit!".
  • co = checkout — This will checkout the branch you specify, e.g. git co my-awesome-branch
  • colast = checkout - — Running git colast will checkout the previous branch you were working in.
  • db = branch -D — This will delete the branch you specify, e.g. git db my-not-so-awesome-branch. Note that this will only work if the branch you’re deleting is not the one you’re currently working in.
  • laf = fsck --lost-found — Running git laf will bring you to git’s lost and found. I’ll admit that I rarely use this, so perhaps it doesn’t warrant an alias and just some professional Googling.
  • last = log -1 HEAD — Running git last will show you what your last commit was.
  • lc = diff HEAD^ HEAD - Compares the head of your branch to the previous commit.
  • pf = push --force-with-lease — Running git pf forces a push, but it is a little less destructive than forcing a push. See here for more info on —force-with-lease vs. —force.
  • psu = push --set-upstream — Run this when you want to push a branch for the first time to the remote (typically origin), e.g. git psu origin my-awesome-branch.
  • pr = pull --rebase — This will rebase your current branch with the branch specified, e.g. git pr develop.
  • ra = rebase --abort — Running git ra will abort a rebase. Run this when you’re like, my rebase is currently messed up. Get me outta here!
  • rc = rebase --continue — Running git rc will continue a rebase. You typically run this when you’ve handled any conflicts in a rebase.
  • remotes = remote -v — Running git remotes shows all the remotes currently configured for a repository.
  • renb = branch -m — When you want to rename a branch, run e.g. git renb my-awesom-branch my-awesome-branch.
  • rhh = reset --hard HEAD — The nuclear option. Run git rhh to wipe out all your changes and start from the HEAD.
  • rh = reset --hard — When you specify what to reset to, a hard reset is performed, e.g. git rh HEAD~2.
  • sfc = diff-tree --no-commit-id --name-only -r — Shows files (relative file paths) for a specific commit, e.g.
❯ git sfc HEAD                                
src/posts/any-contribution-to-open-source-is-valuable-57d3.md
src/posts/april-16th-2021-what-did-you-learn-this-week-3e72.md
src/posts/are-there-plans-for-reviewers-of-articles-we-post--42nf.md
Enter fullscreen mode Exit fullscreen mode
  • s = status -s — Running git s will give you a more terse status. Instead of this
On branch post/my-git-aliases
Your branch is up to date with 'origin/post/my-git-aliases'.

Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)

       modified: src/pages/articles/2018-08-24-my-git-aliases/index.md

no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode

You get this

M src/pages/articles/2018-08-24-my-git-aliases/index.md
Enter fullscreen mode Exit fullscreen mode
  • stashes = stash list — Running git stashes shows you all the stashes you have from stashing. e.g.
stash@{0}: WIP on upgrade: bff6257 Destructuring OCD...
stash@{1}: WIP on upgrade: 3d73199 Fixed LiceCap link.
stash@{2}: WIP on upgrade: c2f78g6 Update default title.
Enter fullscreen mode Exit fullscreen mode
  • unstash = stash pop — Running git unstash pops a stash off the list of saved stashes.
  • vc = clean -dfx — Running git vc cleans your git repository, so anything not in git is wiped, e.g. node_modules, settings files which aren’t supposed to be in a repo etc. So BEWARE before you run this.
  • mend = commit --amend — Running git mend lets you amend a commit.
  • trigger = commit --allow-empty -m "Trigger Build" — Creates an empty commit. This is handy when you need to restart a build remotely in your CI/CD pipeline without committing changes.
  • alias = ! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ / — Running git aliases will show all the aliases you have configured globally in git.

Although it's not Git aliases, I also highly recommend using the GitHub CLI.

Photo courtesy of Flickr user cindy

Top comments (28)

Collapse
 
rhymes profile image
rhymes • Edited

Nice list you have.

This is mine:

➜  ~ git alias
alias = config --get-regexp "alias.*"
append = commit --amend --no-edit
cached = diff --cached
ci = commit
co = checkout
find-change = name-rev
find-change-branch = branch --contains
hist = log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
last = log -1 HEAD
log = log --full-history
lol = log --all --oneline --graph --decorate
ls = ls-files
merge = merge --no-ff
new = ls-files --others --exclude-standard
out = cherry -v
prune-remotes = remote prune origin
pull = pull --rebase
quick-stats = ! /usr/local/bin/git-quick-stats
release-tag = !git tag release-`date -u "+%Y%m%d%H%M"`
st = status -sb
standup = standup -d 8 -D iso
today = log --stat --since='1 Day Ago' --graph --pretty=oneline --abbrev-commit --date=relative
undo = reset HEAD^
unstage = reset HEAD --
who = shortlog -s
Enter fullscreen mode Exit fullscreen mode

I don't use bisect or rebase that much so I don't have shortcuts. I use git out a lot to see what's going up to the origin. I've aliased merge to merge --no-ff because that's what you want 99% of the time. I probably have too many aliases of log :D

Collapse
 
nickytonline profile image
Nick Taylor

Thanks for sharing @rhymes !

Collapse
 
itsasine profile image
ItsASine (Kayla)

I'm a little disappointed git out isn't something like sudo reboot

Collapse
 
rhymes profile image
rhymes • Edited

AHAHHAAHAH thanks for the laughter.

I should probably start aliasing it on colleagues's computers :D

Collapse
 
yucer profile image
yucer • Edited

I usually make aliases only for those that have specific parameters or are compound of more than one command.

In the other cases, I use the shell auto-completion with the TAB key.

Also, for those like this:

alias.s status -s

If you do that very frequent i think it's better a shell alias (gs) than a git alias (git s)

Collapse
 
nickytonline profile image
Nick Taylor

I do have shell aliases for some things, like grabbing a PR locally (fish shell alias)

function copr
        git fetch origin pull/$argv/head:pr$argv; and git co pr$argv;
end

At the end of the day, everyone has their own setups, which is normal. All that matters is that you set things up the way you like that makes you more productive.

Collapse
 
nickytonline profile image
Nick Taylor

I also don't mention it in the post but I have a shell alias for git which is just g, so I'd do g s

Collapse
 
vlasales profile image
Vlastimil Pospichal

gs has an unpleasant collision with GhostScript.

Collapse
 
philnash profile image
Phil Nash

Thanks for the post Nick! There's such a lot in here and I learned about a couple of commands too. The lost and found search is interesting, not a very common use as you said, but something worth remembering just in case.

I just checked my aliases and I was almost disappointed with how few I have!

  st = status
  co = checkout

I do have this function in my .bash_profile though. It allows you to use g in place of git and if you pass no other arguments runs git status. I find it pretty useful.

function g {
 if [[ $# > 0 ]]
 then
     git $@
 else
     git status
 fi
}
Collapse
 
nickytonline profile image
Nick Taylor

Glad you like it. Thanks for suggesting I write it up. I alias git to g as well, but I like how you default to showing status of no git command is passed in. 🔥

Collapse
 
itsdarrylnorris profile image
Darryl Norris

This is my list:

# Git Aliases
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias gcm='git commit -m'
Enter fullscreen mode Exit fullscreen mode

Very simple

Collapse
 
thomasjunkos profile image
Thomas Junkツ

You copied mine XD

Collapse
 
itsdarrylnorris profile image
Darryl Norris

I did copy from somewhere in StackOver flow many many years ago. I am not sure if was from you or not :P.

Collapse
 
bgadrian profile image
Adrian B.G.

I removed my own aliases and went to a more popular list, namely I adopted bash-it framework.

Collapse
 
nickytonline profile image
Nick Taylor

If it makes you more productive going with existing ones, then 💯.

Collapse
 
hoelzro profile image
Rob Hoelz

Out of curiosity, what was the motivation to give up your own aliases in favor of an alias framework?

Collapse
 
bgadrian profile image
Adrian B.G.
  • Reducing the cognitive load mainly (on me, staying up to date, thinking of new aliases, and so on),

  • the fact that more devs are usually better than 1 (bashit being open source),

  • easier to install and

  • increasing the chance that my aliases are on other PCs.

And I didn't saw any disadvantage.

Thread Thread
 
hoelzro profile image
Rob Hoelz

Thanks for answering! I really appreciated the insight, since it's different from how I work, and I like trying to reevaluate my own usage habits from time to time.

Thread Thread
 
bgadrian profile image
Adrian B.G.

Change is always hard, I used those aliases for many years, but it was time to move on once I started to work from multiple workstations and environments (including Cloud9) I had to simplify all my "custom won bubble".

Collapse
 
nickytonline profile image
Nick Taylor

Adding a late one to the list git config --global alias.mend "commit --amend". Calling it mend seems more natural to me and since I alias git to g in my shell, I'm gonna be all about the g mend.

Collapse
 
der_gopher profile image
Alex Pliutau

One thing I don't like about Git aliases is that when you are on a new environment (coworker laptop, remote server, etc.) they won't work. So it's better to remember original commands.

Collapse
 
hoelzro profile image
Rob Hoelz

My personal take on this is that for many people, you're on a small set of machines 95% of the time, so it's worth it to optimize your setup on that set of machines.

However, I know that my experience isn't universal - if most of the time you're logging into a large number of machines or machines that are ephemeral in nature, my take doesn't really apply!

Collapse
 
nickytonline profile image
Nick Taylor

For my day to day this is generally not an issue, but if I were on a co-worker's machine or a remote server and for some reason I couldn't remember a git command, I'd use the Google 😉

Collapse
 
thomasjunkos profile image
Thomas Junkツ

I love those two to three letter aliases. Life's to short for typing ;)
But I use aliases in my .zshrc where all my aliases live :]

Collapse
 
wallybh profile image
Wallison Santos

I loved all your aliases, except those that commit a generic commit message. Thanks for share.

Collapse
 
nickytonline profile image
Nick Taylor • Edited

Yeah, I don't use those ones too often. I'm considering removing them.

Collapse
 
eljayadobe profile image
Eljay-Adobe

Very timely! I've got to learn git in the near future this year, and the one thing I'm sure I'll be using right away is aliases. Muchos gracias! Спасибо!