Git Cheat Sheet
Posted At : February 17, 2010 10:05 AM | Posted By : Bob Silverberg
Related Categories: Git
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:
- mkdir myRepo
- cd myRepo
- git init
Creating a remote repo (at GitHub):
- go to http://github.com/repositories/new
- fill in the form and click Create Repository
Working with Changed Files
Add all new and modified files to the next commit:
- git add .
Add all new, modified and deleted files to the next commit:
- git add -A
Remove a file from the repo, but not from disk:
- git rm --cached filename
Viewing Diffs
Show changes in unstaged files:
- git diff
Viewing changes in staged files:
- git diff --cached
Branching and Tagging
Create a new local branch:
- git branch branchName
Switch to a local branch:
- git checkout branchName
Create a new local branch and switch to it:
- git checkout -b branchName
List all local branches:
- git branch
List all remote branches:
- git branch -r
List all local and remote branches:
- git branch -a
Delete a local branch:
- git branch -d branchName
Compare two branches:
- git diff branchA branchB
Rename a branch:
- git branch -m oldName newName
Create a lightweight tag:
- git tag tagName
Delete a tag:
- git tag -d tagName
Merging
Merge branchA into BranchB, committing all changes:
- git checkout branchB
- git merge branchA
Merge branchA into BranchB, without committing:
- git checkout branchB
- git merge branchA --no-commit --no-ff
Undo a merge:
- git reset --hard ORIG_HEAD
Rebasing
Start an interactive rebase on the current branch, including the past n commits:
- git rebase -i HEAD~n
Start an interactive rebase on the current branch, including all commits not in another branch (e.g., master):
- git rebase -i otherBranch
Working with remotes
Add a remote:
- git remote add remoteName url (e.g., git://github.com/bobsilverberg/ValidateThis.git)
Add a new remote and track an existing branch:
- 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:
- git push remoteName branchName
Create a working copy of a remote branch:
- git checkout -b branchName remoteName/branchName
Delete a remote branch:
- git push remoteName :branchName (notice the colon before the branch name)
Push tags to a remote:
- git push --tags
Delete a tag from a remote:
- git push remoteName :refs/tags/tagName
Undoing stuff
Revert all files in your working directory to the last commit
- git reset --hard HEAD
Undo a merge:
- git reset --hard ORIG_HEAD
I've been an SVN user for years but always open to other alternatives.
Peace,
Steve
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.
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.