In team development, it is very important to follow a reasonable and clear Git usage process.
Otherwise, everyone will submit a bunch of disorganized commits, and the project will soon become difficult to coordinate and maintain.
The following is ThoughtBot’s Git usage specification process. I learned a lot from it and recommend you use Git the same way.
Step 1: Create a new branch
First of all, every time you develop a new feature, you should create a separate branch (you can refer to the "Git Branch Management Strategy" in this regard).
# 获取主干最新代码 $ git checkout master $ git pull # 新建一个开发分支myfeature $ git checkout -b myfeature
Step 2: Submit the branch commit
After the branch is modified, you can submit the commit.
$ git add --all $ git status $ git commit --verbose
The all parameter of the git add command means to save all changes (including new creation, modification and deletion). Starting from Git 2.0, all is the default parameter of git add, so you can also use git add . instead.
The git status command is used to view changed files.
The verbose parameter of the git commit command will list the results of diff.
Step 3: Write the submission message
When submitting a commit, you must give a complete and concise submission message. Below is a sample.
Present-tense summary under 50 characters * More information about commit (under 72 characters). * More information about commit (under 72 characters).
The first line is a summary of no more than 50 words, and then a blank line lists the reasons for the change, major changes, and issues that need attention. Finally, provide the corresponding URL (such as a Bug ticket).
Step 4: Synchronize with the trunk
During the development process of branches, it is necessary to always synchronize with the trunk.
$ git fetch origin $ git rebase origin/master
Step 5: Merge commits
After branch development is completed, there are likely to be a bunch of commits, but when merging into the trunk, you often want to have only one (or at most two or three) commits. This is not only clear, but also easy to manage.
So, how can you merge multiple commits? This requires the git rebase command.
$ git rebase -i origin/master
The i parameter of the git rebase command indicates interactive. At this time, git will open an interactive interface for the next step.
The following uses the example of Tute Costa to explain how to merge commits.
The i parameter of the git rebase command indicates interactive. At this time, git will open an interactive interface for the next step.
The following uses the example of Tute Costa to explain how to merge commits.
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
The above interactive interface first lists the latest 4 commits of the current branch (the lower the ones are, the newer they are). There is an operation command in front of each commit. The default is pick, which means that the commit in this line is selected and needs to be rebase.
Under the four commits are a lot of comments listing the commands that can be used.
pick:正常选中 reword:选中,并且修改提交信息; edit:选中,rebase时会暂停,允许你修改这个commit(参考这里) squash:选中,会将当前commit与上一个commit合并 fixup:与squash相同,但不会保存当前commit的提交信息 exec:执行其他shell命令
Among the above 6 commands, squash and fixup can be used to merge commits. First change the verb in front of the commit that needs to be merged to squash (or 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
After this change is executed, there will only be two commits left in the current branch. The commits in the second and third lines will be merged into the commit in the first line. The submission information will also include the submission information of these three commits.
# 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
If you change the squash command in the third line to the fixup command.
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
The running results are the same. Two commits will still be generated. The commits in the second and third lines are merged into the commit in the first line. However, in the new submission information, the third line of commit information will be commented out.
# 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
The squash and fixup commands can also be used as command line parameters to automatically merge commits.
$ git commit --fixup $ git rebase -i --autosquash
Please refer to this article for this usage, I won’t explain it here.
Step 6: Push to remote warehouse
合并commit后,就可以推送当前分支到远程仓库了。
$ git push --force origin myfeature
git push命令要加上force参数,因为rebase以后,分支历史改变了,跟远程分支不一定兼容,有可能要强行推送(参见这里)。
第七步:发出Pull Request
提交到远程仓库以后,就可以发出 Pull Request 到master分支,然后请求别人进行代码review,确认可以合并到master。