Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
git-useful

git-useful

a collection of useful commands to use Git and GitHub.

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.

Log

shows commit frequency for each user in the repo

git log --format='%an' . | \
    sort | uniq -c | sort -rn | 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/

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

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 merge 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

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