github-Branch management 02-bug and Feature branch

Source: Internet
Author: User
Tags version control system

Reference blog: Liaoche git tutorial

1. Bug Branch

In software development, bugs are like the norm. With bugs that need to be fixed, in git, because branches are so powerful, each bug can be repaired by a new temporary branch, repaired, merged, and then removed by the temporary branch.

1.1. Store the current branch code

When you receive a task to fix a bug with a code number of 101, it is natural that you want to create a branch issue-101 to fix it, but, and so on, the work that is currently in dev progress has not been submitted.

1 [[email protected] zhangtest]# git status2 # on Branch Dev3 # Changes to be committed:4# (use"git reset HEAD <file>"To Unstage)5 #6# NEWfile: index.html7 #8# changes not staged forCommit:9# (use"git add <file>, ... ."To update what'll be committed)Ten# (use"git checkout--<file>, ... ."To discard changesinchworking directory) One # A # Modified:license - # modified:README.md -#

It is not that you do not want to submit, but only half of the work, not submitted, it is expected to be completed in 1 days. However, the bug must be fixed within two hours, what should I do?

Fortunately, Git also provides a stash feature to "store" the current work site, and then continue to work after resuming the site:

1 [[email protected] zhangtest]# git stash   2 Saved working directory and index state WIP on dev:ef9042a Dev Brach new 3 HEAD is now at ef9042a Dev Brach new 4 [[email protected] zhangtest]# git status   5 # on Branch Dev 6 Nothing to commits, working directory clean

Now, with the git status view workspace, it's clean (unless you have a file that's not managed by git), so you can safely create a branch to fix the bug.

1.2. Create a branch that corresponds to the bug and fix

First determine which branch to fix the bug on, assuming that you need to master fix it on the branch, from master creating a temporary branch:

1 [[email protected] zhangtest]# git checkout master2Switched to Branch'Master'3Your Branch is ahead of'Origin/master'By7commits.4(Use"git push"To publish your local commits)5 [[email protected] zhangtest]# git branch6 Dev7*Master8[[email protected] zhangtest]# git checkout-b issue-1019Switched to a new branch'issue-101'Ten [[email protected] zhangtest]# git branch One Dev A* issue-101 -Master

Now fix the bug and need to change "Git is free software ..." to "Git is a free software ..." and then submit.

1[Email protected] zhangtest]#Head-N5 readme.md2 # zhangtest3 zhangtest4 Zhang San5 Git is a distributed version control system.6Git is a Freesoftware. # Places to change7 [[email protected] zhangtest]# git add readme.md8[[email protected] zhangtest]# git commit-m"Fix Bug 101"9[issue-101 1987689] Fix bug101Ten  1 fileChanged1Insertion (+),1Deletion (-)

1.3. Merge and delete bug branches after repair

After the repair is complete, switch to the master branch, complete the merge, and finally delete the issue-101 branch:

1 [[email protected] zhangtest]# git checkout master2Switched to Branch'Master'3Your Branch is ahead of'Origin/master'By7commits.4(Use"git push"To publish your local commits)5[[email protected] zhangtest]# git merge--no-ff-m"merged bug fix 101"issue-101# merging Bug branches6Merge made by the'Recursive'strategy.7readme.md |2+-8  1 fileChanged1Insertion (+),1Deletion (-)9[[email protected] zhangtest]# git branch-d issue-101# Remove a bug branchTenDeleted Branch issue-101(Was1987689). One [[email protected] zhangtest]# git Branch # view branch Information A Dev -* Master

1.4. Go back to the beginning branch and start working

Great, the original two-hour bug fix took only 5 minutes! Now it's time to go back to dev the branch and work!

1 [[email protected] zhangtest]# git checkout dev 2 ' Dev ' 3 [[email protected] zhangtest]# git status 4 # on Branch Dev 5 Nothing to commits, working directory clean

The work area is clean, where did you save the work site just now? Take a git stash list look at the command:

1 [[email protected] zhangtest]# git stash list 2 [email protected]{0}: WIP on dev:ef9042a Dev Brach New

The work site is still there, git put stash content somewhere, but need to restore, there are two ways:

One is to use git stash apply recovery, but after recovery, stash content does not delete, you need git stash drop to use to delete;

Another way is to use git stash pop , restore the stash content also deleted:

1 [[email protected] zhangtest]# git stash pop2 # on Branch Dev3 # Changes to be committed:4# (use"git reset HEAD <file>"To Unstage)5 #6# NEWfile: index.html7 #8# changes not staged forCommit:9# (use"git add <file>, ... ."To update what'll be committed)Ten# (use"git checkout--<file>, ... ."To discard changesinchworking directory) One # A # Modified:license - # modified:README.md - # theDropped Refs/[email protected]{0} (72D4C766F8DE0C10E01D85D282130A393C0601FD)

git stash listIf you look at it again, you won't see any stash content:

1 [[email protected] zhangtest]# git stash list

You can stash multiple times, restore the time, first with git stash list view, then restore the specified stash, with the command:

1 $ git stash apply [email protected]{0}

2. Feature Branch

In software development, there is always an endless amount of new features to add in.

When adding a new feature, you certainly don't want to mess up the main branch because of some experimental code, so every new feature is added, it's best to create a feature branch on top, complete, merge, and finally delete the feature branch.

Now you've finally got a new task: Develop new features named Vulcan.

2.1. Create and use a branch
12 * Dev3  master4 [[email protected] zhangtest]# git Checkout-b feature-Vulcan5'feature-vulcan'

1[Email protected] zhangtest]#Catfeature-vulcan.html # New Add File23feature-Vulcan45[[email protected] zhangtest]# git add feature-vulcan.html # Add a new file to the staging area6[[email protected] zhangtest]# git commit-m'Add Feature-vulcan' . # Submit to Warehouse7[Feature-vulcan 239705f] Add feature-Vulcan8  2Files changed,4Insertions (+)9Create mode100644feature-vulcan.htmlTen [[email protected] zhangtest]# git status # View status One# on Branch feature-Vulcan ANothing-to-commit, working directory clean

2.2. How to Discard branches

Now that everything goes well, the feature branch and the bug branch are similar, merge, and then delete.

Cut back dev , ready to merge:

12 * Dev3   feature-Vulcan4   Master

However, at this time, received orders from superiors, due to insufficient funds, the new function must be canceled!

This branch, which contains confidential information, must be destroyed in situ, although it is in vain:

1 [[email protected] zhangtest]# git branch-d feature-Vulcan2'feature-vulcan  ' is not fully merged. 3 ' git branch-d feature-vulcan '.

Destroy failed. Git friendly reminder, feature-vulcan branch has not been merged, if deleted, will be lost changes, if you want to forcibly delete, you need to use uppercase -D parameters.

Now we forcibly delete:

1 [[email protected] zhangtest]# git branch-d feature-Vulcan2 Deleted Branch Feature-vulcan ( Was 239705f).

Delete Success!

2.3. Summary

To develop a new feature, it is better to create a new branch;

If you want to discard a branch that has not been merged, you can remove it by git branch -D <name> force.

github-Branch management 02-bug and Feature branch

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.