Skip to content

Instantly share code, notes, and snippets.

@amedee
Created June 4, 2024 23:34
Show Gist options
  • Save amedee/da98791652474fc232bd14c03832f897 to your computer and use it in GitHub Desktop.
Save amedee/da98791652474fc232bd14c03832f897 to your computer and use it in GitHub Desktop.
Incredibly useful git commands

git-useful

a collection of incredibly useful git commands

Table of Contents

Add

add files interactively

git add <file> --patch

# working example
git add . --patch

Very useful when you need to commit different lines of a file. See more at the docs for Interactive Mode.

add all changes ignoring whitespace changes

git diff -w --no-color | git apply --cached --ignore-whitespace

That time when your editor removed all trailing spaces

source: http://stackoverflow.com/questions/3515597/git-add-only-non-whitespace-changes

Log

shows commit frequency for each user in the repo

git shortlog --numbered --summary --all --no-merges \
  | head

source: http://mislav.uniqpath.com/2014/02/hidden-documentation/

pretty log (one line with graphic and colors)

git log \
  --graph \
  --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

logs commits that added or removed a certain keyword

git log -S '<keyword>'

source: http://mislav.uniqpath.com/2014/02/hidden-documentation/

lists already-merged branches

git branch --merged [<branch>]

# working examples
git branch --merged master

# in the current HEAD
git branch --merged

source: http://stevenharman.net/git-clean-delete-already-merged-branches

list authors by commits

git shortlog --summary --numbered --no-merges <branch>

# working example
git shortlog --summary --numbered --no-merges master

source: http://codeinthehole.com/writing/command-line-tips-for-effective-release-announcements/

list commits by authors between revisions

git shortlog <rev1>..<rev2> --no-merges

# working example
git shortlog master..HEAD --no-merges

source: http://codeinthehole.com/writing/command-line-tips-for-effective-release-announcements/

list tags by date

git for-each-ref --sort=taggerdate --format '%(refname) %(taggerdate)' refs/tags

source: http://stackoverflow.com/questions/6269927/how-can-i-list-all-tags-in-my-git-repository-by-the-date-they-were-created

Diff

diff word-by-word

git diff --word-diff

source: http://idnotfound.wordpress.com/2009/05/09/word-by-word-diffs-in-git/

short infos about changes in a commit

git diff-tree --no-commit-id --shortstat -r <commit-hash>

# working example
git diff-tree --no-commit-id --shortstat -r HEAD

show changed files in a commit

git diff-tree --no-commit-id --name-only -r <commit-hash>

# working example
git diff-tree --no-commit-id --name-only -r HEAD

If you want a more detailed version, run with the --stat, or --numstat or --dirstat flags instead of --name-only.

source: http://stackoverflow.com/questions/424071/list-all-the-files-for-a-commit-in-git

list every changed file between two commits

git diff --name-only <commit-hash> <commit-hash>

# working example
git diff --name-only HEAD~3 HEAD

source: http://stackoverflow.com/questions/1552340/git-show-all-changed-files-between-two-commits

show modifications in a file in a commit

git diff <commit-hash>~1..<commit-hash> [<file>]

# working example
git diff HEAD~1..HEAD

You can optionally add a file name to show only changes in a specific file.

show files with conflicts

git diff --name-only --diff-filter=U

source: http://stackoverflow.com/questions/3065650/whats-the-simplest-way-to-git-a-list-of-conflicted-files

Branch

deletes already-merged branches

git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

source: http://stevenharman.net/git-clean-delete-already-merged-branches

add new branch and switches to this branch

git checkout -b <branch>

# working example
git checkout -b new_branch

source: http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

Misc

sync fork

# Requires an "upstream" remote, pointing to original repo
# e.g. `git remote add upstream git@github.com:user/repo.git`
git fetch upstream; git checkout master; git rebase upstream/master

source: https://help.github.com/articles/syncing-a-fork

assume file as unchanged

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

# working example
git update-index --assume-unchanged .

undo assume file as unchanged

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

# working example
git update-index --no-assume-unchanged .

source: http://stackoverflow.com/questions/17195861/undo-a-git-update-index-assume-unchanged-file

list files assumed as unchanged

git ls-files -v|grep '^h'

source: http://stackoverflow.com/questions/17195861/undo-a-git-update-index-assume-unchanged-file

Clean

remove untracked files

# list files that would be removed
git clean -f -n

# remove untracked files
git clean -f

source: http://stackoverflow.com/questions/61212/how-do-i-remove-local-untracked-files-from-my-current-git-branch

Auditing

Others

More than one line command useful things

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment