The document provides an overview of Git, including what it is, its benefits over centralized version control systems, basic workflows and commands, branching, tagging, and best practices. Git is an open source distributed version control system designed to manage source code and other files. It allows users to work offline and commit changes incrementally to a local repository before pushing to a remote server.
2. What is Git?
Distributed version control system (DVCS)
SVN = Centralized version control system
Mercurial, Bazaar are other DVCS
Initially designed and developed by Linus
Torvalds to manage Linux kernel code base
when BitKeeper revoked free license.
6. Why Git?
Embraced by all major hosting sites:
Github, Google Code, CodePlex, BitBucket
Used by major projects
Linux kernel, Android, ASP.NET MVC,
Facebook, Eclipse, Twitter Boostrap, ...
7. • Able to work disconnected (ex. VPN / home)
o (Almost) everything is local, fast
o Log is always available
o Private workspace
Able to commit whenever, create snapshots,
push when ready (off VPN, done hacking)
Share when ready, ability to modify previous
commits before sharing
• Local branching, easily switch gears when
customer calls (isolate work units)
features, bug fixes, proof of concepts, etc
Why Git? - Benefits
11. Terms
HEAD
last commit of current branch
HEAD^
parent of HEAD
master
default development branch (trunk)
origin
default upstream repository (name of remote
when git clone was used)
13. git init
New
git clone <url>
Existing
run at the project's top-most directory
creates a .git directory
Initializing
14. Review previous commit
git show --stat
git show --name-status
info about last commit
git show HEAD
git show HEAD^^^
git show master~10
git show @{yesterday}
git show master@{May.16}
18. Making a release (tagging)
git tag -a <name>
Create an annotated tag (with message)
git push --tags
Push tags to remote
git tag
list all tags
19. Remotes
git remote -v
git remote add <name> <url>
git remote show <name>
git branch --set-upstream master origin/master
Set local branch to track remote branch
git svn
http://git-scm.com/book/en/Git-and-Other-Systems-Git-
and-Subversion
20. Updating (Push/Pull)
git fetch / git merge
Fetch latest changes from origin, need to
use git merge to apply.
git pull
Pull latest changes from origin (fetch +
merge)
git push [remote_name] [branch_name]
21. Reverting
git checkout .
Revert changes to all files into working directory,
overwriting any local changes. This is most similar to
"svn revert"
git checkout -- <filename>
Revert changes to a file in working directory
git log --diff-filter=D -- <filename>
git checkout <deleting_commit>^ -- <filename>
Restore a deleted file
22. Reverting (cont.)
git reset HEAD
Unstage something
git reset HEAD^
forget about a commit, load it back into the staging area
git reset --hard HEAD / git checkout -f
Return to last committed state (discard all local
changes). Can not be undone (without reflog)
23. Reverting (cont. more)
git commit --amend
Change last commit (correct the previous commit, with
the staged changes)
git revert HEAD
Revert changes (creates a new commit)
30. Extras - Modifying history
git commit --amend
Modify previous commit. Useful to update
message or include missing file without
creating a new commit
git rebase -i
Rewrite history. Squash, reorder, by
dropping
Uses hashes (SHA1) instead of incrementing version numbers due to no central authority
Push full history to another server / remote
ex. Start developing locally, then decide to push to server, Github, Google Code, etc.
Later decide to change remote (switch from Google Code to Github, etc)
staging area == index
working directory == working tree
local repo, HEAD
remote repo, origin
git init == svnadmin create <repo>
git clone <url> == svn checkout <url>
Later add remote if "git init" used
git remote add origin git@github.com:username/Hello-World.git
git pull -u origin master
git show == git show HEAD
git show --name-status ~= git diff HEAD^ --name-status
git timeline is an alias for git log with flags
git log == git log HEAD
git log branchB..branchA
git log branchA ^branchB
show me commits reachable by branchA that are not reachable by branchB
git log master..wiki
git log wiki ^master
git log master.. # HEAD of current branch
show me the commits in my wiki branch not in master
git log origin/master ^master
git log ..@{u}
get incoming (need to do a fetch first). What is on master branch on remote/server not in working context
git log ^master origin/master
git log @{u}..
get outgoing (all commits committed not yet pushed)
One working directory for all branches, fast context switching (git checkout), not like SVN (one for trunk, one for each branch, etc)
git branch <branchname> <start-point>
git branch -t <branchname> <start-point>
git reset --merge
Cancel a conflicting merge (In git 1.7.0 or later)
git merge --ff-only
Fast forward merge only. Guarantees no conflicts. Means branch must have already merged up to HEAD of current branch
git branch <branchname> <sha1>
git checkout -b <branchname> <sha1>
Create a branch from a commit (sha1). checkout will also automatically switch/checkout to new branch
git branch <branchname> <sha1>
git fetch
git reset --HARD origin/master
Create branch at current master/HEAD, and the move master/HEAD to origin/master. Helpful if master has diverged from origin/master, and you want to sync up with master and then merge in your changes slowly.
== Branching and Merging ==
List: git branch
Create: git branch testing
Checkout: git checkout testing
# work, make some commits
Back: git checkout master
Merge: git merge testing
Create: git checkout -b new-branch
Delete: git branch -d new-branch
git merge origin/master
Checkout remote branch (not already checked out) and track
git checkout -t origin/develop
http://gitready.com/intermediate/2009/01/09/checkout-remote-tracked-branch.html
If need to change branches with outstanding changes
http://www.codingdomain.com/git/tricks/
remote = alias for url
git branch --set-upstream master origin/master
What git clone does by default for master
git remote set-branches origin master my_other_branch
Replace any branches currently being tracked and now track "master" and "my_other_branch"
git remote set-branches origin --add my_new_branch
Add "my_new_branch" as a tracking branch (doesn't remote existing)
git branch foo origin/foo
git push
git push origin master
git push origin [branchname]
Can use Subversion as a remote
git fetch is helpful to update local copy of logs, etc
git pull == svn update
replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept
http://gitready.com/beginner/2009/01/11/reverting-files.html
http://www.cheatography.com/samcollett/cheat-sheets/git/
http://www.codingdomain.com/git/tricks/
http://cheat.errtheblog.com/s/git/
http://bryan-murdock.blogspot.com/2007/07/git-revert-is-not-equivalent-to-svn.html
git reset HEAD == git reset --mixed HEAD
http://git-scm.com/2011/07/11/reset.html
http://stackoverflow.com/questions/927358/git-undo-last-commit
Anything in the master branch should always be deployable
http://nvie.com/posts/a-successful-git-branching-model/
http://justinhileman.info/article/changing-history/
git-flow