Photo from Chile

Git Cheat Sheet

I've been using Git for awhile, but I still use Subversion for most of my projects, so I seem to always come back to Git after a long break. Of course, by that time I've forgotten how to do certain things, so I thought I'd throw together a quick cheat sheet that I, and anyone else, can use as a reference. I will continually update this post as I come up with new questions and answers.

Creating repos

Creating a local repo:

  1. mkdir myRepo
  2. cd myRepo
  3. git init

Creating a remote repo (at GitHub):

  1. go to http://github.com/repositories/new
  2. fill in the form and click Create Repository

Working with Changed Files

Add all new and modified files to the next commit:

  1. git add .

Add all new, modified and deleted files to the next commit:

  1. git add -A

Remove a file from the repo, but not from disk:

  1. git rm --cached filename

Viewing Diffs

Show changes in unstaged files:

  1. git diff

Viewing changes in staged files:

  1. git diff --cached

Branching and Tagging

Create a new local branch:

  1. git branch branchName

Switch to a local branch:

  1. git checkout branchName

Create a new local branch and switch to it:

  1. git checkout -b branchName

List all local branches:

  1. git branch

List all remote branches:

  1. git branch -r

List all local and remote branches:

  1. git branch -a

Delete a local branch:

  1. git branch -d branchName

Compare two branches:

  1. git diff branchA branchB

Rename a branch:

  1. git branch -m oldName newName

Create a lightweight tag:

  1. git tag tagName

Delete a tag:

  1. git tag -d tagName

Merging

Merge branchA into BranchB, committing all changes:

  1. git checkout branchB
  2. git merge branchA

Merge branchA into BranchB, without committing:

  1. git checkout branchB
  2. git merge branchA --no-commit --no-ff

Undo a merge:

  1. git reset --hard ORIG_HEAD

Rebasing

Start an interactive rebase on the current branch, including the past n commits:

  1. git rebase -i HEAD~n

Start an interactive rebase on the current branch, including all commits not in another branch (e.g., master):

  1. git rebase -i otherBranch

Working with remotes

Add a remote:

  1. git remote add remoteName url (e.g., git://github.com/bobsilverberg/ValidateThis.git)

Add a new remote and track an existing branch:

  1. git remote add --track branchName remoteName url (e.g., git://github.com/bobsilverberg/ValidateThis.git)

Take an existing local branch and add it to a remote:

  1. git push remoteName branchName

Create a working copy of a remote branch:

  1. git checkout -b branchName remoteName/branchName

Delete a remote branch:

  1. git push remoteName :branchName (notice the colon before the branch name)

Push tags to a remote:

  1. git push --tags

Delete a tag from a remote:

  1. git push remoteName :refs/tags/tagName

Undoing stuff

Revert all files in your working directory to the last commit

  1. git reset --hard HEAD

Undo a merge:

  1. git reset --hard ORIG_HEAD

TweetBacks
Comments
Bob, when it comes to overall use and setup ease.. and if you had to make a choice... GiT or SVN?

I've been an SVN user for years but always open to other alternatives.

Peace,
Steve
# Posted By Steve Duys | 2/19/10 10:44 AM
If it were only about ease of use and setup I'd have to give the nod to Subversion. The tools available for SVN are definitely far superior, especially on the Mac.

What I like about Git is that it's a decentralized system, so I have an actual copy of the entire repo on my local machine and can commit to my heart's content even while offline.

Git also makes merging a breeze, especially when dealing with multiple developers all working on their own forks of the repo. GitHub also makes that a bit easier, and is a great resource for open source projects.

But there is definitely a fairly steep learning curve, which I'm still climbing, and I do miss my "Synchronize with Repository" Eclipse menu item.
# Posted By Bob Silverberg | 2/19/10 10:51 AM
Bob, thanks for posting this. I'm just now looking into Git so this post will be very helpful for me.
# Posted By Aaron West | 3/7/10 11:02 AM
nice, Bob!

One very slick and unique git feature is "git stash", which creates a clipboard of changes while simultaneously reverting the current branch back to HEAD :

... code, delete, move, chop, slice and dice ...
git stash
git stash branch my-new-branch

This copies the state of the file system into my-new-branch, reverts the current branch to HEAD, and switches you to my-new-branch.
# Posted By bill shelton | 6/21/10 6:11 AM
Thanks for the pointer, Bill. I'll look to add git stash to my cheat sheet the next time I update it.
# Posted By Bob Silverberg | 6/21/10 8:13 AM