Git usage standard process

Source: Internet
Author: User
Tags openpgp
In team development, it is very important to follow a reasonable and clear Git use process. In team development, it is very important to follow a reasonable and clear Git use process.

Otherwise, everyone submits a bunch of messy commit, and the project will soon become difficult to coordinate and maintain.

The following describes the standard Git usage process of ThoughtBot. I have learned a lot and I recommend you use Git as well.

Step 1: Create a branch

First, you should create a new branch for each new function development (for details, refer to Git branch management policy).

# Get the latest code of the trunk $ git checkout master $ git pull # Create a new development branch myfeature $ git checkout-B myfeature

Step 2: submit the branch commit

After modifying the branch, you can submit commit.

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

The all parameter of the git add command stores all changes (including new, modified, and deleted ). From Git 2.0, all is the default parameter of git add, so you can also use git add.

The git status command is used to view the changed files.

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

Step 3: write the submitted information

When submitting a commit, you must provide the complete and brief submission information. The following is a template.

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 with no more than 50 characters, and a blank line lists the reasons for the change, major changes, and issues needing attention. Finally, provide the corresponding URL (such as Bug ticket ).

Step 4: synchronize with the master

During the branch development process, it is often necessary to maintain synchronization with the trunk.

$ git fetch origin$ git rebase origin/master

Step 5: merge commit

After branch development is complete, there may be a bunch of commit, but when merged to the trunk, you often want only one (or up to two or three) commit, which is clear and easy to manage.

So how can we merge multiple commit statements? This requires the git rebase command.

$ git rebase -i origin/master

The I parameter of the git rebase command indicates interactive. in this case, git opens an interactive interface and performs the next step.

The following example uses Tute Costa to explain how to merge commit.

The I parameter of the git rebase command indicates interactive. in this case, git opens an interactive interface and performs the next step.

The following example uses Tute Costa to explain how to merge 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

In the preceding interactive interface, the latest four commit entries of the current branch are listed (the newer the next one ). Each commit is preceded by an Operation Command. the default value is pick, indicating that the row commit is selected and rebase is required.

Below are a lot of comments for the four commit, listing the commands that can be used.

Pick: normally selected reword: selected, and submitted information is modified; edit: Selected, rebase will be paused, allowing you to modify this commit (refer to here) squash: selected, the current commit and the previous commit will be merged fixup: the same as squash, but the current commit information will not be saved exec: execute other shell commands

Among the above six commands, squash and fixup can be used to merge commit. First, change the verb before the commit to squash (or 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

In this way, after execution, only two commit entries are left in the current branch. The commit of the second and third rows will be merged into the commit of the first row. The submission information will also contain the submission information of the three 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

If you change the squash command of the third line 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

If the running result is the same, two commit statements are generated, and the commit statements of the second and third rows are merged to the commit statement of the first row. However, in the new commit information, the commit information of the third line of commit 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

Squash and fixup commands can also be used as command line parameters to automatically merge commit.

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

For this usage, refer to this article and I will not explain it here.

Step 6: Push to remote warehouse

After merging commit, you can push the current branch to the remote repository.

$ git push --force origin myfeature

The force parameter must be added to the git push command, because after rebase, the branch history has changed, which is not necessarily compatible with the remote branch, and may be forced to push (see here ).

Step 7: Send a Pull Request

After being submitted to the remote warehouse, you can send a Pull Request to the master branch, and then Request someone else to review the code to confirm that the Request can be merged to the master.

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.