SlideShare a Scribd company logo
1 of 18
Download to read offline
git入门与实践
ghosTM55
Version Control System
VCS is a kind of system designed to record the changes of
files under control in certain directory
A.K.A Revision Control System
Mainly used in development projects
Local Version Control System
Centralized Version Control System
SVN
CVS
Distributed Version Control System
git
bazaar
BitKeeper
History of git
In 2005, Linux kernel team can't use BitKeeper free of
charge any more
So Linus and other guys designed a brand new distributed
version control system, called git
Inspired by some good features in BitKeeper, git does dvcs
better, especially in speed, it's f***ing fast
Basic concepts about git
git record the stage status and files via snapshot, not like other VCS record
the diff to the former commit .
Nearly all the operations in git can be done locally, so it's more friendly to
the people who can't use network when they want to work on their projects
And of cuz, it's fast due to there is no latency for local operations
git use 40-bit SHA1-HASH to record everything in its repo
Files in git repo may have 4 kind of status
Untracked
Staged
Committed
Modified
git repo is generally made up by three parts
Working directory(git-repo/)
git directory(git-repo/.git/)
Staging area(a file in git directory)
Install git
debian, ubuntu ...
aptitude install git-core
fedora, centos ...
yum install curl-devel expat-devel gettext-devel openssl-
devel zlib-devel
tar xvf git-xxx.tar.gz && cd git-xxx && make all && make
install
yum install git-core(use epel repo maintained by fedora
project)
gentoo
emerge git-core
Mac OS X
get macports && port install git-core
git configuration
Three configuration files using in git
/etc/gitconfig: system-wide configuration
~/.gitconfig: user configuration
git-repo/.git/config:project configuration
git config --global user.name "ghosTM55"
git config --global user.emal "thomas@shlug.org"
git config --global core.editor emacs
'git help config' for more info
Begin git
Initialize a git repo
mkdir git-repo-name && cd git-repo-name && git init
mkdir git-repo.git && cd git-repo.git && git init --bare
Get a git repo from internet (or from other directories local)
git clone git-repo-url
git clone git-repo-url dir-name
github.com
Start to work
Add files to be tracked by git: git add filename
unstage a file: git reset HEAD filename
Checkout the current git status: git status
Commit your work: git commit filename [-a] [-m]
Modify your work and see what happened
diff utility in git: git diff
rename a file tracked by git: git mv filename_a filename_b
remove a file tracked by git: git rm filename
View commit histories
View commit histories: git log
View the last N commit logs: git log -N
View logs one line per commit log: git log --pretty=oneline
View the branch ASCII graph in log: git log --graph
Customize the output format: git log --pretty=format:"format_arguments"
%H Commit hash
%h Abbreviated commit hash
%T Tree hash
%t Abbreviated tree hash
%P Parent hashes
%p Abbreviated parent hashes
%an Author name
%ae Author e-mail
%ad Author date (format respects the date= option)
%ar Author date, relative
%cn Committer name
%ce Committer email
%cd Committer date
%cr Committer date, relative
%s Subject
branch, branch, branch
Killer feature in git
Branch is actually a pointer point to a certain commit .
git use HEAD pointer to determine which branch you're working in
Create a git branch: git branch branch_name
Switch to a certain branch: git checkout branch_name
Delete a branch: git branch -d branch_name
View details about the current branch: git branch -v
Checkout a remote branch: git branch repo_name/branch_name
Merge the works: git merge branch_name
Take care of the conflicts if they exists when you merge
git rebase
git merge vs. git rebase
http://gitguru.com/2009/02/03/rebase-v-merge-in-git/
Milestones
There are two kinds of tags in git
Annotated tag
Lightweight tag
Create an annotated tag: git tag -a tag_ver -m 'comment'
You can sign GPG key in an annotated tag
Annotated tag is a commit
Create a lightweight tag: git tag tag_ver
View details about a tag: git show tag_ver
Push your tags to the repo server: git push repo_name --
tags
Be a victor
Modify the last commit: git commit --amend
Nu-clear option: filter-branch
git filter-branch --tree-filter 'modify script(in bash)' HEAD
git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ];
then
GIT_AUTHOR_NAME="Scott Chacon";
GIT_AUTHOR_EMAIL="schacon@example.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
Time machine in git:
git reset --soft commit-SHA1-value
git reset --hard commit-SHA1-value
git on the air
Default remote is called origin
Default branch is called master
Fetch the work on a certain remote repo:
git fetch remote_name [branch_name]
git pull remote_name [branch_name]
Push your work:
git push remote_name branch_name
People mostly use ssh as the git protocol
git clone ssh://user@host:/path/to/git-repo
Misc
Ignore certain files in git working directory: .gitignore
git command alias: git config --global alias.alias_name
'command_name'
Stashing
Create a stash: git stash
Apply a stash: git stash apply
Create a stash branch: git stash branch branch_name
Apply the patches directly in git: git apply patch_file
Another killer feature: git cherry-pick
git cherry-pick --help
merge specified commit to a branch with cherry-pick and
rebase
Migration from SVN: git svn clone svn-repo-url
git workflows
Centralized workflow
Integration-Manager workflow
1. The project maintainer pushes to their public repository.
2. A contributor clones that repository and makes changes.
3. The contributor pushes to their own public copy.
4. The contributor sends the maintainer an e-mail asking them to pull
changes.
5. The maintainer adds the contributor’s repo as a remote and merges
locally.
6. The maintainer pushes merged changes to the main repository.
Dictator and Lieutenants workflow
1. Regular developers work on their topic branch and rebase their work
on top of master. The master branch is that of the dictator.
2. Lieutenants merge the developers’ topic branches into their master
branch.
3. The dictator merges the lieutenants’ master branches into the
dictator’s master branch.
4. The dictator pushes their master to the reference repository so the
other developers can rebase on it.
More resources about git
Pro Git: progit.org
git community book: book.git-scm.com
github.com
http://www.ghostunix.org/blog
Start using it!
Thank you

More Related Content

What's hot

Version Control Systems with git (and github) as an example
Version Control Systems with git (and github) as an exampleVersion Control Systems with git (and github) as an example
Version Control Systems with git (and github) as an exampleGaurav Kumar Garg
 
Advanced Git Presentation By Swawibe
Advanced Git Presentation By SwawibeAdvanced Git Presentation By Swawibe
Advanced Git Presentation By SwawibeMd Swawibe Ul Alam
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubDSC GVP
 
Github git-cheat-sheet
Github git-cheat-sheetGithub git-cheat-sheet
Github git-cheat-sheetAbdul Basit
 
Tài liệu sử dụng GitHub
Tài liệu sử dụng GitHubTài liệu sử dụng GitHub
Tài liệu sử dụng GitHubviet nghiem
 
Git the fast version control system
Git the fast version control systemGit the fast version control system
Git the fast version control systemJeroen Rosenberg
 
Git Developer Cheatsheet
Git Developer CheatsheetGit Developer Cheatsheet
Git Developer CheatsheetAbdul Basit
 
The everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitThe everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitE Carter
 

What's hot (19)

Version Control Systems with git (and github) as an example
Version Control Systems with git (and github) as an exampleVersion Control Systems with git (and github) as an example
Version Control Systems with git (and github) as an example
 
Git in 5 Minutes
Git in 5 MinutesGit in 5 Minutes
Git in 5 Minutes
 
Techoalien git
Techoalien gitTechoalien git
Techoalien git
 
Advanced Git Presentation By Swawibe
Advanced Git Presentation By SwawibeAdvanced Git Presentation By Swawibe
Advanced Git Presentation By Swawibe
 
Git for beginners
Git for beginnersGit for beginners
Git for beginners
 
Git 101
Git 101Git 101
Git 101
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHub
 
Git commands
Git commandsGit commands
Git commands
 
Github git-cheat-sheet
Github git-cheat-sheetGithub git-cheat-sheet
Github git-cheat-sheet
 
Tài liệu sử dụng GitHub
Tài liệu sử dụng GitHubTài liệu sử dụng GitHub
Tài liệu sử dụng GitHub
 
Git the fast version control system
Git the fast version control systemGit the fast version control system
Git the fast version control system
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
 
Git basics
Git basicsGit basics
Git basics
 
Git
GitGit
Git
 
Git
GitGit
Git
 
Git Developer Cheatsheet
Git Developer CheatsheetGit Developer Cheatsheet
Git Developer Cheatsheet
 
The everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitThe everyday developer's guide to version control with Git
The everyday developer's guide to version control with Git
 
Git presentation
Git presentationGit presentation
Git presentation
 
Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
 

Viewers also liked

Book Report - Foxtrot
 Book Report - Foxtrot Book Report - Foxtrot
Book Report - FoxtrotCraig Nansen
 
Staff development Thesis Summary
Staff development Thesis SummaryStaff development Thesis Summary
Staff development Thesis SummaryCraig Nansen
 
49 Cach Song Khoe
49 Cach Song Khoe49 Cach Song Khoe
49 Cach Song KhoeTom Doan
 
Calvin and Hobbes - Learning is Fun
Calvin and Hobbes - Learning is FunCalvin and Hobbes - Learning is Fun
Calvin and Hobbes - Learning is FunCraig Nansen
 
Get the Facts: Oracle's Unbreakable Enterprise Kernel
Get the Facts: Oracle's Unbreakable Enterprise KernelGet the Facts: Oracle's Unbreakable Enterprise Kernel
Get the Facts: Oracle's Unbreakable Enterprise KernelTerry Wang
 
Git 入门 与 实践
Git 入门 与 实践Git 入门 与 实践
Git 入门 与 实践Terry Wang
 

Viewers also liked (9)

ALUI 6.5
ALUI 6.5ALUI 6.5
ALUI 6.5
 
Book Report - Foxtrot
 Book Report - Foxtrot Book Report - Foxtrot
Book Report - Foxtrot
 
Staff development Thesis Summary
Staff development Thesis SummaryStaff development Thesis Summary
Staff development Thesis Summary
 
49 Cach Song Khoe
49 Cach Song Khoe49 Cach Song Khoe
49 Cach Song Khoe
 
Calvin and Hobbes - Learning is Fun
Calvin and Hobbes - Learning is FunCalvin and Hobbes - Learning is Fun
Calvin and Hobbes - Learning is Fun
 
Get the Facts: Oracle's Unbreakable Enterprise Kernel
Get the Facts: Oracle's Unbreakable Enterprise KernelGet the Facts: Oracle's Unbreakable Enterprise Kernel
Get the Facts: Oracle's Unbreakable Enterprise Kernel
 
VP07Escape Workshop Tilburg
VP07Escape Workshop TilburgVP07Escape Workshop Tilburg
VP07Escape Workshop Tilburg
 
Git 入门 与 实践
Git 入门 与 实践Git 入门 与 实践
Git 入门 与 实践
 
Git
GitGit
Git
 

Similar to Git 入门与实践

Introduction to Git and Github
Introduction to Git and Github Introduction to Git and Github
Introduction to Git and Github Max Claus Nunes
 
Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?9 series
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git TutorialSage Sharp
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | WorkshopAnuchit Chalothorn
 
Git Workshop : Getting Started
Git Workshop : Getting StartedGit Workshop : Getting Started
Git Workshop : Getting StartedWildan Maulana
 
Intro to Git DevOps Tally Presentation 101615
Intro to Git DevOps Tally Presentation 101615Intro to Git DevOps Tally Presentation 101615
Intro to Git DevOps Tally Presentation 101615Brian K. Vagnini
 
Git workflows automat-it
Git workflows automat-itGit workflows automat-it
Git workflows automat-itAutomat-IT
 
Improving your workflow with git
Improving your workflow with gitImproving your workflow with git
Improving your workflow with gitDídac Ríos
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshopthemystic_ca
 

Similar to Git 入门与实践 (20)

Introduction to Git and Github
Introduction to Git and Github Introduction to Git and Github
Introduction to Git and Github
 
Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?Git Commands Every Developer Should Know?
Git Commands Every Developer Should Know?
 
Git basics for beginners
Git basics for beginnersGit basics for beginners
Git basics for beginners
 
390a gitintro 12au
390a gitintro 12au390a gitintro 12au
390a gitintro 12au
 
sample.pptx
sample.pptxsample.pptx
sample.pptx
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git Tutorial
 
Git Tech Talk
Git  Tech TalkGit  Tech Talk
Git Tech Talk
 
Collaborative development with Git | Workshop
Collaborative development with Git | WorkshopCollaborative development with Git | Workshop
Collaborative development with Git | Workshop
 
git2.ppt
git2.pptgit2.ppt
git2.ppt
 
Subversion to Git Migration
Subversion to Git MigrationSubversion to Git Migration
Subversion to Git Migration
 
Git Workshop : Getting Started
Git Workshop : Getting StartedGit Workshop : Getting Started
Git Workshop : Getting Started
 
Git github
Git githubGit github
Git github
 
git2.ppt
git2.pptgit2.ppt
git2.ppt
 
Intro to Git DevOps Tally Presentation 101615
Intro to Git DevOps Tally Presentation 101615Intro to Git DevOps Tally Presentation 101615
Intro to Git DevOps Tally Presentation 101615
 
Git workflows automat-it
Git workflows automat-itGit workflows automat-it
Git workflows automat-it
 
git.ppt.pdf
git.ppt.pdfgit.ppt.pdf
git.ppt.pdf
 
Improving your workflow with git
Improving your workflow with gitImproving your workflow with git
Improving your workflow with git
 
GDSC GIT AND GITHUB
GDSC GIT AND GITHUB GDSC GIT AND GITHUB
GDSC GIT AND GITHUB
 
GIT.pptx
GIT.pptxGIT.pptx
GIT.pptx
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
 

More from Terry Wang

Introduction to containers, k8s, Microservices & Cloud Native
Introduction to containers, k8s, Microservices & Cloud NativeIntroduction to containers, k8s, Microservices & Cloud Native
Introduction to containers, k8s, Microservices & Cloud NativeTerry Wang
 
Debugging and Configuration Best Practices for Oracle Linux
Debugging and Configuration Best Practices for Oracle LinuxDebugging and Configuration Best Practices for Oracle Linux
Debugging and Configuration Best Practices for Oracle LinuxTerry Wang
 
Btrfs by Chris Mason
Btrfs by Chris MasonBtrfs by Chris Mason
Btrfs by Chris MasonTerry Wang
 
Oracle Linux Nov 2011 Webcast
Oracle Linux Nov 2011 WebcastOracle Linux Nov 2011 Webcast
Oracle Linux Nov 2011 WebcastTerry Wang
 
Oracle Buys Ksplice
Oracle Buys KspliceOracle Buys Ksplice
Oracle Buys KspliceTerry Wang
 
Eliminating Silent Data Corruption with Oracle Linux
Eliminating Silent Data Corruption with Oracle Linux Eliminating Silent Data Corruption with Oracle Linux
Eliminating Silent Data Corruption with Oracle Linux Terry Wang
 
我的 Ubuntu 之旅
我的 Ubuntu 之旅我的 Ubuntu 之旅
我的 Ubuntu 之旅Terry Wang
 
Git 101 tutorial presentation
Git 101 tutorial presentationGit 101 tutorial presentation
Git 101 tutorial presentationTerry Wang
 
WCI 10gR3 overview
WCI 10gR3 overviewWCI 10gR3 overview
WCI 10gR3 overviewTerry Wang
 

More from Terry Wang (10)

Introduction to containers, k8s, Microservices & Cloud Native
Introduction to containers, k8s, Microservices & Cloud NativeIntroduction to containers, k8s, Microservices & Cloud Native
Introduction to containers, k8s, Microservices & Cloud Native
 
Debugging and Configuration Best Practices for Oracle Linux
Debugging and Configuration Best Practices for Oracle LinuxDebugging and Configuration Best Practices for Oracle Linux
Debugging and Configuration Best Practices for Oracle Linux
 
Btrfs by Chris Mason
Btrfs by Chris MasonBtrfs by Chris Mason
Btrfs by Chris Mason
 
Oracle Linux Nov 2011 Webcast
Oracle Linux Nov 2011 WebcastOracle Linux Nov 2011 Webcast
Oracle Linux Nov 2011 Webcast
 
RHEL roadmap
RHEL roadmapRHEL roadmap
RHEL roadmap
 
Oracle Buys Ksplice
Oracle Buys KspliceOracle Buys Ksplice
Oracle Buys Ksplice
 
Eliminating Silent Data Corruption with Oracle Linux
Eliminating Silent Data Corruption with Oracle Linux Eliminating Silent Data Corruption with Oracle Linux
Eliminating Silent Data Corruption with Oracle Linux
 
我的 Ubuntu 之旅
我的 Ubuntu 之旅我的 Ubuntu 之旅
我的 Ubuntu 之旅
 
Git 101 tutorial presentation
Git 101 tutorial presentationGit 101 tutorial presentation
Git 101 tutorial presentation
 
WCI 10gR3 overview
WCI 10gR3 overviewWCI 10gR3 overview
WCI 10gR3 overview
 

Git 入门与实践

  • 2. Version Control System VCS is a kind of system designed to record the changes of files under control in certain directory A.K.A Revision Control System Mainly used in development projects Local Version Control System Centralized Version Control System SVN CVS Distributed Version Control System git bazaar BitKeeper
  • 3.
  • 4. History of git In 2005, Linux kernel team can't use BitKeeper free of charge any more So Linus and other guys designed a brand new distributed version control system, called git Inspired by some good features in BitKeeper, git does dvcs better, especially in speed, it's f***ing fast
  • 5. Basic concepts about git git record the stage status and files via snapshot, not like other VCS record the diff to the former commit . Nearly all the operations in git can be done locally, so it's more friendly to the people who can't use network when they want to work on their projects And of cuz, it's fast due to there is no latency for local operations git use 40-bit SHA1-HASH to record everything in its repo Files in git repo may have 4 kind of status Untracked Staged Committed Modified git repo is generally made up by three parts Working directory(git-repo/) git directory(git-repo/.git/) Staging area(a file in git directory)
  • 6. Install git debian, ubuntu ... aptitude install git-core fedora, centos ... yum install curl-devel expat-devel gettext-devel openssl- devel zlib-devel tar xvf git-xxx.tar.gz && cd git-xxx && make all && make install yum install git-core(use epel repo maintained by fedora project) gentoo emerge git-core Mac OS X get macports && port install git-core
  • 7. git configuration Three configuration files using in git /etc/gitconfig: system-wide configuration ~/.gitconfig: user configuration git-repo/.git/config:project configuration git config --global user.name "ghosTM55" git config --global user.emal "thomas@shlug.org" git config --global core.editor emacs 'git help config' for more info
  • 8. Begin git Initialize a git repo mkdir git-repo-name && cd git-repo-name && git init mkdir git-repo.git && cd git-repo.git && git init --bare Get a git repo from internet (or from other directories local) git clone git-repo-url git clone git-repo-url dir-name github.com
  • 9. Start to work Add files to be tracked by git: git add filename unstage a file: git reset HEAD filename Checkout the current git status: git status Commit your work: git commit filename [-a] [-m] Modify your work and see what happened diff utility in git: git diff rename a file tracked by git: git mv filename_a filename_b remove a file tracked by git: git rm filename
  • 10. View commit histories View commit histories: git log View the last N commit logs: git log -N View logs one line per commit log: git log --pretty=oneline View the branch ASCII graph in log: git log --graph Customize the output format: git log --pretty=format:"format_arguments" %H Commit hash %h Abbreviated commit hash %T Tree hash %t Abbreviated tree hash %P Parent hashes %p Abbreviated parent hashes %an Author name %ae Author e-mail %ad Author date (format respects the date= option) %ar Author date, relative %cn Committer name %ce Committer email %cd Committer date %cr Committer date, relative %s Subject
  • 11. branch, branch, branch Killer feature in git Branch is actually a pointer point to a certain commit . git use HEAD pointer to determine which branch you're working in Create a git branch: git branch branch_name Switch to a certain branch: git checkout branch_name Delete a branch: git branch -d branch_name View details about the current branch: git branch -v Checkout a remote branch: git branch repo_name/branch_name Merge the works: git merge branch_name Take care of the conflicts if they exists when you merge git rebase git merge vs. git rebase http://gitguru.com/2009/02/03/rebase-v-merge-in-git/
  • 12. Milestones There are two kinds of tags in git Annotated tag Lightweight tag Create an annotated tag: git tag -a tag_ver -m 'comment' You can sign GPG key in an annotated tag Annotated tag is a commit Create a lightweight tag: git tag tag_ver View details about a tag: git show tag_ver Push your tags to the repo server: git push repo_name -- tags
  • 13. Be a victor Modify the last commit: git commit --amend Nu-clear option: filter-branch git filter-branch --tree-filter 'modify script(in bash)' HEAD git filter-branch --commit-filter ' if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ]; then GIT_AUTHOR_NAME="Scott Chacon"; GIT_AUTHOR_EMAIL="schacon@example.com"; git commit-tree "$@"; else git commit-tree "$@"; fi' HEAD Time machine in git: git reset --soft commit-SHA1-value git reset --hard commit-SHA1-value
  • 14. git on the air Default remote is called origin Default branch is called master Fetch the work on a certain remote repo: git fetch remote_name [branch_name] git pull remote_name [branch_name] Push your work: git push remote_name branch_name People mostly use ssh as the git protocol git clone ssh://user@host:/path/to/git-repo
  • 15. Misc Ignore certain files in git working directory: .gitignore git command alias: git config --global alias.alias_name 'command_name' Stashing Create a stash: git stash Apply a stash: git stash apply Create a stash branch: git stash branch branch_name Apply the patches directly in git: git apply patch_file Another killer feature: git cherry-pick git cherry-pick --help merge specified commit to a branch with cherry-pick and rebase Migration from SVN: git svn clone svn-repo-url
  • 16. git workflows Centralized workflow Integration-Manager workflow 1. The project maintainer pushes to their public repository. 2. A contributor clones that repository and makes changes. 3. The contributor pushes to their own public copy. 4. The contributor sends the maintainer an e-mail asking them to pull changes. 5. The maintainer adds the contributor’s repo as a remote and merges locally. 6. The maintainer pushes merged changes to the main repository. Dictator and Lieutenants workflow 1. Regular developers work on their topic branch and rebase their work on top of master. The master branch is that of the dictator. 2. Lieutenants merge the developers’ topic branches into their master branch. 3. The dictator merges the lieutenants’ master branches into the dictator’s master branch. 4. The dictator pushes their master to the reference repository so the other developers can rebase on it.
  • 17. More resources about git Pro Git: progit.org git community book: book.git-scm.com github.com http://www.ghostunix.org/blog