talk to git
DESCRIPTION
A summarize and re-categorize of the git tutorial.TRANSCRIPT
![Page 1: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/1.jpg)
Talk to Git
Jim Chen
![Page 2: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/2.jpg)
VCS and Git
![Page 3: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/3.jpg)
Why Version Control?
•History is important.
•Time travel is possible!o Roll back to old version
•Collaborationo Contributors can work at the same time.
![Page 4: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/4.jpg)
Local Version Control System
•Only on your PC
![Page 5: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/5.jpg)
Centralized Version Control System
•Server owns all versions.
•Clients only have one version.
![Page 6: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/6.jpg)
Distributed Version Control System
•Every one has a full copy
•Recovery friendly
![Page 7: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/7.jpg)
History
In 2005, relationship between Linux Kernel community and commercial company of BitKeeper broken down for reasons.
![Page 8: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/8.jpg)
Goals of Git
•Speed
•Simple design
•Strong support for non-linear development (branches over the world)
•Fully distributed
•Able to handle large projects efficiently (Linux kernel is really huge)
![Page 9: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/9.jpg)
Git Basics
![Page 10: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/10.jpg)
Snapshots, Not Differences
![Page 11: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/11.jpg)
Nearly Operations are local
•Full copy in local computer
•Make changes but can keep update with remote
•Network independent
![Page 12: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/12.jpg)
Integrity
•Check-sum based tracking
•SHA-1 Hash function
•40-characters
![Page 13: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/13.jpg)
Generally Only Adds Data
•Everything committed will be a snapshot in Git history
•Even another commit remove the file.
![Page 14: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/14.jpg)
Three States
•Committed
•Modified
•Staged
![Page 15: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/15.jpg)
Welcome Aboard
![Page 16: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/16.jpg)
Get Repository
• Initialize in existing directory$ git init$ git add *.sh$ git commit -m "Initial project version"
•Clone from existing repository$ git clone <url>
![Page 17: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/17.jpg)
File Status
untrackedunmodifie
dmodified staged
add the file
remove the file
git add <file>
git rm <file> commit
stage the file
edit the filegit add <file>
git commit
git reset HEAD <file>
git checkout <file>
![Page 18: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/18.jpg)
Checking Status
$ git status
![Page 19: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/19.jpg)
Tracking New File
$ touch README$ git status
![Page 20: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/20.jpg)
Tracking New File
$ git add README$ git status
![Page 21: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/21.jpg)
Staging modfied
Make some change in hello.sh$ git status
![Page 22: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/22.jpg)
Staging modfied
$ git add hello.sh$ git status
![Page 23: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/23.jpg)
Viewing Unstaged Changes
$ vim hello.sh$ git diff
![Page 24: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/24.jpg)
Viewing Staged Changes
$ git diff --cached
![Page 25: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/25.jpg)
Discard modified
$ git status$ git checkout hello.sh$ git status
![Page 26: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/26.jpg)
Unstage staged
$ git reset HEAD hello.sh
![Page 27: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/27.jpg)
Commit It
$ git commit
![Page 28: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/28.jpg)
Guidelines to Commit
No whitespaces$ git diff --check
• Try to make each commit a logically separate change set.
• Getting in the habit of creating quality commit messages.o This would help others to review.
![Page 29: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/29.jpg)
More commands
•Removing files$ git rm <file>
•Moving files$ git mv <file>
![Page 30: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/30.jpg)
Viewing History in CLI
$ git log
![Page 31: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/31.jpg)
Viewing History in GUI
$ gitk
![Page 32: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/32.jpg)
Oops!Something missed.
But committed already...
![Page 33: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/33.jpg)
Change Last Commit
$ git commit --amend
![Page 34: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/34.jpg)
Working with Remote
![Page 35: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/35.jpg)
Clone Repository
$ git clone <url> <folder>
![Page 36: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/36.jpg)
Showing Remotes
$ git remote -v
![Page 37: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/37.jpg)
Adding and Removing Remotes
$ git remote add <remote-name> <url>$ git remote rm <remote-name>
![Page 38: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/38.jpg)
Fetching and Pulling from Remotes
$ git fetch <remote-name>$ git pull <remote-name>
= git fetch + git merge
![Page 39: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/39.jpg)
Pushing changes to Remote
$ git push <remote-name> <branch-name>
![Page 40: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/40.jpg)
Branching
![Page 41: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/41.jpg)
Why branches?
•Stabilize stables
•Make topics no bother
![Page 42: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/42.jpg)
Data Structure
•Blobo Storing file data
•Treeo Structure of project, just like directory
•Commito A pointer to a single tree, with some meta-
data
•Tago Mark a commit as special
![Page 43: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/43.jpg)
Commit
![Page 44: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/44.jpg)
Versions
![Page 45: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/45.jpg)
A lightweight movable pointer to one of the commits.
Branch in Git is
HEAD is a special pointer to the current working branch you are.
![Page 46: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/46.jpg)
I want to resolve a topic
![Page 47: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/47.jpg)
$ git branch topic1$ git checkout topic1
C0 C1 C2
master
HEAD
topic1
HEAD
![Page 48: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/48.jpg)
$ git checkout -b topic1
C0 C1 C2
master
HEAD
topic1
HEAD
![Page 49: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/49.jpg)
Commit new change on topic1
C0 C1 C2
master
topic1
C3
topic1
![Page 50: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/50.jpg)
Urgent!!Bug fix for master
![Page 51: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/51.jpg)
Create hotfix branch from master for fix bug (C4)
$ git checkout master$ git checkout -b hotfixFix bug and commit change
C0 C1 C2
master
topic1
C3
C4
hotfix
Quiz:Where is the HEAD? HEAD
![Page 52: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/52.jpg)
Merge branches
hotfix is good for release
![Page 53: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/53.jpg)
Merge Mechanism
•Fast-forwardBranches are in the same history flow so Git can
only change the pointer to the new one.
•Three-way merge– Locate the common ancestor– Git automatically calculate how to merge– Create a merge commit recording new
snapshot
![Page 54: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/54.jpg)
Fast-forward
$ git checkout master$ git merge hotfixUpdating ...Fast forward
C0 C1 C2
master
topic1
C3
C4
hotfix
master
![Page 55: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/55.jpg)
Three-way merge
$ git checkout master$ git merge topic1Merge made by recursive
C0 C1 C2
topic1
C3
C4
master
C5
Snapshot to merge into
Snapshot to merge into
Common Ancestor C6
master
![Page 56: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/56.jpg)
Gosh...Conflict happens
![Page 57: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/57.jpg)
What will you see when merge conflict
![Page 58: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/58.jpg)
Resolve and commit the change
![Page 59: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/59.jpg)
Rebasing
Merge is good butI want a clearer history
![Page 60: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/60.jpg)
Merge
$ git checkout master$ git merge topic1Merge made by recursive
C0 C1 C2
topic1
C3
C4
C5
C6
master
![Page 61: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/61.jpg)
Basic Rebase
$ git checkout topic1$ git rebase master
C0 C1 C2
C4
topic1
C3 C5
master
topic1
C3' C5'
![Page 62: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/62.jpg)
Rebasing pushed commits confuses reviewers.
Do not rebase commits have pushed to a public repository!
![Page 63: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/63.jpg)
Example of rebasing a pushed commit
![Page 64: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/64.jpg)
What's more?
![Page 65: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/65.jpg)
Where is the commit lost?
Look into commit updated history
![Page 66: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/66.jpg)
$ git reflog
SHA-1 id you can use
Cherry-pick commit d6297c2$ git cherry-pick d6297c2
![Page 67: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/67.jpg)
Stashing
New request comes but I am working on something else.
![Page 68: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/68.jpg)
$ git stash$ git stash list/pop/apply
![Page 69: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/69.jpg)
Interactive Rebase
Change multiple commitsReordering
![Page 70: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/70.jpg)
Interactive rebase
C0 C1 C2
master
C3 C4
$ git rebase -i HEAD~2
Successfully rebased and updated refs/heads/master.
master
C4 C3
![Page 71: Talk to git](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a5010b4c905572f8b4805/html5/thumbnails/71.jpg)
git official sites: http://git-scm.com
commands: http://git-scm.com/book/commands
zh book: http://git-scm.com/book/zh
Git and Linux Kernel: http://en.wikipedia.org/wiki/Linux_kernel#Revision_control
Git Immersion: http://gitimmersion.com (Practices)
Revision Control: http://en.wikipedia.org/wiki/Revision_control
Distributed VCS: http://en.wikipedia.org/wiki/Distributed_revision_control
References