Skip to content

Instantly share code, notes, and snippets.

@ryansturmer
Last active October 12, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryansturmer/4043389 to your computer and use it in GitHub Desktop.
Save ryansturmer/4043389 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheat Sheet

This attempts to be a useful "cheat sheet" for git. I'm just writing git recipes down here as I find and use them.

Getting Code

Clone a repository (GitHub)

git clone git@github.com:username/repository.git

Checkout a branch into your working tree

git checkout branchname

Dealing with the Working Tree

Delete untracked files in your working tree

git clean -f      # Remove untracked files
git clean -f -d   # Also directories
git clean -f -x   # Also ignored files
git clean -f -X   # Just ignored files

Restore to the HEAD of your current branch This is a good way to abort a merge in progress

git reset --hard HEAD

Restore a file from an old revision

git checkout [commit_id] -- path/to/oldfile

Checkout a file from another branch

git checkout [branch_name] -- /path/to/file(s)

Merging

Resolve a merge conflict with "ours" or "theirs"

git checkout --ours [filename]
git checkout --theirs [filename]

Merge a branch

git merge [branchname]

Merge a branch, adjusting the rename threshold

git merge -X rename-threshold=<0-100> [branchname]

The rename threshold is %difference. A lower number means a more permissive rename detect

Branches

Create a new branch locally and switch to it

git checkout -b branchname

Delete a local branch

git branch -d branchname

Push a new branch for the first time

git push -u origin branchname

Tags

Create an annotated tag

Annotated tags are more rich than regular tags, and include date/author information.

git tag -a [tagname] -m "Tag Message..."

Checkout a tag

git checkout [tagname]

The History, and Logs

View the commit history, showing the status of files that changed

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