DEV Community

Cover image for git command-line tricks
Remi Kristelijn
Remi Kristelijn

Posted on

git command-line tricks

Personally I prefer command-line over tools. This has a few reasons;

  1. The command-line is always available;
  2. If you can do it by commandline, you can do it everywhere
  3. Using the command-line makes me look geeky

The reason for this post is that I grew tired of typing git log --one-line -10 that shows the last 10 commits and also having to type q afterwards, where the information that I needed goes away, because I want to create a commit message that is in line with the rest of the commits.

So I decided to look into making it more pleasant for me to quickly get some history information from git.

The first thing I learned was that most git commands end up in a so called 'pager';

git log --oneline -10

e3aa791 (HEAD -> master) notes
81affbd docs: translated installation guide and moved errors to bottom
9a592cd docs: nearly there
aaa98fe docs: add stuff from the thing
c522b66 docs: generic docs
e09ee2b chore(Doc) sort
8740d11 chore(doc): add new stuff
bd8e74c notes after meeting
0de07df chore(Base): initial checkin
(END)

I discovered that I can bypass the pager so that git uses cat by adding --no-pager as first argument. It only works as a first argument.

--no-pager

git --no-pager log --oneline -10

➜  notes git:(master) git --no-pager log --oneline -10
e3aa791 (HEAD -> master) notes
81affbd docs: translated installation guide and moved errors to bottom
9a592cd docs: nearly there
aaa98fe docs: add kostenscherm
c522b66 docs: generic docs
e09ee2b chore(Doc) sort
8740d11 chore(doc): add new stuff
bd8e74c notes after meeting
0de07df chore(Base): initial checkin
➜  notes git:(master) 

notice how I get my shell back and the information is still on my screen. Now I could write a git commit message in line with the rest.

So this solves part of my problem. I don't want to type the whole command as above over and over again. It is nice to remember it when you are working on someone else's machine, but in this case I want to 'save' it.

The good news is; you can. per command, you can toggle whether it should use pager.

In this case you can type:

git config --global pager.log false

➜  notes git:(master) git config --global pager.log false
➜  notes git:(master) git log -1
commit e3aa791e0e970023734988c8aeb736ecda6099f4 (HEAD -> master)
Author: Remi Kristelijn <remi.kristelijn@myawesomecompany.com>
Date:   Fri Nov 29 15:51:02 2019 +0100

    notes
➜  notes git:(master) 

These settings are stored in ~/.gitconfig

Also I know with combination of --pretty and --graph you can decorate you log message and save this as an alias.

Adding an alias

The command I use is log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -10

➜  git:(master) git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -10
*   d2a1b98 - (HEAD -> master, origin/master) Merge branch 'feat/wizard-steps' into 'master' (3 weeks ago) <Remi Kristelijn>
|\  
| * 20b7b2e - (origin/feat/wizard-steps, feat/wizard-steps) feat:step 2 of wizard (3 weeks ago) <Remi Kristelijn>
| * 077f5e1 - feat: wizard placeholder (3 weeks ago) <Remi Kristelijn>
| * e2f4c29 - feat: add readme.md as an example (3 weeks ago) <Remi Kristelijn>
| * debbfcd - fix: hyperlink goes outside of context (3 weeks ago) <Remi Kristelijn>
| * af11d21 - feat:allow *.md imports for Typescript (3 weeks ago) <Remi Kristelijn>
|/  
*   cb562e3 - Merge branch 'feat/style' into 'master' (3 weeks ago) <Remi Kristelijn>
|\  
| *   51df26c - (origin/feat/style, feat/style) fix:merge (3 weeks ago) <Remi Kristelijn>
| |\  
| |/  
|/|   
* |   3583c92 - Merge branch 'feat/docs' into 'master' (3 weeks ago) <Remi Kristelijn>
|\ \  
| * | ba0f827 - (origin/feat/docs) docs:add loglevel (3 weeks ago) <Remi Kristelijn>%  

To save this command as an alias, let's say lg

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

Then you can flag this command to also not use 'pager'

git config --global pager.lg false

But because log is already flagged false for pager, it will cary that through when using lg.

Top comments (6)

Collapse
 
franjozen profile image
franjozen

I'd like to suggest that you look into the less parameters -X -F -R discussed in this SO question: superuser.com/questions/106637/les... If the result fits in one screen, less closes, otherwise not. R is for colors. You can add this to the global variable LESS to enable it globally, or to shell or git aliases if you find the first alternative too intrusive.

Collapse
 
victoryarema profile image
Victor Yarema

Regarding the problem you were trying to solve with parameters, configuration, aliases, ... I usually solve any problem like this just by using "tmux". I highly recommend it.

Collapse
 
rkristelijn profile image
Remi Kristelijn

Thanks Victor, I will have a look at tmux

Collapse
 
fekerr profile image
Fred Kerr

I use putty and tmux is my friend.

Collapse
 
victoryarema profile image
Victor Yarema

There are few more advantages for those who use command line tools:

  • Easier to troubleshoot if something goes wrong and tool (usually) outputs useful stuff.
  • Can be part of a shell script that can do much more.
Collapse
 
maj profile image
Major Hoffman

This is pretty great - thanks for sharing