Skip to content

Instantly share code, notes, and snippets.

@ouyi
Last active Dec 27, 2017
Embed
What would you like to do?
Frequently used Git commands
# Update commit comments before the push
git commit -m "Fix an issue"
git commit -m "Fix an important issue" --amend
# Throw away all changes on local master and to have it exactly the same as origin/master:
git checkout master
git reset --hard origin/master
# Force origin/master to be the same as local master (dangerous!!!):
git push -f origin master:master
# Clean up already merged local branches
git branch --merged | grep -v master | while read b; do echo $b; git branch -d "$b" ; done
# Get hash code of the empty tree
# Useful when a "complete" patch file is required, e.g., for a completely new project on the review board
git hash-object -t tree --stdin < /dev/null
# 4b825dc642cb6eb9a060e54bf8d69288fbee4904
# git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 <another commit hash>
# File-level overview about the changes
git diff --summary hash1 hash2
git diff --name-status hash1 hash2
# Export Git project without history
git archive --format zip --output /tmp/project_awesome.zip hash1
# Show all changes a branch would bring onto the master (a diff in the pull request style)
git diff origin/master...origin/feature_awesome
# Commands for producing a Git patch of a merge commit and using the patch to revert the commit
git diff origin/master...origin/feature_awesome > changes.patch
# Use the patch for a rollback
git apply -R changes.patch
# See https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.html
# A merge commit can not be reverted "completely" -- only the content changes can be reverted,
# not the merge history, i.e., the fact that a merge commit has two paraents can not be reverted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment