Mastering Git Basicsby Tom Preston-Werner
Presentation http://test.smackaho.st:9090/onepage
1 of 116 3/31/10 11:56 AM
Forget everything you know
about version control
Presentation http://test.smackaho.st:9090/onepage
4 of 116 3/31/10 11:56 AM
Mac
Git OSX Installer
Homebrew
MacPorts
Manually
Presentation http://test.smackaho.st:9090/onepage
6 of 116 3/31/10 11:56 AM
Linux
Apt
Ports
Yum
Manually
Presentation http://test.smackaho.st:9090/onepage
7 of 116 3/31/10 11:56 AM
Initial Configurationhttp://gist.github.com/340818
$ git config --global user.name "Tom Preston-Werner"$ git config --global user.email "[email protected]"
$ git config --global color.ui true
Presentation http://test.smackaho.st:9090/onepage
10 of 116 3/31/10 11:56 AM
Creating and Committing
Presentation http://test.smackaho.st:9090/onepage
11 of 116 3/31/10 11:56 AM
Make a directory for your new project $ cd path/to/repos $ mkdir hello $ cd hello
Presentation http://test.smackaho.st:9090/onepage
12 of 116 3/31/10 11:56 AM
git init $ ls -al # dir is empty $ git init # initialize git repo $ ls -al # new .git dir
Presentation http://test.smackaho.st:9090/onepage
14 of 116 3/31/10 11:56 AM
Write some code $ vim hello.sh $ vim goodbye.sh
Presentation http://test.smackaho.st:9090/onepage
16 of 116 3/31/10 11:56 AM
git add$ git add hello.sh # add content to index$ git add goodbye.sh # add content to index
Presentation http://test.smackaho.st:9090/onepage
17 of 116 3/31/10 11:56 AM
Index now contains working dir content
Presentation http://test.smackaho.st:9090/onepage
18 of 116 3/31/10 11:56 AM
git statusShow the status of index and working dir
$ git status
Presentation http://test.smackaho.st:9090/onepage
19 of 116 3/31/10 11:56 AM
git commit $ git commit # make a commit
Presentation http://test.smackaho.st:9090/onepage
20 of 116 3/31/10 11:56 AM
A commit is a snapshot taken from the index
NOT THE WORKING DIRECTORY
Presentation http://test.smackaho.st:9090/onepage
21 of 116 3/31/10 11:56 AM
git logPrint a log of commits
$ git log
Presentation http://test.smackaho.st:9090/onepage
22 of 116 3/31/10 11:56 AM
RecapThe current state of the repo
Presentation http://test.smackaho.st:9090/onepage
23 of 116 3/31/10 11:56 AM
Make some ambitious changes $ vim hello.sh # modify the file
Presentation http://test.smackaho.st:9090/onepage
24 of 116 3/31/10 11:56 AM
Working dir now contains D0: a delta from C0
Presentation http://test.smackaho.st:9090/onepage
25 of 116 3/31/10 11:56 AM
Review the changed files $ git status
Presentation http://test.smackaho.st:9090/onepage
26 of 116 3/31/10 11:56 AM
git diffShow diff between index and working dir
$ git diff
Presentation http://test.smackaho.st:9090/onepage
27 of 116 3/31/10 11:56 AM
D0 = Diff(Index, WorkingDir)
Presentation http://test.smackaho.st:9090/onepage
28 of 116 3/31/10 11:56 AM
git add -pInteractively add changed hunks
$ git add -p
Presentation http://test.smackaho.st:9090/onepage
29 of 116 3/31/10 11:56 AM
D0 is now in working dir AND index
Presentation http://test.smackaho.st:9090/onepage
30 of 116 3/31/10 11:56 AM
git diff now shows nothing!Where did it go?
Presentation http://test.smackaho.st:9090/onepage
31 of 116 3/31/10 11:56 AM
git diff --stagedShow diff between commit and index
$ git diff --staged
Presentation http://test.smackaho.st:9090/onepage
32 of 116 3/31/10 11:56 AM
D0 = Diff(Commit, Index)
Presentation http://test.smackaho.st:9090/onepage
33 of 116 3/31/10 11:56 AM
git commit -mCreate a commit with the given commit message $ git commit -m "more ambition!"
Presentation http://test.smackaho.st:9090/onepage
34 of 116 3/31/10 11:56 AM
Commit is rolled from index
Presentation http://test.smackaho.st:9090/onepage
35 of 116 3/31/10 11:56 AM
Remember:
Make changes to working dir1.
Stage those changes to the index2.
Commit the current state of the index3.
Presentation http://test.smackaho.st:9090/onepage
36 of 116 3/31/10 11:56 AM
RecapTwo commits, C0 and C1
Presentation http://test.smackaho.st:9090/onepage
37 of 116 3/31/10 11:56 AM
Every commit has zero or
more parent commits
Presentation http://test.smackaho.st:9090/onepage
38 of 116 3/31/10 11:56 AM
git branchShow all local branches
$ git branch
Presentation http://test.smackaho.st:9090/onepage
40 of 116 3/31/10 11:56 AM
The default branch is named
master
Presentation http://test.smackaho.st:9090/onepage
41 of 116 3/31/10 11:56 AM
Branches are just pointers to commits
Presentation http://test.smackaho.st:9090/onepage
42 of 116 3/31/10 11:56 AM
It's easiest to think of the working
directory as corresponding to a branch
Presentation http://test.smackaho.st:9090/onepage
43 of 116 3/31/10 11:56 AM
As you commit, the branch moves with you
Presentation http://test.smackaho.st:9090/onepage
44 of 116 3/31/10 11:56 AM
As you commit, the branch moves with you
Presentation http://test.smackaho.st:9090/onepage
45 of 116 3/31/10 11:56 AM
As you commit, the branch moves with you
Presentation http://test.smackaho.st:9090/onepage
46 of 116 3/31/10 11:56 AM
git branchCreate a new branch pointing at
the current commit $ git branch uppercase
Presentation http://test.smackaho.st:9090/onepage
47 of 116 3/31/10 11:56 AM
A new branch has been created
but the working dir has not changed
Presentation http://test.smackaho.st:9090/onepage
48 of 116 3/31/10 11:56 AM
git checkoutSwitch working dir to the given branch
$ git checkout uppercase
Presentation http://test.smackaho.st:9090/onepage
49 of 116 3/31/10 11:56 AM
Working dir now corresponds to uppercase
Presentation http://test.smackaho.st:9090/onepage
50 of 116 3/31/10 11:56 AM
Convert the string to uppercase
on this branch and commit the change$ vim hello.sh$ git add -p$ git commit -m 'convert string to uppercase'
Presentation http://test.smackaho.st:9090/onepage
51 of 116 3/31/10 11:56 AM
Branches have now diverged!
Presentation http://test.smackaho.st:9090/onepage
52 of 116 3/31/10 11:56 AM
git branch -vShow branches and the commits they point to $ git branch -v
Presentation http://test.smackaho.st:9090/onepage
53 of 116 3/31/10 11:56 AM
Switch back to the master branch.
Notice that working dir has been changed.$ cat hello.sh # uppercase version$ git checkout master$ cat hello.sh # master version
Presentation http://test.smackaho.st:9090/onepage
54 of 116 3/31/10 11:56 AM
Working directory is now consistent
with the master branch
Presentation http://test.smackaho.st:9090/onepage
55 of 116 3/31/10 11:56 AM
git diff R1 R2Diff between two arbitrary commits
$ git diff master uppercase
Presentation http://test.smackaho.st:9090/onepage
56 of 116 3/31/10 11:56 AM
Show the work done between branches
Presentation http://test.smackaho.st:9090/onepage
57 of 116 3/31/10 11:56 AM
git mergeMerge the given commit into the current branch $ git merge uppercase
Presentation http://test.smackaho.st:9090/onepage
58 of 116 3/31/10 11:56 AM
Both branches now point at the same commit
Presentation http://test.smackaho.st:9090/onepage
59 of 116 3/31/10 11:56 AM
This kind of merge is known as a
fast-forward merge because the
merged branch was a direct descendent
Presentation http://test.smackaho.st:9090/onepage
60 of 116 3/31/10 11:56 AM
git branch -dDelete the given branch
$ git branch -d uppercase
Presentation http://test.smackaho.st:9090/onepage
61 of 116 3/31/10 11:56 AM
Only the pointer has been deleted
Presentation http://test.smackaho.st:9090/onepage
62 of 116 3/31/10 11:56 AM
What if both branches have commits?
Presentation http://test.smackaho.st:9090/onepage
63 of 116 3/31/10 11:56 AM
git checkout -bCreate a new branch and switch to it
$ git checkout -b greeting
Presentation http://test.smackaho.st:9090/onepage
64 of 116 3/31/10 11:56 AM
Modify the greetings; commit;
and switch back to master $ vim hello.sh $ vim goodbye.sh $ git add -p $ git commit -m 'new greetings' $ git checkout master
Presentation http://test.smackaho.st:9090/onepage
65 of 116 3/31/10 11:56 AM
Create a new file on master
and attempt to add it $ vim README $ git add -p # nothing to review! $ git status # find out why
Presentation http://test.smackaho.st:9090/onepage
66 of 116 3/31/10 11:56 AM
File trackingGit remembers what files have been added.
New files must be explicitly added.
Presentation http://test.smackaho.st:9090/onepage
67 of 116 3/31/10 11:56 AM
Add the contents of the new file $ git add . # add everything! $ git status # see that it worked
Presentation http://test.smackaho.st:9090/onepage
68 of 116 3/31/10 11:56 AM
Oops, we forgot something... $ vim README $ git status # make sure
Presentation http://test.smackaho.st:9090/onepage
69 of 116 3/31/10 11:56 AM
Two deltas have been introduced
Presentation http://test.smackaho.st:9090/onepage
70 of 116 3/31/10 11:56 AM
Inspect the deltas, and continue $ git diff --staged # old change $ git diff # new change $ git add -p # add again $ git commit -m "add a readme"
Presentation http://test.smackaho.st:9090/onepage
71 of 116 3/31/10 11:56 AM
Recap of forked lineage
Presentation http://test.smackaho.st:9090/onepage
72 of 116 3/31/10 11:56 AM
Merge greeting into master $ git merge greeting
Presentation http://test.smackaho.st:9090/onepage
73 of 116 3/31/10 11:56 AM
A new merge commit (C5) is created
Presentation http://test.smackaho.st:9090/onepage
74 of 116 3/31/10 11:56 AM
This kind of merge is known as
a recursive merge and
uses a 3-way merge strategy
Presentation http://test.smackaho.st:9090/onepage
75 of 116 3/31/10 11:56 AM
Three way (recursive) merge strategy
Presentation http://test.smackaho.st:9090/onepage
76 of 116 3/31/10 11:56 AM
git log --graphShow the commit log with graph structure
$ git log --graph
Presentation http://test.smackaho.st:9090/onepage
77 of 116 3/31/10 11:56 AM
First, a word about referencesA reference is a way to refer to a commit
Presentation http://test.smackaho.st:9090/onepage
79 of 116 3/31/10 11:56 AM
Examples:
5c673e53912d86eb771ee0ab0c678ecffa4b939c
5c673e5
master
HEAD
HEAD^^
Presentation http://test.smackaho.st:9090/onepage
80 of 116 3/31/10 11:56 AM
HEAD is a dynamic reference that
follows your current checkout
Presentation http://test.smackaho.st:9090/onepage
81 of 116 3/31/10 11:56 AM
HEAD is a dynamic reference that
follows your current checkout
Presentation http://test.smackaho.st:9090/onepage
82 of 116 3/31/10 11:56 AM
HEAD is a dynamic reference that
follows your current checkout
Presentation http://test.smackaho.st:9090/onepage
83 of 116 3/31/10 11:56 AM
Ancestry reference modifiers
Presentation http://test.smackaho.st:9090/onepage
84 of 116 3/31/10 11:56 AM
git reset --hardReset a branch and working dir
$ git reset --hard head^
Presentation http://test.smackaho.st:9090/onepage
85 of 116 3/31/10 11:56 AM
master is now pointing to C4.
C5 still exists, but is dangling.
Presentation http://test.smackaho.st:9090/onepage
86 of 116 3/31/10 11:56 AM
git reflogShow previous values of HEAD
$ git reflog
Presentation http://test.smackaho.st:9090/onepage
87 of 116 3/31/10 11:56 AM
What about merge conflicts?
Presentation http://test.smackaho.st:9090/onepage
88 of 116 3/31/10 11:56 AM
Modify a line that was also changed
in the greeting branch $ vim hello.sh $ git add -p
Presentation http://test.smackaho.st:9090/onepage
89 of 116 3/31/10 11:56 AM
git commit --amendModify the content of the last commit
$ git commit --amend
Presentation http://test.smackaho.st:9090/onepage
90 of 116 3/31/10 11:56 AM
Attempt to merge greeting $ git merge greeting
Presentation http://test.smackaho.st:9090/onepage
91 of 116 3/31/10 11:56 AM
Cleanly merged files are staged.
Conflicts are left in working dir. $ git diff --staged # clean $ git diff # dirty $ git status # summary
Presentation http://test.smackaho.st:9090/onepage
92 of 116 3/31/10 11:56 AM
Clean up the mess; use git add to mark
a conflicted file as properly merged.
Commit to seal the deal. $ git add hello.sh $ git commit
Presentation http://test.smackaho.st:9090/onepage
93 of 116 3/31/10 11:56 AM
Back to your repos directory $ cd ..
Presentation http://test.smackaho.st:9090/onepage
95 of 116 3/31/10 11:56 AM
git cloneClone a repository
$ git clone hello helloclone $ cd helloclone
Presentation http://test.smackaho.st:9090/onepage
96 of 116 3/31/10 11:56 AM
You can also clone from remote repositories
Presentation http://test.smackaho.st:9090/onepage
97 of 116 3/31/10 11:56 AM
git remoteDisplay a list of remotes
$ git remote -v
Presentation http://test.smackaho.st:9090/onepage
98 of 116 3/31/10 11:56 AM
git branch -aShow all branches (local and remote)
$ git branch -a
Presentation http://test.smackaho.st:9090/onepage
99 of 116 3/31/10 11:56 AM
Assume that upstream makes a commit
on the master branch
Presentation http://test.smackaho.st:9090/onepage
100 of 116 3/31/10 11:56 AM
Current state of upstream
Presentation http://test.smackaho.st:9090/onepage
101 of 116 3/31/10 11:56 AM
git fetchFetch commits from the given remote
$ git fetch origin
Presentation http://test.smackaho.st:9090/onepage
102 of 116 3/31/10 11:56 AM
Remote commits have been downloaded,
but have not affected your local branches
Presentation http://test.smackaho.st:9090/onepage
103 of 116 3/31/10 11:56 AM
View the changes in the upstream
origin/master branch $ git diff head origin/master
Presentation http://test.smackaho.st:9090/onepage
104 of 116 3/31/10 11:56 AM
Comparing unmerged upstream commits
Presentation http://test.smackaho.st:9090/onepage
105 of 116 3/31/10 11:56 AM
Merge the upstream origin/master branch
into your local master branch $ git merge origin/master
Presentation http://test.smackaho.st:9090/onepage
106 of 116 3/31/10 11:56 AM
The upstream commits have been merged
Presentation http://test.smackaho.st:9090/onepage
107 of 116 3/31/10 11:56 AM
What if you want to share your changes?
Presentation http://test.smackaho.st:9090/onepage
108 of 116 3/31/10 11:56 AM
Assume a new branch fi x with a commit
Presentation http://test.smackaho.st:9090/onepage
109 of 116 3/31/10 11:56 AM
git pushPush some commits to a remote
$ git push origin fix
Presentation http://test.smackaho.st:9090/onepage
110 of 116 3/31/10 11:56 AM
Remote now contains the fi x branch!
Presentation http://test.smackaho.st:9090/onepage
111 of 116 3/31/10 11:56 AM
Now others that have access to the remote
can fetch and work on the fi x branch
Presentation http://test.smackaho.st:9090/onepage
112 of 116 3/31/10 11:56 AM
Collaboration in Git is all about moving
commits around between repositories
Presentation http://test.smackaho.st:9090/onepage
113 of 116 3/31/10 11:56 AM
Where do I go from here?
Presentation http://test.smackaho.st:9090/onepage
114 of 116 3/31/10 11:56 AM
Pro Git by Scott Chaconhttp://progit.org
Presentation http://test.smackaho.st:9090/onepage
115 of 116 3/31/10 11:56 AM
Top Related