I'm trying to wrap my head around Git, Linus Torvalds's complicated but powerful distributed version control system. Here's some quick notes and a wad of links:
git config --global user.name "John Q. Hacker" git config --global user.email "email@example.com"
Start a new empty repository
mkdir fooalicious cd fooalicious git init touch README git add README git commit -m 'first commit' git remote add origin firstname.lastname@example.org:nastyhacks/foo.git git push -u origin master
Create a local copy of a remote repository
git clone [remote-repository]
Commit to local repository
git commit -a -m "my message"
Review previous commits
git log --name-only
See what branches exist
git branch -v
Switch to a different branch
git checkout [branch you want to switch to]
Create a new branch and switch to it
git checkout -b [name of new branch]
git merge mybranch
merge the development in the branch "mybranch" into the current branch.
Show remote repositories tracked
Track a remote repository
git remote add --track master origin email@example.com:jqhacker/foo.git
Retrieve from a remote repository
Git fetch grabs changes from remote repository and puts it in your repository's object database. It also fetches branches from remote repository and stores them as remote-tracking branches. (see this.)
Fetch and merge from a remote repository
Push to a remote repository
Pull changes from another fork
git checkout -b otherguy-master master git fetch https://github.com/otherguy/foo.git master git merge otherguy-master/master git checkout master git merge otherguy-master git push origin master
Resolve merge conflict in favor of us/them
git checkout --theirs another.txt git checkout --ours some.file.txt
Diff between local working directory and remote tracking branch
Say you're working with Karen on a project. She adds some nifty features to the source file nifty_files/our_code.py. You'd like to diff your local working copy against hers to see the changes, and prepare to merge them in. First, make sure you have a remote tracking branch for Karen's repo.
git remote add karen git://github.com/karen/our_project.git git remote -v
The results ought to look something like this:
karen git://github.com/karen/our_project.git (fetch) karen git://github.com/karen/our_project.git (push) origin firstname.lastname@example.org:cbare/our_project.git (fetch) origin email@example.com:cbare/our_project.git (push)
Next, fetch Karen's changes into your local repo. Git can't do a diff across the network, so we have to get a local copy of Karen's commits stored in a remote tracking branch.
git fetch karen
Now, we can do our diff.
git diff karen/master:nifty_files/our_code.py nifty_files/our_code.py
Fixing a messed up working tree
git reset --hard HEAD
return the entire working tree to the last committed state
Branches, remote-tracking branches, and tags are all references to commits. Git allows shorthand, so you mostly ever shorthand rather than full names:
- The branch "test" is short for "refs/heads/test".
- The tag "v2.6.18" is short for "refs/tags/v2.6.18".
- "origin/master" is short for "refs/remotes/origin/master".
- Git User’s Manual Mostly cryptic but occasionally very helpful. See:
- Git Tutorial
- github's Git cheat sheets
- github on Working with remotes
- The Git Community Book
- Understanding Git Conceptually
- Git - SVN Crash Course
- The thing about Git by Ryan Tomayko
- git ready
- git equivalent of svn status -u
- git: fetch and merge, don’t pull
- Git Cheat Sheet
- Mastering Git Basics video presentation by Tom Preston-Werner of GitHub