Git 使用规范流程(原文出处: 阮一峰)

来源:互联网 发布:逃不开的经济周期2淘宝 编辑:程序博客网 时间:2024/06/08 00:46

下图是ThoughtBot的Git使用规范流程。
ThoughtBot

第一步:新建分支
首先,每次开发新功能,都应该新建一个单独的分支(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 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,这样不仅清晰,也容易管理。这就要用到 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操作。

  • 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后,就可以推送当前分支到远程仓库了。

$ git push --force origin myfeature

git push命令要加上force参数,因为rebase以后,分支历史改变了,跟远程分支不一定兼容,有可能要强行推送。

第七步:发出Pull Request
提交到远程仓库以后,就可以发出 Pull Request 到master分支,然后请求别人进行代码review,确认可以合并到master。

(完)

0 0
原创粉丝点击