3. Design GoalDesign Goal
Git is an open source, distributed version
control system designed to be fast and
branching
Decentralized
vs Centralized (SVN / Perforce / CVS)
Suitable for fast development of multiple branch in
parallel
Git Generally DOES NOT delete data
Git Generally append data in version tree.
Recovering deleted data could be tricky
2 . 1
5. Pros / ConsPros / Cons
Centralized Decentralized
Concurrent Access
Control
File Locking N.A; Manual
intervention is required
to resolve the conflict.
Offline Development Unable to submit
changelist; create a tag /
branch.
Able to commit to local
repository
Remote Server One Centralized server multiple remote repo
2 . 3
6. Pros / ConsPros / Cons
Centralized Decentralized
Concurrent Access
Control
File Locking N.A; Manual
intervention is required
to resolve the conflict.
Offline Development Unable to submit
changelist; create a tag /
branch.
Able to commit to local
repository
Remote Server One Centralized server multiple remote repo
2 . 3
7. Pros / ConsPros / Cons
Centralized Decentralized
Concurrent Access
Control
File Locking N.A; Manual
intervention is required
to resolve the conflict.
Offline Development Unable to submit
changelist; create a tag /
branch.
Able to commit to local
repository
Remote Server One Centralized server multiple remote repo
2 . 3
8. Git ObjectsGit Objects
$ ll .git/
4096 Apr 7 2018 branches/
528 Nov 9 17:32 COMMIT_EDITMSG
284 Nov 8 20:48 config
73 Apr 7 2018 description
272 Jun 18 00:04 FETCH_HEAD
23 Nov 9 12:35 HEAD
4096 Apr 7 2018 hooks/
16153 Nov 9 17:31 index
4096 Apr 7 2018 info/
4096 Apr 7 2018 logs/
4096 Aug 15 22:56 objects/
41 Nov 8 20:49 ORIG_HEAD
107 Apr 7 2018 packed-refs
4096 Nov 6 14:31 refs/
3 . 1
9. CommitCommit
A.k.a. Ref / Reference
Alias to a hash (SHA-1) commit hash
See the SHA-1 commit hashes
$ git log
commit 059806c1e05ed7a3a387da16d05844f63c47251a
Author: Author Name <Author Email>
Date: Fri Nov 9 17:31:31 2018 +0800
Commit Title
Commit Description
commit 91e622c61f99f24dbda741050e251837a643f6c9
Author: Michael Fong <mcfong.open@gmail.com>
Date: Thu Nov 8 20:26:33 2018 +0800
Another Commit
3 . 2
10. HEADHEAD
HEAD file is a symbolic ref for the current branch
$ tree .git/refs/heads/
.git/refs/heads/
├── ConnectionPool
├── LocalhostJmxConnectorServerProxyDemo
├── master
├── RACBenchmark
├── string-builder-perf-death-match
├── study-fadvise-effect
├── tuneWarmUpIteration
└── unintentional-obj-retension
0 directories, 8 files
$ cat .git/refs/heads/master
059806c1e05ed7a3a387da16d05844f63c47251a
Uses HEAD file to store latest commit info of the
branch
3 . 3
12. Initialize Git SettingInitialize Git Setting
User Information
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
Editor Information
# Set default editor to vim
$ git config --global core.editor vim
# Set default editor to emacs
$ git config --global core.editor emacs
List current settings
$ git config --list
user.email=mcfong.open@gmail.com
user.name=Michael Fong
core.editor=vim
...
4 . 2
13. Clone RepositoryClone Repository
$ git clone https://github.com/<username>/<project>
Clone repository via Https
$ git clone ssh://github.com/<username>/<project>
Clone repository via SSH
Show current repository status
$ git status
On branch <local-repo>
Your branch is up-to-date with <remote-repo>.
nothing to commit, working directory clean
<If there is (new) file different from current ver,
they will appear here>
# Diff against local workspace and index
$ git diff
4 . 3
14. Commit ChangesCommit Changes
# add new files
$ git add 1.file
# add by pattern
$ git add *.file
# add by everything under directory
$ git add ./
$ git add -all
# remove file from tracking
$ git rm file.py
# Diff against local repo and index
$ git diff --cached
Staging Changes
Update to local Staging Area / Index
Commit Changes
Update to local repo
# Enter commit commit description in default editor
$ git commit
# alternatively, one command commit
$ git commit -am "Enter your commit description"
4 . 4
15. Branch ManagementBranch Management
# create a new branch
$ git checkout master
$ git branch new-branch
# Alternatively, create a new branch from remote-repo/remote-branch
$ git checkout <remote-repo>/<remote-branch> -b <local-branch>
$ git checkout origin/master -b new-branch
#Create a new branch from tag
$ git checkout tags/<tag-name> -b new-branch
Create a new local branch
Switch to existing local branch
List all Branches
$ git checkout new-branch
# (noarg - local branch; -r remote branch; -a all branch)
$ git branch -av
Delete Branch
# Delete local repository
$ git branch -d new-branch
# Delete local repository by force
$ git branch -D new-branch
# Delete a branch from remote repository
$ git push <remote-repo> :<branch-name> 4 . 5
17. (Local Branch) Merge Strategy I(Local Branch) Merge Strategy I
Fast Forward Merge
When the merge resolves as a fast-forward, only
update the branch pointer, without creating a
merge commit.
Default merge strategy
$ git checkout master
$ git merge new-branch (--ff)
4 . 7
18. (Local Branch) Merge Strategy II(Local Branch) Merge Strategy II
Non Fast Forward Merge
Create a merge commit
$ git checkout master
$ git merge new-branch --no-ff
4 . 8
20. (Remote Branch) Merge(Remote Branch) Merge
Merge between two branch on remote repository
Github: Pull Request
Gitlab: Merge Request
Non fast forward merge
Default strategy for merging PR
Usually configurableg
4 . 10
21. Merge StrategyMerge Strategy
Log Branch Graph
Fast Forward Clean Less intuitive
No Fast Forward Merge commit More intuitive
4 . 11
22. Merge ConflictsMerge Conflicts
Manual intervention is required.
After self correction is done; add and commit the
conflict files.
$ git add index.html
$ git commit
4 . 12
23. Rebase IRebase I
Moving a branch onto a new base commit
Rebasing onto master will take all commits from
master and apply your branch commits on top one by
one.
Later could combine with merge to master
4 . 13
24. Rebase IIRebase II
If combined with squash, there will possibly be only
one commit.
4 . 14
25. Squash ISquash I
squash is a mechanism to consolidate multiple
commit into one.
It comes with rebase / merge command
4 . 15
29. Cherry PickCherry Pick
Given one or more existing commits, apply the change
each one introduces, recording a new commit for
each.
$ git cherry-pick C2 C4
4 . 19
30. TagTag
Annotation to represent release
Create a tag
List all tags
$ git tag -a <tag-name> -m 'description'
$ git push <remote-repo> <tag-name>
$ git tag -l
Show tag metadata
$ git show <tag-name>
Delete a tag
$ git tag -d <tag-name>
$ git push <remote-repo> <tag-name>
4 . 20
32. StashStash
How to save unfinished implementation temporarily?
Save the current state of work.
# Save current work, and rollback to HEAD keep current work space clean
$ git stash -u
# Reload latest stash record
$ git stash pop
# List stash record
$ git stash list
5 . 2
33. Undo Commit IUndo Commit I
$ git reset --soft HEAD~1
$ git diff --cached
$ git reset HEAD~1
$ git diff
$ git reset --hard HEAD~1
Reset current HEAD to the specific state
Changes are UNSTAGED
Changes are STAGED
Changes are REMOVED from log
5 . 3
34. Undo Commit IIUndo Commit II
$ git checkout path/to/filename
$ git revert commit-hash
Reverts a commit with another commit
Restores the file to the latest version
in HEAD, changes are dropped
5 . 4
35. Reference LogReference Log
Historical records when the tips of branches and other
references were updated in the LOCAL repository.
Helpful when you want to look up historical commit
$ git reflog
Maybe your last resort to undo bad commit
Use at your RISK!
$ git reset --hard HEAD@{n}
5 . 5
36. Bash Alias IBash Alias I
# Nice format for `git log`
alias git.log="git log --graph --abbrev-commit --decorate --
format=format:'%C(bold blue)%h%C(reset) - %C(bold
cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset) %C(bold cyan)
(committed: %cD)%C(reset) %C(auto)%d%C(reset)%n''
%C(white)%s%C(reset)%n'' %C(dim white)- %an <%ae>
%C(reset) %C(dim white)(committer: %cn <%ce>)%C(reset)' --all"
* 059806c - Fri, 9 Nov 2018 17:31:31 +0800 (2 days ago) (committed: Fri, 9 Nov 2018 17:31:31 +080
| Add unit test to ByteBuf
| - Michael Fong <mcfong.open@gmail.com> (committer: Michael Fong <mcfong.open@gmail.c
| * f5832fa - Fri, 9 Nov 2018 12:35:40 +0800 (2 days ago) (committed: Fri, 9 Nov 2018 12:35:40 +0
| | squash
| | - Michael Fong <mcfong.open@gmail.com> (committer: Michael Fong <mcfong.open@gmail
| * f5f556a - Tue, 6 Nov 2018 19:54:47 +0800 (5 days ago) (committed: Thu, 8 Nov 2018 20:49:30 +0
|/ Add fadvise64
| - Michael Fong <mcfong.open@gmail.com> (committer: Michael Fong <mcfong.open@gmail
* 91e622c - Thu, 8 Nov 2018 20:26:33 +0800 (3 days ago) (committed: Thu, 8 Nov 2018 20:42:14 +080
| Copy libnative-utility.so to target/classes for java-jni
| - Michael Fong <mcfong.open@gmail.com> (committer: Michael Fong <mcfong.open@gmail.c
* 794bf26 - Wed, 7 Nov 2018 16:18:03 +0800 (4 days ago) (committed: Thu, 8 Nov 2018 00:45:32 +080
5 . 6
41. Branching ModelsBranching Models
master - ONE true MASTER
develop
feature
Feature branches typically exist in
developer repos only, not in origin
release
Release branches support preparation
of a new production release.
hot-fix
When a critical bug in a production
version must be resolved
immediately, a hotfix branch may be
branched off from the corresponding
tag on the master branch that marks
the production version.
6 . 4