Skip to content

Instantly share code, notes, and snippets.

@danawoodman
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danawoodman/10917488 to your computer and use it in GitHub Desktop.
Save danawoodman/10917488 to your computer and use it in GitHub Desktop.
Useful Git Commands

Branching

Checkout New Branches

To create a new branch from the current branch and check it out in one command:

git checkout -b my-branch

Delete Branches

To delete a local branch that has been merged:

git branch -d my-branch

If the branch is unmerged but you're sure you want to delete it, use the -D flag instead.

To delete a remote branch:

git push origin :my-branch # notice the colon

Undo a Merge

git reset --hard <sha> # the SHA you want to reset to

Patches

Create a patch from a git commit:

git format-patch -1 <sha> # for specific SHA
git format-patch -1 HEAD # for commit at HEAD

Apply the patch:

patch -p1 < file.patch

Adding, Committing, Diffing

Add Changes Interactively

To review individual change "chunks", do the following:

git add -p

Add a chunk with a, skip a chunk with n, split it with s and stop adding chunks with q.

To learn more about available sub commands, type ?.

Compare Diffs

Compare one branch with another

git diff master...other-branch

Modify a Previous Commit

git commit --amend

Revert the Previous Commit

git revert HEAD^ # or HEAD^1, HEAD^2... HEAD^n for the previous 2, 3 or n+1 commits

Stash Changes

To temporarily save changes with committing, you can stash:

git stash

Then when you're ready to re-apply your changes, you can pop:

git stash pop

Squash Commits

To take a set of commits you've made locally and squash them into one history, try interactive rebase:

git rebase -i master

This will take all your changes that are on top of the master branch and then allow you to choose which commits you want to merge together. Note that this changes your repo history, so be careful and read up on this before attempting.

Tags

To create a new tag at the current point in the repository history:

git tag v1.2 # or "2014-04-16" or "cats" or whatever you want...

To delete a tag:

git tag -d v1.2 # or whatever tag you want to remove

Push your local tags to the remote repo:

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