• Bugs are a common occurrence in software development. Bugs need to be fixed, and in Git, because branches are so powerful, each bug can be fixed with a new temporary branch, which is merged, and then removed.
  • When you receive a task to fix a bug code-named 101, you naturally want to create a branchissue-101To fix it, but, wait, right nowdevWork carried out on:
$ 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
Copy the code
  • It’s not that you don’t want to submit, but that the work is only half done and still can’t be submitted. It’s expected to take another day to complete. But what if the bug has to be fixed within two hours?
  • Fortunately, Git provides onestashFunction, can be the current work site “storage”, and so on after the recovery of the site to continue to work:
$ git stash
Saved working directory and index state WIP on dev: f52c633 add merge
Copy the code
  • Now, withgit statusIf you look at your workspace, it’s clean (unless you have files managed by Git), so you can safely create branches to fix bugs.
  • First determine which branch to fix the bug on, assuming you need to fix the bug onmasterOn the branch, right from themasterCreate temporary branch:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits

$ git checkout -b issue-101
Switched to a new branch 'issue-101'
Copy the code
  • Now fix the bug, need to”Git is free software ...“To”Git is a free software ...“And then submit:
$ git add readme.txt 
$ git commit -m "fix bug 101"
[issue-101 4c805e2] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)
Copy the code
  • When the repair is complete, switch tomasterBranch, and complete the merge, and finally deleteissue-101Branches:
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local 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(-)
Copy the code
  • Great, what was supposed to be a two-hour bug fix only took 5 minutes! Now, it’s time to go backdevBranch work!
$ git switch dev
Switched to branch 'dev'

$ git status
On branch dev
nothing to commit, working tree clean
Copy the code
  • The workspace is clean. Where is the work site? withgit stash listCommand to see:
$ git stash list
stash@{0}: WIP on dev: f52c633 add merge
Copy the code
  • The workspace is still there, Git has stash content somewhere, but you need to restore it. There are two ways to do this:
  • One is to usegit stash applyRecovery, but after recovery,stashContent is not deleted, you need to usegit stash dropTo delete;
  • Another way is to usegit stash popDelete the Stash content as well:
$ 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} (5d677e2ee266f39ea296182fb2354265b91b3b2a)
Copy the code
  • Then usegit stash listIf you look at it, you won’t see any stash content:
$ git stash list
Copy the code
  • You can do it many timesstashWhile recovering, use it firstgit stash listView and then restore the specified stash with the command:
$ git stash apply stash@{0}
Copy the code
  • inmasterOnce we fix the bug on the branch, we have to think,devThe branch is early frommasterSo, this bug actually exists on the current dev branch as well.
  • That how todevFix the same bug on branches? Just do it again and submit it, okay?

Is there an easier way? There are!

  • Same bug, to be indevFix it. We just need to get it4c805e2 fix bug 101The changes made by this commit are “copied” todevBranch. Note: We only want to copy4c805e2 fix bug 101The changes made by this commit are not the wholemasterbranchmergeCome here.
  • Git provides one for ease of usecherry-pickThe command allows us to copy a specific commit to the current branch:
$ git branch
* dev
  master
$ git cherry-pick 4c805e2
[master 1d4b803] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)
Copy the code
  • Git automatically commits to the dev branchcommitis1d4b803It is not the same asmasterthe4c805e2Because of these twocommitIt’s just the same change, but it’s really two different thingscommit. With the git cherry-pick, we don’t need to be indevThe process of fixing the bug is repeated manually on the branch.
  • Some smart kids will think, now that you canmasterAfter fixing the bug on the branch, indevBranches can “replay” this repair process, then directly indevFix the bug on the branch and “replay” it on the master branch. Sure, but you still need itgit stashCommand to save the scene fromdevBranch over tomasterBranch.

summary

  • When fixing bugs, we fix them by creating new bug branches, then merging them, and finally deleting them;
  • When the work at hand is not finished, put the work on the spot firstgit stashAnd then fix the bug, and then fix it, and thengit stash pop, return to the work site;
  • inmasterBug fixed on branch that you want to merge into currentdevBranch, you can usegit cherry-pick <commit>Command to “copy” bug submitted changes to the current branch to avoid duplication of effort.