Ask HN: What are some Git aliases you setup that save you time?

29 points by CuriousGeorgeQA ↗ HN
For me it's nothing fancy. Just "co", "st", and "br" for "checkout", "status" and "branch".

34 comments

[ 1.8 ms ] story [ 186 ms ] thread
I'd always make one for my work dev folder so I could quickly switch to it as soon as I restarted my terminal. I'd always make it just a 2 letter abbreviation. For example, my last one was an alias "s6" to take me the the correct dev folder.
Check out zsh-z, autojump (or any derivative of these)
I alias git status --untracked=no so that i can easily see what a gunked up thats already on the repo.
For anyone new to aliases, you can add them with:

  alias my_alias="command"
Add it to your ~/.bash_profile or ~/.zprofile so it's ready whenever you are. Check out this page for more info: https://linuxize.com/post/how-to-create-bash-aliases/
Okay, but this is about git aliases. Quite similar.

    [alias]
      br = branch
      co = checkout
      d = diff
      dc = diff --cached
      ec = config --global -e
      latest = log -1 HEAD
      please = push --force-with-lease
      st = status
Aliases for git can be added to ~/.gitconfig
Nothing fancy either

  alias.co checkout
  alias.br branch
  alias.ci commit
  alias.st status
  alias.cm commit -m
  alias.unstage reset HEAD --
  alias.cob checkout -b
  alias.pp pull --prune
ga - add

gc - commit

gc! - amend

gcm - checkout master

gco - checkout

gcb - checkout new branch

Rebase:

grb

grbc - continue

grba - abort

Cherry pick:

gcp

gcpc - continue

gcpa - abort

These all are included in zsh git aliases but listed these I'm using on regular basis.

I also have a function that checks out master, pulls it, runs DB migrations and so on.

For longer ones like `g reset --soft HEAD~1` where I cba setting up alias, I just ctrl+r and fetch them with fzf or zsh autosuggestions - probably same amount of keystrokes as typing out alias

I don't use aliases, and I don't feel line I am losing time, like at all. Well I need to touch my hardware key when I push, which takes half a second...
I actually have them documented here and the aliases file is autogenerated whenever I add or update new ones :)

https://jdsalaro.com/note/wrist-friendly-git-shortcuts

I decided against git aliases in favour of shell aliases, but people might still find them interesting.

For me, and my wrists, gs and gf have been game-changing.

Emacs' Magit is an excellent interface for git.

e.g. committing the staged changes would involve just "cc". Rewording the last commit is "cw", amending is "ce", and fixing up earlier commits is "cf".

Checking out a new branch is "bc", whereas creating a new branch is "bn".

Fetching to the default origin is "ff", likewise pushing is "pp".

But aside from the keymap being very terse, it's also highly discoverable. You're shown all the git commands, and before invoking a command, all the options for that command.

    # Prettier logs:
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative
    ll = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative -20
    l1 = log --pretty=oneline --abbrev-commit --decorate

    # Show (all) (cached) diffs in Beyond Compare:
    da = !sh diffall.sh
    dac = !sh diffall.sh --cached
    dt = difftool
    dtc = difftool --cached

    # Short status:
    st = status -sb
    ss = "!showci () { rev=${1:-HEAD}; git da $rev~1 $rev; }; showci $1"
I have a similar git lg. It’s my most used command

    [alias]
        justdoit = commit --amend --no-edit
I feel like every other commit I forget something, so a `git j<tab>` (I don't have any other aliases that what with j) puts my last minute fix where it belongs. Also nice during interactive rebases.
I use this a lot but I have a version with --no-verify to skip hooks too.
Yoy dirty, I hope Mr Gandalf pipeline, say the words!
You shall not pass! But I'm a demon and I'm taking my commit with me to the depths of hell
Haha I'm just kidding, I also have similar alias on my toolbelt too, it's for emergencies purposes, I swear!
resync = git checkout master && git pull && git checkout - && git pull --rebase master

asquash = git rebase -i master --autosquash

abs = git absorb -b HEAD~20

everything else is handled by vim-fugitive

not an alias, but:

git checkout -

returns you to the previous branch

(same for cd, "cd -" returns you to previous dir)

The legendary g command to show git traffic:

[alias] g = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

(comment deleted)
I have a bunch, but one of my favorite is a fish shell function that writes wip commits:

```

function w

    #set --local staged (gs | cut -c1 | ag -v "\?" | string collect | string trim)
    set --local staged (git status -s | grep "^[MADRCU]" | string collect | string trim)
    if test -n "$staged"
        #echo "something staged"
    else
        #echo "nothing staged"
        git add .
    end

    if not string length -q -- "$argv"
        gc -m 'WIP' -n
    else
        gc -m "$argv" -n
    end
end

```

Lifesaver and timesaver.

I have one called `b` that creates a new branch and commits.

One called `poop` that pushes the current branch to GitHub and create a PR.

etc etc

I commented elsewhere, but I also have

    [alias]
        unstash = stash pop
because I was used to perforce nomenclature. It's also a quick autocomplete since it's my only u... command.
I have a similar one:

unstage = git restore --staged

Here are a couple that I use a lot. There's probably a shorter way to write them:

  # show the last 10 branches I used
  lb = !git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | head -n 10 | awk -F' ~ HEAD@{' '{printf(\"  \\033[33m%s: \\033[37m %s\\033[0m\\n\", substr($2, 1, length($2)-1), $1)}'

  # checkout the previous branch I was using
  cop = !git checkout --recurse-submodules $(git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | head -n 10 | grep -v $(git branch --show-current) | head -1 | cut -d' ' -f1)
Unless I’m misunderstanding you can checkout your previous branch with

  git checkout -
(The same way you can use ‘cd -‘ to change to your previous directory)
Thank you I didn't know about this. Your version is a bit more compact.