How to Use Git
團(tuán)隊(duì)開發(fā)中,遵循一個(gè)合理、清晰的Git使用流程,是非常重要的。
否則,每個(gè)人都提交一堆雜亂無章的commit,項(xiàng)目很快就會變得難以協(xié)調(diào)和維護(hù)。
第零步:克隆遠(yuǎn)程倉庫
| 1 | $ git clone git@gitlab.moodle.nordanglia.com:moodle/school.git |
第一步:新建issue和分支
首先,每次開發(fā)新功能或者修復(fù)bug,都應(yīng)該新建一個(gè)issue和一個(gè)單獨(dú)的分支。
分支命名應(yīng)該以issue number開頭,例如1-feature-isams-integration,24-bug-require-parameter。
| 1 2 3 4 5 6 | # 獲取production最新代碼 $ git checkout production_29 $ git pull # 新建一個(gè)開發(fā)分支1-feature-name, $ git checkout -b 1-feature-name |
第二步:提交分支commit
分支修改后,就可以提交commit了。
| 1 2 3 | $ git add . $ git status $ git commit --verbose |
git status 命令,用來查看發(fā)生變動(dòng)的文件。
git diff 命令,用來查看詳細(xì)改動(dòng)。
git commit 命令的verbose參數(shù),會列出 diff 的結(jié)果。
第三步:撰寫提交信息
提交commit時(shí),必須給出完整扼要的提交信息,下面是一個(gè)范本。
| 1 2 3 4 5 | Present-tense summary under 50 characters More information about commit (under 72 characters). issue name #issue number |
第一行是不超過50個(gè)字的提要,然后空一行,羅列出改動(dòng)原因、主要變動(dòng)、以及需要注意的問題。
最后,提供對應(yīng)的issue的name和number(比如iSAMS integration #1)。
第四步:與主干同步
分支的開發(fā)過程中,要經(jīng)常與主干保持同步。
| 1 2 3 4 5 | # 獲取遠(yuǎn)程production最新代碼 $ git fetch origin # 本地合并production的到當(dāng)前分支,并把你的commits放在最后面。 $ git rebase origin/production_29 |
第五步:合并commit
分支開發(fā)完成后,很可能有一堆commit,但是合并到主干的時(shí)候,往往希望只有一個(gè)(或最多兩三個(gè))commit,這樣不僅清晰,也容易管理。
那么,怎樣才能將多個(gè)commit合并呢?這就要用到 git rebase 命令。
| 1 | $ git rebase -i origin/production_29 |
?
git rebase命令的i參數(shù)表示互動(dòng)(interactive),這時(shí)git會打開一個(gè)互動(dòng)界面,進(jìn)行下一步操作。
下面采用Tute Costa的例子,來解釋怎么合并commit。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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 |
上面的互動(dòng)界面,先列出當(dāng)前分支最新的4個(gè)commit(越下面越新)。
每個(gè)commit前面有一個(gè)操作命令,默認(rèn)是pick,表示該行commit被選中,要進(jìn)行rebase操作。
4個(gè)commit的下面是一大堆注釋,列出可以使用的命令。
pick:正常選中
reword:選中,并且修改提交信息;
edit:選中,rebase時(shí)會暫停,允許你修改這個(gè)commit(參考這里)
squash:選中,會將當(dāng)前commit與上一個(gè)commit合并
fixup:與squash相同,但不會保存當(dāng)前commit的提交信息
exec:執(zhí)行其他shell命令
上面這6個(gè)命令當(dāng)中,squash和fixup可以用來合并commit。
先把需要合并的commit前面的動(dòng)詞,改成squash(或者s)。
| 1 2 3 4 | 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í)行后,當(dāng)前分支只會剩下兩個(gè)commit。
第二行和第三行的commit,都會合并到第一行的commit。提交信息會同時(shí)包含,這三個(gè)commit的提交信息。
| 1 2 3 4 5 6 7 8 9 | # 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命令。
| 1 2 3 4 | 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 |
運(yùn)行結(jié)果相同,還是會生成兩個(gè)commit,第二行和第三行的commit,都合并到第一行的commit。
但是,新的提交信息里面,第三行commit的提交信息,會被注釋掉。
| 1 2 3 4 5 6 7 8 9 | # 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 |
第六步:推送到遠(yuǎn)程倉庫
合并commit后,就可以推送當(dāng)前分支到遠(yuǎn)程倉庫了。
| 1 | $ git push --force origin 1-feature-name |
git push命令要加上force參數(shù),因?yàn)閞ebase以后,分支歷史改變了,跟遠(yuǎn)程分支不一定兼容,有可能要強(qiáng)行推送(參見這里)。
第七步:發(fā)出Merge Request
提交到遠(yuǎn)程倉庫以后,就可以發(fā)出 Merge Request 到Production_29分支,然后請求別人進(jìn)行代碼review,并部署到Staging Server測試。
第八步: 部署
部署到Production Server。
如果一切順利,合并到Production_29,如果有問題,用production_29的代碼回滾。
轉(zhuǎn)載于:https://www.cnblogs.com/morninglight/p/8202368.html
總結(jié)
以上是生活随笔為你收集整理的How to Use Git的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 埃拉托色尼素数筛法(转)
- 下一篇: div中图片居中