18

Click here to load reader

Git 入门与实践

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Git 入门与实践

git入门与实践

ghosTM55

Page 2: Git 入门与实践

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 projectsLocal Version Control SystemCentralized Version Control System

SVNCVS

Distributed Version Control SystemgitbazaarBitKeeper

Page 3: Git 入门与实践
Page 4: Git 入门与实践

History of git

In 2005, Linux kernel team can't use BitKeeper free of charge any moreSo Linus and other guys designed a brand new distributed version control system, called gitInspired by some good features in BitKeeper, git does dvcs better, especially in speed, it's f***ing fast

Page 5: Git 入门与实践

Basic concepts about gitgit 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 projectsAnd of cuz, it's fast due to there is no latency for local operationsgit use 40-bit SHA1-HASH to record everything in its repoFiles in git repo may have 4 kind of status

UntrackedStaged CommittedModified

git repo is generally made up by three partsWorking directory(git-repo/)git directory(git-repo/.git/)Staging area(a file in git directory)

Page 6: Git 入门与实践

Install gitdebian, ubuntu ...

aptitude install git-corefedora, centos ...

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-develtar xvf git-xxx.tar.gz && cd git-xxx && make all && make installyum install git-core(use epel repo maintained by fedora project)

gentooemerge git-core

Mac OS Xget macports && port install git-core

Page 7: Git 入门与实践

git configuration

Three configuration files using in git/etc/gitconfig: system-wide configuration~/.gitconfig: user configurationgit-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

Page 8: Git 入门与实践

Begin git

Initialize a git repomkdir git-repo-name && cd git-repo-name && git initmkdir 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-urlgit clone git-repo-url dir-name

github.com

Page 9: Git 入门与实践

Start to work

Add files to be tracked by git: git add filenameunstage a file: git reset HEAD filename Checkout the current git status: git statusCommit your work: git commit filename [-a] [-m]Modify your work and see what happeneddiff utility in git: git diffrename a file tracked by git: git mv filename_a filename_bremove a file tracked by git: git rm filename

Page 10: Git 入门与实践

View commit historiesView commit histories: git logView the last N commit logs: git log -NView logs one line per commit log: git log --pretty=onelineView the branch ASCII graph in log: git log --graphCustomize 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

Page 11: Git 入门与实践

branch, branch, branchKiller feature in gitBranch is actually a pointer point to a certain commit .git use HEAD pointer to determine which branch you're working inCreate a git branch: git branch branch_nameSwitch to a certain branch: git checkout branch_nameDelete a branch: git branch -d branch_nameView details about the current branch: git branch -vCheckout a remote branch: git branch repo_name/branch_nameMerge the works: git merge branch_name

Take care of the conflicts if they exists when you merge git rebasegit merge vs. git rebase

http://gitguru.com/2009/02/03/rebase-v-merge-in-git/

Page 12: Git 入门与实践

MilestonesThere are two kinds of tags in git

Annotated tagLightweight tag

Create an annotated tag: git tag -a tag_ver -m 'comment'You can sign GPG key in an annotated tagAnnotated tag is a commit

Create a lightweight tag: git tag tag_verView details about a tag: git show tag_verPush your tags to the repo server: git push repo_name --tags

Page 13: Git 入门与实践

Be a victorModify the last commit: git commit --amendNu-clear option: filter-branch

git filter-branch --tree-filter 'modify script(in bash)' HEADgit 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-valuegit reset --hard commit-SHA1-value

Page 14: Git 入门与实践

git on the airDefault remote is called originDefault branch is called masterFetch 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 protocolgit clone ssh://user@host:/path/to/git-repo

Page 15: Git 入门与实践

MiscIgnore certain files in git working directory: .gitignoregit command alias: git config --global alias.alias_name 'command_name'Stashing

Create a stash: git stash Apply a stash: git stash applyCreate a stash branch: git stash branch branch_name

Apply the patches directly in git: git apply patch_fileAnother killer feature: git cherry-pick

git cherry-pick --helpmerge specified commit to a branch with cherry-pick and rebase

Migration from SVN: git svn clone svn-repo-url

Page 16: Git 入门与实践

git workflowsCentralized workflowIntegration-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 workflow1. 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.

Page 17: Git 入门与实践

More resources about git

Pro Git: progit.orggit community book: book.git-scm.comgithub.comhttp://www.ghostunix.org/blog

Page 18: Git 入门与实践

Start using it!

Thank you