Skip to content

Instantly share code, notes, and snippets.

@Telematica
Last active September 19, 2024 00:36
Show Gist options
  • Save Telematica/5e0011612e7ce59774a2a1a9c02c6275 to your computer and use it in GitHub Desktop.
Save Telematica/5e0011612e7ce59774a2a1a9c02c6275 to your computer and use it in GitHub Desktop.
Git Recipes

Git Recipies for a good Git Cookbook (and misc tricks)

Git Log

git log --author=<pattern>

Search By Author (patter) and contains (pattern), case-insensitive

git log --author="linuxeron" --grep="fix" -i

The --stat option displays the number of insertions and deletions to each file altered by each commit (note that modifying a line is represented as 1 insertion and 1 deletion).

git log --stat

Git rm

git rm -r --cached .
git add .
git commit -m "fixed untracked files"

Git Graphic Log

git log --graph --abbrev-commit --decorate --all --format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)"

git config --global alias.grog 'log --graph --abbrev-commit --decorate --all --format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)"'

Git rebase

git fetch && git pull origin/develop --rebase --autostash

Git merge dry-run

git merge --no-commit --no-ff $BRANCH
git merge --abort

How would I extract a single file (or changes to a file) from a git stash?

git diff stash@{0}^1 stash@{0} -- <filename>
git diff stash@{0}^! -- <filename>

Git shortlog

git shortlog -s -n --all --no-merges
git shortlog -ns --no-merges v11..HEAD | head -n15
git shortlog -ns --no-merges n2.4..HEAD | head -n15

Git diff

git diff HEAD^^ HEAD main.c
git diff HEAD^^..HEAD -- main.c
git diff HEAD~2 HEAD -- main.c

Git misc

git update-index --assume-unchanged <file>

#Long Path Issue Windows OS
git config --system core.longpaths true

How to generate and apply patches with git?

1. Generate the patch:

git diff > some-changes.patch

2. Apply the diff:

Then copy this patch to your local machine, and apply it to your local working copy with:

git apply /path/to/some-changes.patch

Count Git commits per period

git rev-list --count --since="Dec 3 2015"  --before="Jan 3 2016" --all
git rev-list --count --since="Dec 3 2015"  --before="Jan 3 2016" --all --no-merges
git rev-list --count HEAD  --since=30.minute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment