bug branch and feature Branch _ Power node of Java College collation

  • 2020-06-19 12:17:22
  • OfStack

bug is a staple in software development. With bug you need to fix it. In Git, because the branches are so powerful, each bug can be fixed with a new temporary branch, after which the branch is merged, and then the temporary branch is removed.

When you're given a task to fix a 101 bug, it's only natural that you want to create a branch issue-101 To fix it, but wait, it's currently working dev Work in progress has not been submitted:


$ git status
# On branch dev
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
#    new file:  hello.py
#
# Changes not staged for commit:
#  (use "git add <file>..." to update what will be committed)
#  (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:  readme.txt
#

It is not that you don't want to submit, but the work is only half done and cannot be submitted yet. It is estimated that it will take another day to complete. But what if the bug must be fixed within two hours?

Fortunately, Git also provides an stash feature that allows you to "store" your current work site and resume it later:


$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge

Now, with git status If you look at the workspace, it is clean (unless there are no files managed by Git), so you can safely create branches to fix bug.

First determine which branch you want to fix bug on, assuming you need to fix bug on master Repair on the branch, just from master Create temporary branches:


$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
$ git checkout -b issue-101
Switched to a new branch 'issue-101'

Now to fix bug, you need to put "Git is free software... "Git is a free software..." , then submit:


$ git add readme.txt 
$ git commit -m "fix bug 101"
[issue-101 cc17032] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)

When the fix is complete, switch to master Branch, and complete the merge, and finally delete issue-101 Branches:


$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
$ git merge --no-ff -m "merged bug fix 101" issue-101
Merge made by the 'recursive' strategy.
 readme.txt |  2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git branch -d issue-101
Deleted branch issue-101 (was cc17032).

Great! The planned two-hour bug repair only took 5 minutes! Now, it's time to go back dev The branch is working!


$ git checkout dev
Switched to branch 'dev'
$ git status
# On branch dev
nothing to commit (working directory clean)

The work area is clean. Where is the work site just now? Use the git stash list command to see:


$ git stash list
stash@{0}: WIP on dev: 6224937 add merge

The work site is still there, Git has saved stash content somewhere, but it needs to be restored. There are two ways:

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

The other way is to use dev0 , the content of stash was also deleted:


$ git stash pop
# On branch dev
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
#    new file:  hello.py
#
# Changes not staged for commit:
#  (use "git add <file>..." to update what will be committed)
#  (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:  readme.txt
#
Dropped refs/stash@{0} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)

Then use git stash list Check it out and you won't see any stash content:


$ git stash list

You can stash many times, and when you restore it, use it first git stash list View, then restore the specified stash with the command:


$ git stash apply stash@{0}

Feature branch

In software development, there are always endless new features to add.

When you add a new feature, you don't want to mess up the main branch because of some experimental code, so for every new feature you add, it's best to create a new feature branch, develop on it, merge, and finally delete the feature branch.

Now, you've finally got a new mission: develop a new feature code-named Vulcan, which is planned for the next generation of starships.

So I was ready to develop:


$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge
0

After 5 minutes, the development was completed:


$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge
1

Cut back to dev, ready to merge:


$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge
2

The feature branch and bug branch are similar if the cut goes well, merge and then delete.

But, be in right now, receive superior order, because fund is insufficient, new function must cancel!

Although it was dried for nothing, the branch had to be destroyed on the spot:


$ git branch -d feature-vulcan
error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.

Destruction failed. Git Friendly reminder, feature-vulcan The branch has not been merged, if deleted, the changes will be lost, if forced to delete, you need to use the command git branch -D feature-vulcan。

Now we force delete:


$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge
4

Finally delete success!


Related articles: