HACKER Q&A
📣 CuriousGeorgeQA

What are some Git aliases you setup that save you time?


For me it's nothing fancy. Just "co", "st", and "br" for "checkout", "status" and "branch".


  👤 rgoulter Accepted Answer ✓
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.


👤 jdsalaro
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.


👤 fantapop
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)

👤 ConorSheehan1
`git standup` to dump my commits from yesterday, or Friday if today is Monday. https://gist.github.com/ConorSheehan1/f6da062b536c633622e1aa...

👤 hboon
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


👤 Leftium

    # 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"

👤 pawelduda
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


👤 mrdude42
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.

👤 gitgud
[1] github-branch-open-pr - Open current branch as new PR in browser

[2] git-push-set-upstream - Push current branch to remote

And plenty more…

[1] https://github.com/benwinding/dotfiles/blob/master/bin/githu...

[2] https://github.com/benwinding/dotfiles/blob/2239e56df2a49818...


👤 palata
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...

👤 selljamhere
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/

👤 hwc
I have a script checked into a git repo so I can more easily sync between machines.

https://gitlab.com/halcanary/config/-/blob/master/git_config...


👤 jmholla

    [alias]
        justdoit = commit --amend --no-edit
I feel like every other commit I forget something, so a `git j` (I don't have any other aliases that what with j) puts my last minute fix where it belongs. Also nice during interactive rebases.

👤 hsfzxjy
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


👤 jmholla
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.

👤 lizardwalk5
used this post from Jonathan Suh, it's fairly comprehensive. I think I use most except several that I'm worried I might accidentally type in (in a hurry or whatnot) and do something too destructive: https://jonsuh.com/blog/git-command-line-shortcuts/

👤 jpmoral
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

👤 polalavik
I alias git status --untracked=no so that i can easily see what a gunked up thats already on the repo.

👤 drekipus
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


👤 blibble
not an alias, but:

git checkout -

returns you to the previous branch

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