Git 使用規範流程【轉】

來源:互聯網
上載者:User

標籤:

轉自:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html

 阮一峰

日期: 2015年8月 5日

團隊開發中,遵循一個合理、清晰的Git使用流程,是非常重要的。

否則,每個人都提交一堆雜亂無章的commit,項目很快就會變得難以協調和維護。

下面是ThoughtBot 的Git使用規範流程。我從中學到了很多,推薦你也這樣使用Git。

第一步:建立分支

首先,每次開發新功能,都應該建立一個單獨的分支(這方面可以參考《Git分支管理原則》)。

# 擷取主幹最新代碼$ git checkout master$ git pull# 建立一個開發分支myfeature$ git checkout -b myfeature
第二步:提交分支commit

分支修改後,就可以提交commit了。

$ git add --all$ git status$ git commit --verbose

git add 命令的all參數,表示儲存所有變化(包括建立、修改和刪除)。從Git 2.0開始,all是 git add 的預設參數,所以也可以用 git add . 代替。

git status 命令,用來查看發生變動的檔案。

git commit 命令的verbose參數,會列出 diff 的結果。

第三步:撰寫提交資訊

提交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個字的提要,然後空一行,羅列出改動原因、主要變動、以及需要注意的問題。最後,提供對應的網址(比如Bug ticket)。

第四步:與主幹同步

分支的開發過程中,要經常與主幹保持同步。

$ git fetch origin$ git rebase origin/master
第五步:合并commit

分支開發完成後,很可能有一堆commit,但是合并到主乾的時候,往往希望只有一個(或最多兩三個)commit,這樣不僅清晰,也容易管理。

那麼,怎樣才能將多個commit合并呢?這就要用到 git rebase 命令。

$ git rebase -i origin/master

git rebase命令的i參數表示互動(interactive),這時git會開啟一個互動介面,進行下一步操作。

下面採用Tute Costa的例子,來解釋怎麼合并commit。

pick 07c5abd Introduce OpenPGP and teach basic usagepick de9b1eb Fix PostChecker::Post#urlspick 3e7ee36 Hey kids, stop all the highlightingpick 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(參考這裡)
  • squash:選中,會將當前commit與上一個commit合并
  • fixup:與squash相同,但不會儲存當前commit的提交資訊
  • exec:執行其他shell命令

上面這6個命令當中,squash和fixup可以用來合并commit。先把需要合并的commit前面的動詞,改成squash(或者s)。

pick 07c5abd Introduce OpenPGP and teach basic usages de9b1eb Fix PostChecker::Post#urlss 3e7ee36 Hey kids, stop all the highlightingpick fa20af3 git interactive rebase, squash, amend

這樣一改,執行後,當前分支只會剩下兩個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 usages de9b1eb Fix PostChecker::Post#urlsf 3e7ee36 Hey kids, stop all the highlightingpick fa20af3 git interactive rebase, squash, amend

運行結果相同,還是會產生兩個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命令,還可以當作命令列參數使用,自動合并commit。

$ git commit --fixup  $ git rebase -i --autosquash 

這個用法請參考這篇文章,這裡就不解釋了。

第六步:推送到遠程倉庫

合并commit後,就可以推送當前分支到遠程倉庫了。

$ git push --force origin myfeature

git push命令要加上force參數,因為rebase以後,分支曆史改變了,跟遠程分支不一定相容,有可能要強行推送(參見這裡)。

第七步:發出Pull Request

提交到遠程倉庫以後,就可以發出 Pull Request 到master分支,然後請求別人進行代碼review,確認可以合并到master。

(完)

Git 使用規範流程【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.