Skip to content

Instantly share code, notes, and snippets.

@rossluebe
Last active February 25, 2016 21:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossluebe/fd83cacabb6245ff4cb2 to your computer and use it in GitHub Desktop.
Save rossluebe/fd83cacabb6245ff4cb2 to your computer and use it in GitHub Desktop.

Git Cheat Sheet

Transcribed from Tower’s blog post

Create

$ git clone ssh://user@domain.com/repo.git

Clone an existing repository

$ git init

Create a new local repository

Local Changes

$ git status

Changed files in your working directory

$ git diff

Changes to tracked files

$ git add .

Add all current changes to the next commit

$ git add -p <file>

Add some changes in to the next commit

$ git commit -a

Commit all local changes in tracked files

$ git commit

Commit previously staged changes

$ git commit --amend

Change the last commit Don’t amend published commits!

Commit History

$ git log

Show all commits, starting with newest

$ git log -p <file>

Show changes over time for a specific file

$ git blame <file>

Who changed what and when in <file>

Branches & Tags

$ git branch

List all existing branches

$ git checkout <branch>

Switch HEAD branch

$ git branch <new-branch>

Create a new branch based on your current HEAD

$ git checkout --track <remote/branch>

Create a new tracking branch based on a remote branch

$ git branch -d <branch>

Delete a local branch

$ git tag <tag-name>

Mark the current commit with a tag

Update & Publish

$ git remote -v

List all currently configured remotes

$ git remote show <remote>

Show information about a remote

$ git remote add <remote> <url>

Add new remote repository, named <remote>

$ git fetch <remote>

Download all changes from <remote>, but don’t integrate into HEAD

$ git pull <remote> <branch>

Download changes and directly merge/integrate into HEAD

$ git push <remote> <branch>

Publish local changes on a remote

$ git branch -dr <remote/branch>

Delete a branch on the remote

$ git push --tags

Publish your tags

Merge & Rebase

$ git merge <branch>

Merge <branch> into your current HEAD

$ git rebase <branch>

Rebase your current HEAD onto <branch> Don’t rebase published commits!

$ git rebase --abort

Abort a rebase

$ git rebase --continue

Continue a rebase after resolving conflicts

$ git mergetool

Use your configured merge tool to solve conflicts

$ git add <resolved-file>
$ git rm <resolved-file>

Use your editor to manually solve conflicts and (after resolving) mark file as resolved

Undo

$ git reset --hard HEAD

Discard all local changes in your working directory

$ git checkout HEAD <file>

Discard local changes in a specific file

$ git revert <commit>

Revert a commit (by producing a new commit with contrary changes)

Reset your HEAD pointer to a previous commit

$ git reset --hard <commit>

...and discard all changes since then

$ git reset <commit>

...and preserve all changes as unstaged changes

$ git reset --keep <commit>

...and preserve uncommitted local changes

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