作者: 阮一峰
日期: 2015年8月 5日
團隊開發(fā)中,遵循一個合理、清晰的Git使用流程纲熏,是非常重要的。
否則杨耙,每個人都提交一堆雜亂無章的commit赤套,項目很快就會變得難以協(xié)調(diào)和維護。
下面是ThoughtBot 的Git使用規(guī)范流程珊膜。我從中學到了很多容握,推薦你也這樣使用Git。
第一步:新建分支
首先车柠,每次開發(fā)新功能剔氏,都應該新建一個單獨的分支(這方面可以參考《Git分支管理策略》)塑猖。
# 獲取主干最新代碼
$ git checkout master
$ git pull
# 新建一個開發(fā)分支myfeature
$ git checkout -b myfeature
第二步:提交分支commit
分支修改后,就可以提交commit了谈跛。
$ git add --all
$ git status
$ git commit --verbose
git add 命令的all參數(shù)羊苟,表示保存所有變化(包括新建、修改和刪除)感憾。從Git 2.0開始蜡励,all是 git add 的默認參數(shù),所以也可以用 git add . 代替阻桅。
git status 命令凉倚,用來查看發(fā)生變動的文件。
git commit 命令的verbose參數(shù)嫂沉,會列出 diff 的結(jié)果稽寒。
第三步:撰寫提交信息
提交commit時,必須給出完整扼要的提交信息趟章,下面是一個范本杏糙。
Present-tense summary under 50 characters
* More information about commit (under 72 characters).
* More information about commit (under 72 characters).
http://project.management-system.com/ticket/123
第一行是不超過50個字的提要,然后空一行蚓土,羅列出改動原因宏侍、主要變動、以及需要注意的問題北戏。最后负芋,提供對應的網(wǎng)址(比如Bug ticket)。
第四步:與主干同步
分支的開發(fā)過程中嗜愈,要經(jīng)常與主干保持同步旧蛾。
$ git fetch origin
$ git rebase origin/master
第五步:合并commit
分支開發(fā)完成后,很可能有一堆commit蠕嫁,但是合并到主干的時候锨天,往往希望只有一個(或最多兩三個)commit,這樣不僅清晰剃毒,也容易管理病袄。
那么,怎樣才能將多個commit合并呢赘阀?這就要用到 git rebase 命令益缠。
$ git rebase -i origin/master
git rebase命令的i參數(shù)表示互動(interactive),這時git會打開一個互動界面基公,進行下一步操作幅慌。
下面采用Tute Costa的例子,來解釋怎么合并commit轰豆。
pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
pick 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
## Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
## These lines can be re-ordered; they are executed from top to bottom.
## If you remove a line here THAT COMMIT WILL BE LOST.
## However, if you remove everything, the rebase will be aborted.
## Note that empty commits are commented out
上面的互動界面胰伍,先列出當前分支最新的4個commit(越下面越新)齿诞。每個commit前面有一個操作命令,默認是pick骂租,表示該行commit被選中祷杈,要進行rebase操作。
4個commit的下面是一大堆注釋渗饮,列出可以使用的命令但汞。
pick:正常選中
reword:選中,并且修改提交信息抽米;
edit:選中特占,rebase時會暫停糙置,允許你修改這個commit(參考[這里](https://schacon.github.io/gitbook/4_interactive_rebasing.html))
squash:選中云茸,會將當前commit與上一個commit合并
fixup:與squash相同,但不會保存當前commit的提交信息
exec:執(zhí)行其他shell命令
上面這6個命令當中谤饭,squash和fixup可以用來合并commit标捺。先把需要合并的commit前面的動詞,改成squash(或者s)揉抵。
pick 07c5abd Introduce OpenPGP and teach basic usage
s de9b1eb Fix PostChecker::Post#urls
s 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
這樣一改亡容,執(zhí)行后,當前分支只會剩下兩個commit冤今。第二行和第三行的commit闺兢,都會合并到第一行的commit。提交信息會同時包含戏罢,這三個commit的提交信息屋谭。
# This is a combination of 3 commits.
# The first commit's message is:
Introduce OpenPGP and teach basic usage
# This is the 2nd commit message:
Fix PostChecker::Post#urls
# This is the 3rd commit message:
Hey kids, stop all the highlighting
如果將第三行的squash命令改成fixup命令。
pick 07c5abd Introduce OpenPGP and teach basic usage
s de9b1eb Fix PostChecker::Post#urls
f 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
運行結(jié)果相同龟糕,還是會生成兩個commit桐磁,第二行和第三行的commit,都合并到第一行的commit讲岁。但是我擂,新的提交信息里面,第三行commit的提交信息缓艳,會被注釋掉校摩。
# This is a combination of 3 commits.
# The first commit's message is:
Introduce OpenPGP and teach basic usage
# This is the 2nd commit message:
Fix PostChecker::Post#urls
# This is the 3rd commit message:
# Hey kids, stop all the highlighting
Pony Foo提出另外一種合并commit的簡便方法,就是先撤銷過去5個commit阶淘,然后再建一個新的衙吩。
$ git reset HEAD~5
$ git add .
$ git commit -am "Here's the bug fix that closes #28"
$ git push --force
squash和fixup命令,還可以當作命令行參數(shù)使用舶治,自動合并commit分井。
$ git commit --fixup
$ git rebase -i --autosquash
這個用法請參考這篇文章车猬,這里就不解釋了。
第六步:推送到遠程倉庫
合并commit后尺锚,就可以推送當前分支到遠程倉庫了珠闰。
$ git push --force origin myfeature
git push命令要加上force參數(shù),因為rebase以后瘫辩,分支歷史改變了伏嗜,跟遠程分支不一定兼容,有可能要強行推送(參見這里)伐厌。
第七步:發(fā)出Pull Request
提交到遠程倉庫以后承绸,就可以發(fā)出 Pull Request 到master分支,然后請求別人進行代碼review挣轨,確認可以合并到master军熏。
(完)