DEV Community

Cover image for 5 BEST Git Commands
Jake Roggenbuck
Jake Roggenbuck

Posted on

5 BEST Git Commands

These are the top 5 most useful git commands that I use almost every day. Out of the 30+ git command aliases I have, these are the for sure the best 5! Please let me know in the comments which ones you like the best!

If you have a favorite command of your own, please share in the comments! I'll highlight my favorite ones!

1. Git List (GLS)

Git List Command

View a super short summary of the most recent commits. Instead of filling the page with just a few commits, view each commit in a single line.

git log --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

I have this aliased to gls with

alias gls='git log --pretty=oneline --abbrev-commit'
Enter fullscreen mode Exit fullscreen mode

2. Push Origin (PUSHO)

Push to the current default branch. This one is simple, but I use this one the most.

git push origin $(git symbolic-ref --short HEAD)
Enter fullscreen mode Exit fullscreen mode

I have this aliases to pusho in my .bashrc with

alias pusho='git push origin $(git symbolic-ref --short HEAD)'
Enter fullscreen mode Exit fullscreen mode

3. Git logg (LOGG)

Git Logg Command

View an ascii art history of all of the branches.

git log --graph --decorate --all
Enter fullscreen mode Exit fullscreen mode

I have this aliased to logg with

alias logg='git log --graph --decorate --all'
Enter fullscreen mode Exit fullscreen mode

4. Git Diff Precise (GDP)

Have you ever been looking at a git diff and it just tells you the whole line is different? Well, this command tells you exactly what characters have changed. Extremely helpful for catching issues in review or remembering exactly what you changed in a large line.

Before

Git diff before

After

Git diff after

This is a simple example and it's easy to see what has changed, but if you had a really complicated line with a few different changes in different places, it can be hard to catch. Say an SQL command or similar.

Here is how to do it!

git diff --word-diff=color --word-diff-regex=.
Enter fullscreen mode Exit fullscreen mode

I have this aliased to gdp with

alias gdp='git diff --word-diff=color --word-diff-regex=.'
Enter fullscreen mode Exit fullscreen mode

5. The MOST useful of all (lookz)

Have you ever tried looking through a whole git history to try to find where you added or removed something? Has a line gone missing or a type changed that has caused an issue in production that you need to fix ASAP! Looking through each commit for a single line may take hours with a big repository. This allows you to search through every commit and use FZF to do it.

This does require you to have fzf installed, but I recommend having that anyway. It's super useful for this type of thing.

git remote && git log --pretty=oneline --abbrev-commit \
| awk '{print $1}' \
| xargs -I {} git show {} | fzf
Enter fullscreen mode Exit fullscreen mode

I have this aliased to lookz with

alias lookz='git remote && git log --pretty=oneline --abbrev-commit | awk '"'"'{print $1}'"'"' | xargs -I {} git show {} | fzf'
Enter fullscreen mode Exit fullscreen mode

Note the single quotes having to be escaped with '"'"' in the bash alias version but not the one directly for the shell.

I need help making this command more useful. It lets you find a specific name of something that can then be used to search, but once it's selected, you can't find the commit. If anyone knows how to do this, please leave a comment!

Top comments (5)

Collapse
 
waterkip profile image
Wesley Schwengle • Edited

I have git poh for pushing HEAD to my origin, which is an alias for... git push origin HEAD, you don't need to do magic to resolve HEAD. Git does that for you, equally nice: git po is git push origin and git po HEAD:foo pushes the current branch to the remote foo branch.

My favorite (aliases) are:

cib = branch --all --contains
cit = tag --contains

# git l == git log --use-mailmap, this is now the default, but not when i started out
ll = l --stat
lp = l -p
sl = l --format='%C(yellow)%h%Creset %aI %Cgreen%s%Creset %aE'
# Which I'm now testing out with a small change:
sl = l --format='%C(yellow)%h%Creset %<(25)%ai %<(10,trunc)%aN 
%Cgreen%s%Creset'
lb = sl @{u}...HEAD
Enter fullscreen mode Exit fullscreen mode
Collapse
 
georgeberdovskiy profile image
George Berdovskiy

I really like the GDP alias, will definitely be using that one myself.

Collapse
 
sc0v0ne profile image
sc0v0ne

Amazing post !!! I liked new commands.

Collapse
 
valvonvorn profile image
val von vorn • Edited

What does BEST acronym actually stand for?

  1. The MOST useful of all (lookz)

and lookz didn't not knew yet either 🤔 🤔?

Collapse
 
robert_mast_dcf48ed741093 profile image
Robert Mast

Isn't lookz just another git blame?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.