DEV Community

Cover image for 5 Great Git CLI Shortcuts

5 Great Git CLI Shortcuts

Jeff Gould on September 26, 2019

I think we're all onboard with git these days - it's the best way to collaborate on code with a team or to just keep things organized, synchronized...
Collapse
 
willsmart profile image
willsmart

A nice one is

alias gdammit='git --amend --no-edit'

For those times you were just too quick on the trigger, and want to add some additional staged files to your last commit.

Feel free to use a different alias, like goops or gredo 🤨

Collapse
 
jrgould profile image
Jeff Gould

I've been meaning to add one like this - thinking gcrap or gdoh, or maybe just gc!

Collapse
 
zenphp profile image
Jason Murray

I use this to set git lg to be a cleaner overview of history:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"

Results in a nice tree view of history showing authors, hashes, titles and the tag/branch names.

Collapse
 
shostarsson profile image
Rémi Lavedrine

That's a good one.
I must say that I never goes beyond the aliases that are by default in zsh and oh_my_zsh.

Here is a quick extract of the aliases available in oh_my_zsh :

g=git
ga='git add'
gaa='git add --all'
gap='git apply'
...
glo='git log --oneline --decorate'
...
grmv='git remote rename'
groh='git reset origin/$(git_current_branch) --hard'
grrm='git remote remove'
grset='git remote set-url'
grt='cd "$(git rev-parse --show-toplevel || echo .)"'
gru='git reset --'
...
gupv='git pull --rebase -v'
gwch='git whatchanged -p --abbrev-commit --pretty=medium'
gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
Collapse
 
jrgould profile image
Jeff Gould

I wish that the aliases in oh_my_zsh were commented to tell you what they do in plain english. I will often look in there and try to add a new one to my arsenal, but that becomes a bit of a research project since I'm not fluent in git commands and typically need to look things up, at least just to be sure that the commands will do what I suspect.

Collapse
 
syntaxseed profile image
SyntaxSeed (Sherri W)

One of my fav aliases I always set up in git's own aliasing config is:

git ac

For git "all commit". Which is an alias for git add .; git commit -m.

To just add & commit everything in one go.

Collapse
 
jkkgbe profile image
Jakub

git add . is 'all commit' only if you have changes on the current path. If you have changes in e.g. sibling dir it won't work as you'd have to go up the path which is ... For a true 'all commit' use git add -A.

Collapse
 
syntaxseed profile image
SyntaxSeed (Sherri W) • Edited

Thanks!
I do always run this from project root so I'm ok. But I think I'll switch to your suggestion! :)

Collapse
 
jrgould profile image
Jeff Gould

I like that - I have gc aliased to git commit so I'll usually do gc -am "commit message" but that does have the the drawback of not adding new files and I forget that 90% of the time and then have to amend the commit which is a whole thing.

Collapse
 
canro91 profile image
Cesar Aguirre

Today I came across to this one:

alias gconf='git diff --name-only --diff-filter=U'

To list the conficted files after a merge

Collapse
 
rochacbruno profile image
Bruno Rocha

I use hub and I alias git=hub

then I have

git sync, git pr list, git pull-request create, git release etc...

Collapse
 
jrgould profile image
Jeff Gould

I hadn't heard of hub, will have to check it out.

hub: A command-line tool that makes git easier to use with GitHub

Collapse
 
zivni profile image
Ziv

Here are the aliases I use gist.github.com/zivni/90a6a93fec67...

Collapse
 
v_schroeder profile image
Vincent Schröder

Just created this little handy alias for interactive rebase

alias gitrebase='git checkout master && git pull && git checkout - && git rebase -i HEAD~$(git rev-list --count HEAD ^master)'