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

Git

  • 1.
  • 2.
    Version Control System VCSis 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
  • 4.
    History of git In2005, 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 aboutgit 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 configurationfiles 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 "[email protected]" git config --global core.editor emacs 'git help config' for more info
  • 8.
    Begin git Initialize agit 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 Addfiles 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 Viewcommit 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 Killerfeature 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 twokinds 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 Modifythe 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="[email protected]"; 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 theair 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 filesin 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-Managerworkflow 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 aboutgit Pro Git: progit.org git community book: book.git-scm.com github.com http://www.ghostunix.org/blog
  • 18.