3. Overview
Version control is a system that records changes to a file or set of files
over time so that you can recall specific versions later. A VCS allows
you to:
●
●
●
●
revert files back to a previous state
revert the entire project back to a previous state
review changes made over time
see who last modified something
7. History of Git
Linux Kernel was use DVCS call BitKeeper, proprietary
software but in 2005 the company behind BitKeeper brokedown and tool’s free-of-charge was revoked. The Linux
development community develop their own tool.
8. Goal of Git
● Speed
● Simple design
● Strong support for non-linear development (1000+ for
parallel branches)
● Fully distributed
● Able handle a large project (data and size)
12. Git config
The git config command lets you configure your Git
installation (or an individual repository) from the command
line.
13. Git config user.name
Define the author name to be used for all commits in the
current repository. Typically, you’ll want to use the -global flag to set configuration options for the current
user.
git config --global user.name <name>
14. Git config user.email
Define the author email to be used for all commits by the
current user.
git config --global user.email <email>
15. Git config core.editor
Define the text editor used by commands like git commit for
all users on the current machine. eg; vi
git config --system core.editor <editor>
16. Git config core.autocrlf
Config the line ending for your operating system
git config --global core.autocrlf true
git config --global core.autocrlf input
22. Workshop 3
Create git repositories call myrepo as working repository
and master-proj as shared repository, see what is
difference of two repository.
23. Git clone
The git clone command copies an existing Git repository.
git clone <repo>
git clone <repo> <directory>
Origin
Working Copy
24. Collaboration
Central Repo to Working Copy
Collaboration
Repo to Repo Collaboration
SVN
Repo
Git
Repo
Git
Repo
Git
Repo
42. Workshop 9
Add 2 lines in in readme.txt and commit change to your
repository, check revision form git log and try to change to
the first state and try to change state back to the latest
revision (master)
43. Git revert
The git revert command undoes a committed snapshot. it
the ‘safe’ way to undo.
git revert <commit>
45. Workshop 10
Add 1 lines in in readme.txt and commit change to your
repository, check revision form git log and try to revert to
previous version by using git revert
46. Git reset
The git reset command undo changes in dangerous
method — it is a permanent undo.
git reset <file>
git reset <commit>
git reset --hard
git reset --hard <commit>
48. Workshop 11
Add 1 lines in in readme.txt and commit change to your
repository, check revision form git log and try to revert to
previous version by using git reset
49. Git clean
The git clean command removes untracked files from your
working directory.
git clean
git clean -f
git clean -df
git clean -xf
55. Workshop 12
Create new branch call issue123 for your current revision.
use git branch to show branches.
56. Git checkout
The git checkout command lets you navigate between the
branches created by git branch.
git checkout <existing-branch>
git checkout -b <new-branch>
61. Workshop 14
Change to branch issue123 edit some file and commit
then change to master branch and merge issue123 to
master branch then delete issue123 branch.
62. Rewriting Git History
Git also designed to give you total control over your
development workflow, letting you define exactly what your
project history looks like; however, it also creates the
potential to lose commits.
63. Git rebase
Rebasing is the process of moving a branch to a new base
commit.
git rebase <base>
65. Example
# Start a new feature
git checkout -b feature master
# Edit files
git commit -a -m "Start developing a feature"
git checkout master
# Edit files
git commit -a -m "Fix security hole"
66. Example
# Merge back into master
git checkout feature
git rebase master
# Merge back into master
git checkout master
git merge feature
67. Git rebase -i
Running git rebase with the -i flag begins an interactive
rebasing session.
git rebase -i <base>
68. Example
# Start a new feature
git checkout -b feature master
# Edit files
git commit -a -m "Start developing a feature"
git checkout master
# Edit files
git commit -a -m "Fix security hole"
69. Example
# Merge back into master
git checkout feature
git rebase -i master
# Config merge method in change log
# Merge back into master
git checkout master
git merge feature
70. Workshop 15
● Initial git repo
○ add blank readme.txt and index.php
● Branch new feature from master call feature then
○ update index.php with some function
● Change branch to master
○ update readme.txt
● Change branch to feature
○ merge changes from base
● Change branch to master
○ merge feature to master
71. Git reflog
Git keeps track of updates to the tip of branches using a
mechanism called reflog.
git reflog
git reflog --relative-date
72. Example
git reflog
12aeb71 HEAD@{0}: merge feature: Fast-forward
df6c71f HEAD@{1}: reset: moving to df6c71f
12aeb71 HEAD@{2}: merge feature: Fast-forward
df6c71f HEAD@{3}: checkout: moving from feature to master
73. Example
git reflog --relative-date
12aeb71 HEAD@{9 minutes ago}: merge feature: Fast-forward
df6c71f HEAD@{11 minutes ago}: reset: moving to df6c71f
12aeb71 HEAD@{13 minutes ago}: merge feature: Fast-forward
df6c71f HEAD@{14 minutes ago}: checkout: moving from
feature to master
75. Remote Repositories
Git’s collaboration model, which gives every developer their
own copy of the repository, complete with its own local
history and branch structure. Instead of committing a
changeset from a working copy to the central repository, Git
lets you share entire branches between repositories.
76. Quick Git Server (1)
# install ssh server and git
sudo apt-get install openssh-server
sudo apt-get install git
# add git user for all users access
sudo adduser --group sudo git
# then import user’s public key to .ssh/authorized_keys
sudo su git
cd
mkdir .ssh
cat id_rsa.john.pub >> .ssh/authorized_keys
77. Quick Git Server (2)
# Make git repo dir at /git for easy access
sudo mkdir /git
sudo chown git:git /git
# Make share repo called project.git
sudo su git
cd /git
git init --bare project.git
# Disable git user to ssh to server
sudo usermod -s /usr/bin/git-shell git
78. Workshop 17
● Setup git server add following users (or your team)
○ John
○ Jessie
○ Jessica
● Create share project called webapps
79. Git remote
The git remote command lets you create, view, and delete
connections to other repositories.
git remote
git remote add <name> <url>
origin
Central
Repo
git remote rm <name>
John’s
Repo
Jess’s
Repo
80. Example
# on John’s computer
cd myproject
git init
git add .
git commit -m 'initial commit'
git remote add origin git@gitserver:/git/project.git
git push origin master
81. Example
# on John’s computer
git clone git@gitserver:/git/project.git myproject
cd myproject
touch README
git add README
git commit -am “add readme”
git push origin master
82. Workshop 18
● Lets John first commit following files to repository
○ README
○ LICENSE
○ CONTRIBUTOR
● Another users clone repository to their machine
83. Git fetch
The git fetch command imports commits from a remote
repository into your local repo.
git fetch <remote>
git fetch <remote> <branch>
84. Example
cd myproject
git init
git remote add origin git@gitserver:/git/project.git
git fetch origin
git checkout master
git merge origin/master
85. Workshop 19
● Lets Jessica create feature branch and commit to
repository
● Another users fetch feature branch from repository to
their machine
86. Git pull
The git pull command merge upstream changes into your
local repository, it’s combined git fetch and git merge in
single command.
git pull <remote>
git pull --rebase <remote>
88. Workshop 20
Create working repository in machine and add remote
repository and use git pull to merge remote repository to
working repository.
89. Git push
The git pull command merge upstream changes into your
local repository, it’s combined git fetch and git merge in
single command.
git push
git push <remote>
90. Example
echo “this is a readme” > README
git commit -am “update readme”
git push origin master
origin
John’s
Repo
Central
Repo
Jess’s
Repo
91. Workshop 21
Create working repository in machine and add remote
repository and use git pull to merge remote repository to
working repository. Edit some file and push to remote
repository
92. Git Workflows
The possible workflows can make it hard to know where to
begin when implementing Git in the workplace. These
workflows are designed to be guidelines rather than
concrete rules.
93. Centralized Workflow
The Centralized Workflow uses a
central repository to serve as the
single point-of-entry for all changes to
the project. Instead of trunk, the
default development branch is called
master and all changes are committed
into this branch.
96. Example
# John done his feature
git status
git add
git commit
git push origin master
97. Example
# Jessie try to push her feature
git push origin master
# Git will refuse the request with a rather
verbose error message. She needs to pull
John’s updates into her repository, integrate
them with her local changes, and then try
again.
99. Example
# Jessie and John working on relate feature
rebasing process will generate conflicts
git status
git add <somefile>
git rebase --continue
# If you get to this point and you have no idea what’
s going on, don’t panic. Just execute following
command and you’ll be right back.
git rebase --abort
101. Workshop 22
●
●
●
●
Group 2-3 people
Setup your team git repository server.
Use centralized workflow to fix code in sample application.
Sample application can pull from https://github.
com/anoochit/git-workshop.git
102. Feature Branch Workflow
the Centralized Workflow, adding feature branches to your
development process is an easy way to encourage
collaboration and streamline communication between
developers.
108. Example
# John is ready to accept the pull request,
someone needs to merge the feature into the
stable project (this can be done by either John
or Jessie):
git checkout master
git pull
git pull origin jessie-feature
git push
109. Workshop 23
●
●
●
●
Group 2-3 people
Setup your team git repository server.
Use feature branch workflow to fix code in sample application.
Sample application can pull from https://github.
com/anoochit/git-workshop.git
110. Git Flow Workflow
The Gitflow Workflow defines a strict branching model
designed around the project release. While somewhat more
complicated than the Feature Branch Workflow, this
provides a robust framework for managing larger projects.
113. Example
# Jessie create development branch from master
git checkout -b develop master
git push -u origin developer
114. Example
# John add new feature
git checkout -b feature developer
git status
git add <file>
git commit
115. Example
# John ready to merge feature to development
git pull origin developer
git checkout developer
git merge feature
git push
git branch -d feature
116. Example
# Jessie prepare release
git checkout -b release-1.0 develop
git checkout master
git merge release-1.0
git tag -a 0.1 -m "Initial public release" master
git push --tags
git branch -d release-1.0
117. Example
# Discover some bugs
git checkout -b issue-123 master
# Fix some bugs
git checkout master
git merge issue-123
git push
git branch -d issue-123
118. Workshop 24
●
●
●
●
Group 2-3 people
Setup your team git repository server.
Use git flow workflow to fix code in sample application.
Sample application can pull from https://github.
com/anoochit/git-workshop.git
119. Forking Workflow
The Forking Workflow is fundamentally different than the other
workflows Instead of using a single server-side repository to act as the
“central” codebase, it gives every developer a server-side repository.
Each contributor has not one, but two Git repositories: a private local
and a public server-side.
126. Example
Project maintainer integrate their feature
1. Inspect the code
2. Pull code in local repository and
manually merge
git fetch user@gitserver/user/repo.git feature
# Inspect code
git checkout master
git merge FETCH_HEAD
127. Example
Developers synchronized with official
repository, the main codebase has moved
forward, other developers should
synchronize with the official repository
git pull upstream master
128. Workshop 25
●
●
●
●
Group 2-3 people
Setup your own git repository servers.
Use forking workflow to fix code in sample application.
Sample application can pull from https://github.
com/anoochit/git-workshop.git
129. Workshop 26
Discussion in your developer team, choose workflow or mix and match
your developer team. Present your idea, workflow to another groups