Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Git 入门与实践


Published on

  • Be the first to comment

  • Be the first to like this

Git 入门与实践

  1. 1. git入门与实践 ghosTM55
  2. 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. 3. 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
  4. 4. 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)
  5. 5. 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
  6. 6. 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 "ghosTM55" git config --global user.emal "" git config --global core.editor emacs 'git help config' for more info
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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=""; 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
  13. 13. 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
  14. 14. 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
  15. 15. 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.
  16. 16. More resources about git Pro Git: git community book:
  17. 17. Start using it! Thank you