Cheat Sheet - Git Commands

Created: 2020-08-13 | Updated: 2022-01-23 | 3 min read


My Cheat Sheet for: Git Commands.

Gathering quick references I used to search over and over again. Good to have them handy and supposed to (slowly) grow.

#git log

Git log with some styling (colors and specific order I like to see)

1$ git log --pretty=format:"%C(Green)%h %C(Red)[.%D.] %C(Blue)[%an] %C(Yellow)[%s] %C(Magenta)[%ad (%ar)]"

Git log, raw info, tabular style

1$ git log --pretty=format:"%h | %ad | %><(23,trunc)%ar | %><(15,trunc)%an | %<(72,trunc)%s | %D" --date=format:"%a %Y-%m-%d %H:%M:%S"

#git diff

Get number of files changed, lines inserted and lines removed between two branches or commits

1$ git diff --shortstat master..develop
2 13 files changed, 90 insertions(+), 410 deletions(-)
3
4$ git diff --shortstat 499a153..8059ee2
5 3 files changed, 69 insertions(+), 1 deletion(-)

Get diff between two branches or commits with only relevant information (not the whole diff)

 1# File names only
 2$ git diff --name-only origin/main..develop
 3blog/intro/index.mdx
 4generator/MarkdownRenderer.py
 5resources/404.html
 6resources/about.html
 7resources/index.html
 8resources/layout.html
 9resources/notes.html
10resources/post.html
11resources/styles.css
12
13# File names with status (modified, deleted, added)
14$ git diff --name-status origin/main..develop
15M       blog/intro/index.mdx
16M       generator/MarkdownRenderer.py
17M       resources/404.html
18M       resources/about.html
19M       resources/index.html
20M       resources/layout.html
21M       resources/notes.html
22M       resources/post.html
23M       resources/styles.css
24
25# With stats
26$ git diff --stat origin/main..develop
27 blog/intro/index.mdx          | 19 +++++++++----------
28 generator/MarkdownRenderer.py |  9 +++------
29 resources/404.html            |  2 +-
30 resources/about.html          |  2 +-
31 resources/index.html          |  2 +-
32 resources/layout.html         | 12 +++++-------
33 resources/notes.html          |  2 +-
34 resources/post.html           |  3 +--
35 resources/styles.css          | 36 ++++++++++--------------------------
36 9 files changed, 32 insertions(+), 55 deletions(-)

#git stash

Stash your changes quickly

1$ git stash

Stash your changes with a custom message:

1$ git stash push -m "your message here"

See your stash entries

1$ git stash list
2stash@{0}: On main: your message here

See the diff of a given stash entry

1$ git stash show -p stash@{0}
2diff --git a/blog/cheat-sheet-git-commands/index.mdx b/blog/cheat-sheet-git-commands/index.mdx
3index 0a038ac..6134c05 100644
4--- a/blog/cheat-sheet-git-commands/index.mdx
5+++ b/blog/cheat-sheet-git-commands/index.mdx
6@@ -6,12 +6,16 @@ slug: cheat-sheet-git-commands
7:
8(more)

Save stash diff on disk

1$ git stash show -p stash@{0} > ~/your_diff.patch

Apply stash to your current branch. Note: The stash will be removed from the stash store

1$ git stash pop stash@{0}

Clean your stash entries

1# Only one entry
2$ git stash drop stash@{2}
3# Delete all your stash entries
4$ git stash clear

#git reset

Unstage changes made on certain files. (Changes can be staged again with git add command)

1git reset file1.py file2.py

#GitWeb

To launch git web view

1git instaweb --httpd=webrick

And stop with

1git instaweb --httpd=webrick --stop

In browser: http://localhost:1234

Ref: https://git-scm.com/book/en/v2/Git-on-the-Server-GitWeb

#Misc

Stage all changed files even the ones living in parent directories of a git repo

1git add -A
2# or...
3git add --all

See other cheatsheets here