"Go" "Nanyi Blog" Git uses the canonical process

Source: Internet
Author: User
Tags vcard openpgp

Nanyi

Date: August 5, 2015

In team development, it is important to follow a logical and clear git usage process.

Otherwise, everyone commits a bunch of messy commits, and the project will quickly become difficult to reconcile and maintain.

The following is a Thoughtbot git usage specification process. I've learned a lot from it, and I recommend that you use Git as well.

First step: Create a new branch

First, each time you develop a new feature, you should create a separate branch (refer to the GIT Branch management policy).

# 获取主干最新代码$ git checkout master$ git pull# 新建一个开发分支myfeature$ git checkout -b myfeature
Step two: Commit the Branch commit

After the branch is modified, you can commit the commit.

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

The all parameter of the git add command, which means that all changes are saved (including new, modified, and deleted). Starting with Git 2.0, all is the default parameter for git add, so you can also use git Add. Replace.

Git status command to view the files that have changed.

The verbose parameter of the git commit command lists the results of the diff.

Step Three: Compose submission information

When committing a commit, you must give a complete summary of the submission, and here is a template.

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     

The first line is a feed of no more than 50 words, and then an empty line, ROM lists the reasons for the change, major changes, and issues to be aware of. Finally, provide the corresponding URL (such as Bug ticket).

Fourth step: synchronizing with the backbone

During the development of the branch, you should always keep in sync with the trunk.

$ git fetch origin$ git rebase origin/master
Fifth Step: Merge Commit

After the branch development is complete, it is possible to have a bunch of commits, but when merging into the trunk, it is often desirable to have only one (or up to two or three) commits, which is not only clear, but also manageable.

So how can you merge multiple commits? This will use the git rebase command.

-i origin/master

The I parameter of the git rebase command represents the interaction (interactive), when Git opens an interactive interface for next steps.

The following example of Tute Costa is used to explain how to merge commits.

Pick 07c5abd introduce OpenPGP and teach basic Usagepick de9b1eb Fix Postchecker::P OST#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 = u Se 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, exe c = Run command (the rest of the line) using Shell## these lines C An IS re-ordered; They is executed from top to bottom. ## If you remove a line here, COMMIT would be Lost.## However, if you remove everything, the rebase would be Aborted.## Note that empty commits is commented out    "/ span>        

The interactive interface above lists the latest 4 commits for the current branch (the more recent). Each commit is preceded by an action command, which defaults to pick, which indicates that the row commit is selected for rebase operation.

4 commits the following is a bunch of comments that list the commands you can use.

  • Pick: Normal Check
  • Reword: Select, and modify the submission information;
  • Edit: Checked, rebase will be paused, allowing you to modify this commit (see here)
  • Squash: checked, the current commit is merged with the previous commit
  • Fixup: Same as squash, but does not save commit information for the current commit
  • EXEC: Execute other shell commands

Among the 6 commands above, squash and fixup can be used to merge commits. Change the verb in front of the commit that needs to be merged to squash (or s) first.

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

With this change, the current branch will only have two commits left after execution. The commit of the second and third rows is merged into the commit of the first row. The submission information will also contain the three commit submissions.

# 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 row to the fixup command.

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

The result is the same, two commits are generated, and a commit of the second and third rows is merged into the commit of the first row. However, in the new submission, the commit information for the third row of commits 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

Pony Foo proposes another easy way to merge commits by undoing the last 5 commits before building a new one.

$ git reset HEAD~5$ git add .$ git commit -am "Here‘s the bug fix that closes #28"$ git push --force

The squash and fixup commands can also be used as command-line arguments to automatically merge commits.

--fixup  $ git rebase -i --autosquash 

Please refer to this article for this usage, it is not explained here.

Sixth step: push to remote repository

Once the commit is merged, you can push the current branch to the remote repository.

--force origin myfeature

The git push command has to add the Force parameter, because after rebase, the branch history changes, and the remote branch is not necessarily compatible, it is possible to push (see here).

Seventh step: Issue Pull request

After committing to the remote repository, you can issue a pull request to the master branch and then request someone else to code review, confirming that it can be merged into master.

Finish

"Go" "Nanyi Blog" Git uses the canonical process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.