分布式版本管理

Preview:

Citation preview

DVCS分布式版本管理简介

盛大创新院 李骏

调查

• Visual SourceSafe

• CVS/Subversion/TFS/etc.

• Git/Mercurial/Bazaar/etc.

Why?

诚实的回答自己

• 你第一次写出的代码是完美的吗?

Subversion, CVS, etc.

• 中心化源码库• 一个人提交,所有人得到

请选择

• 不提交未充分测试、完成度不高的代码or

• 提交有问题的阶段性成果把所有人逼疯

如果很少提交版本,我们干嘛还用版本管理?

中心化

去中心化 → 分布式

CVS/SVN/etc. Git/Mercurial/etc.

revision changeset

在分布式版本管理中

• 提交新版本 ≠ 交付新代码给别人

• 代码合并(merge)更容易成功

• 所有操作都针对整个代码库

实例

在开始之前。。。

• Mercurial不是SVN加强版

% hgMercurial Distributed SCM

basic commands:

add add the specified files on the next commit annotate show changeset information by line for each file clone make a copy of an existing repository commit commit the specified files or all outstanding changes diff diff repository (or selected files) export dump the header and diffs for one or more changesets forget forget the specified files on the next commit init create a new repository in the given directory log show revision history of entire repository or files merge merge working directory with another revision pull pull changes from the specified source push push changes to the specified destination remove remove the specified files on the next commit serve start stand-alone webserver status show changed files in the working directory summary summarize working directory state update update working directory (or switch revisions)

use "hg help" for the full list of commands or "hg -v" for details

% pwd/Users/neo/Code/Repo/hginit

% lltotal 24drwxr-xr-x 5 neo staff 170 Aug 7 13:13 .drwxr-xr-x 12 neo staff 408 Aug 7 13:11 ..-rw-r--r--@ 1 neo staff 1605 Jun 26 12:23 Info.plist-rw-r--r--@ 1 neo staff 2907 Jun 19 16:18 global.html-rw-r--r-- 1 neo staff 1183 Jun 12 16:22 link.png

% hg init

% lltotal 24drwxr-xr-x 6 neo staff 204 Aug 7 13:14 .drwxr-xr-x 12 neo staff 408 Aug 7 13:11 ..drwxr-xr-x 5 neo staff 170 Aug 7 13:14 .hg-rw-r--r--@ 1 neo staff 1605 Jun 26 12:23 Info.plist-rw-r--r--@ 1 neo staff 2907 Jun 19 16:18 global.html-rw-r--r-- 1 neo staff 1183 Jun 12 16:22 link.png

% hg addadding Info.plistadding global.htmladding link.png

% hg commit

% hg commitInfo.plistglobal.htmllink.pngcommitted changeset 0:0ae94e57c184

% hg logchangeset: 0:0ae94e57c184tag: tipuser: "Neo Lee <neo.lee at gmail.com>"date: Sat Aug 07 14:43:24 2010 +0800files: Info.plist global.html link.pngdescription:Init commit.

% hg commit

% hg logchangeset: 1:dc12cb350527tag: tipuser: "Neo Lee <neo.lee at gmail.com>"date: Sat Aug 07 14:54:57 2010 +0800files: global.htmldescription:Capitalize 'ERROR' prompt.

changeset: 0:0ae94e57c184user: "Neo Lee <neo.lee at gmail.com>"date: Sat Aug 07 14:43:24 2010 +0800files: Info.plist global.html link.pngdescription:Init commit.

% rm Info.plist% rm link.png

% e global.html%

% hg revert --allreverting Info.plistreverting link.pngreverting global.html

标准工作流程

1. 做一些修改(多大规模的修改都好)2. 检验它们是否工作良好3. 如果是,提交(commit)它们4. 如果不是,回退(revert)到前一次提交5. 回到(1)循环

% cp Info.plist Info.plist.1% e Info.plist% rm link.pngremove link.png? y

% hg statusM Info.plist! link.png? Info.plist.1

% hg diff Info.plistdiff -r dc12cb350527 Info.plist--- a/Info.plist Sat Aug 07 14:54:57 2010 +0800+++ b/Info.plist Sat Aug 07 15:07:18 2010 +0800@@ -3,7 +3,7 @@ <plist version="1.0"> <dict> <key>Author</key>- <string>Neo Lee</string>+ <string>Li Jun</string> <key>CFBundleDisplayName</key> <string>Goo.gl</string> <key>CFBundleIdentifier</key>

% hg remove link.pngremoving link.png

% hg statusM Info.plistR link.png? Info.plist.1

% hg addadding Info.plist.1

% hg stM Info.plistA Info.plist.1R link.png

% hg commit

% hg logchangeset: 2:5ba69dac6378tag: tipuser: "Neo Lee <neo.lee at gmail.com>"date: Sat Aug 07 15:10:02 2010 +0800files: Info.plist Info.plist.1 link.pngdescription:Some dirty work!

changeset: 1:dc12cb350527user: "Neo Lee <neo.lee at gmail.com>"date: Sat Aug 07 14:54:57 2010 +0800files: global.htmldescription:Capitalize 'ERROR' prompt.

changeset: 0:0ae94e57c184user: "Neo Lee <neo.lee at gmail.com>"date: Sat Aug 07 14:43:24 2010 +0800files: Info.plist global.html link.pngdescription:Init commit.

% hg cat Info.plist

% hg cat -r 0 Info.plist

% hg diff -r 0:1 Info.plist

% hg update -r 0

% hg up -r 1

% hg up

其它常用的指令

% mkdir repo

% cd repo

% hg init

% hg servelistening at http://8.oasis.local:8000/ (bound to *:8000)

% hg clone http://8.oasis.local:8000/ workingno changes foundupdating to branch defaultresolving manifests0 files updated, 0 files merged, 0 files removed, 0 files unresolved

% cd working

% lltotal 0drwxr-xr-x 3 neo staff 102 Aug 7 15:49 .drwxr-xr-x 4 neo staff 136 Aug 7 15:49 ..drwxr-xr-x 8 neo staff 272 Aug 7 15:49 .hg

% hg pushpushing to http://8.oasis.local:8000/searching for changesadding changesetsadding manifestsadding file changesadded 2 changesets with 2 changes to 1 files

Joel

Rose

% hg outgoingcomparing with http://8.oasis.local:8000/searching for changeschangeset: 2:689026657682tag: tipuser: Rose M <rose@snda.com>date: Mon Feb 08 15:29:09 2010 -0500summary: Spicier kind of chile.

% hg pushpushing to http://8.oasis.local:8000/searching for changesadding changesetsadding manifestsadding file changesadded 1 changesets with 1 changes to 1 files

Rose

% hg outgoingcomparing with http://8.oasis.local:8000/searching for changeschangeset: 2:4ecdb2401ab4tag: tipuser: Joel S <joel@snda.com>date: Mon Feb 08 15:32:01 2010 -0500summary: potato chips. No one can eat just one.% hg pushpushing to http://8.oasis.local:8000/searching for changesabort: push creates new remote heads!(did you forget to merge? use push -f to force)

% hg incomingcomparing with http://8.oasis.local:8000/searching for changeschangeset: 3:689026657682tag: tipparent: 1:a52881ed530duser: Rose M <rose@example.com>date: Mon Feb 08 15:29:09 2010 -0500summary: spicier kind of chile% hg pullpulling from http://8.oasis.local:8000/searching for changesadding changesetsadding manifestsadding file changesadded 1 changesets with 1 changes to 1 files (+1 heads)(run 'hg heads' to see heads, 'hg merge' to merge)

Joel

% hg mergemerging guac0 files updated, 1 files merged, 0 files removed, 0 files unresolved(branch merge, don't forget to commit)% hg commit -m "merge"% hg logchangeset: 4:0849ca96c304tag: tipparent: 2:4ecdb2401ab4parent: 3:689026657682user: Joel S <joel@snda.com>date: Mon Feb 08 16:07:23 2010 -0500summary: merge... ...% hg outcomparing with http://8.oasis.local:8000//searching for changeschangeset: 2:4ecdb2401ab4... ...changeset: 4:0849ca96c304... ...% hg pushpushing to http://8.oasis.local:8000/searching for changesadding changesetsadding manifestsadding file changesadded 2 changesets with 2 changes to 1 files

Joel

% hg pullpulling from http://8.oasis.local:8000/searching for changesadding changesetsadding manifestsadding file changesadded 2 changesets with 2 changes to 1 files(run 'hg update' to get a working copy)

% hg log

% hg parentchangeset: 2:689026657682user: Rose H <rose@snda.com>date: Mon Feb 08 15:29:09 2010 -0500summary: spicier kind of chile

% hg up1 files updated, 0 files merged, 0 files removed, 0 files unresolved

Rose

享受灵活性带来的疯狂试验乐趣

• 失败的试验可以回退• 如果未提交:hg revert

• 如果已提交:hg rollback(一次commit或pull)

• 大规模的、高风险的试验• hg clone project peoject-experiment

Backout 时间机器!% hg backout -m 'back out second change' tip

% hg backout --merge -m 'Back out second change.' 1reverting myfilecreated new headchangeset 3:abc7fd860049 backs out changeset 1:7e341ee3be7amerging with changeset 3:abc7fd860049merging myfile0 files updated, 1 files merged, 0 files removed, 0 files unresolved(branch merge, don't forget to commit)

1st change 2nd change 3rd change

Back-out 2nd change

Automated merge

重视并行开发与合并

• 在同一个code base上进行并行开发

• 获取他人的改动(pull)

• 提交你的改动(push)

• 时而通过合并(merge)解决改动冲突

• 必要时进行人工合并

构建优秀的代码库架构

• 去中心化带来的好处• 使用“变化集”更灵活的管理版本

• 对并行开发和分支合并的良好支持• 更灵活强大的库管理及操作流程

分布式版本管理带来

Subversion → Mercurial/Git?

• 凡事有得必有失• 利用前/后端代理

References

• Mercurial: The Definitive Guideby Bryan O'Sullivan, http://hgbook.red-bean.com/

• Hg Init: a Mercurial tutorialby Joel Spolsky, http://hginit.com/top/index.html

• Git for the Lazyby Spheriki, http://www.spheredev.org/wiki/Git_for_the_lazy

• PeepCode Git Internalsby Scott Chacon

Contact Me

• neo.lee@gmail.com

• @soulhacker

• 盛大创新院

Questions?

Recommended