- 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 branch
issue-101
To fix it, but, wait, right nowdev
Work 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 one
stash
Function, 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, with
git status
If 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 on
master
On the branch, right from themaster
Create 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 to
master
Branch, and complete the merge, and finally deleteissue-101
Branches:
$ 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 back
dev
Branch 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? with
git stash list
Command 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 use
git stash apply
Recovery, but after recovery,stash
Content is not deleted, you need to usegit stash drop
To delete; - Another way is to use
git stash pop
Delete 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 use
git stash list
If you look at it, you won’t see any stash content:
$ git stash list
Copy the code
- You can do it many times
stash
While recovering, use it firstgit stash list
View and then restore the specified stash with the command:
$ git stash apply stash@{0}
Copy the code
- in
master
Once we fix the bug on the branch, we have to think,dev
The branch is early frommaster
So, this bug actually exists on the current dev branch as well. - That how to
dev
Fix the same bug on branches? Just do it again and submit it, okay?
Is there an easier way? There are!
- Same bug, to be in
dev
Fix it. We just need to get it4c805e2 fix bug 101
The changes made by this commit are “copied” todev
Branch. Note: We only want to copy4c805e2 fix bug 101
The changes made by this commit are not the wholemaster
branchmerge
Come here. - Git provides one for ease of use
cherry-pick
The 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 branch
commit
is1d4b803
It is not the same asmaster
the4c805e2
Because of these twocommit
It’s just the same change, but it’s really two different thingscommit
. With the git cherry-pick
, we don’t need to be indev
The process of fixing the bug is repeated manually on the branch. - Some smart kids will think, now that you can
master
After fixing the bug on the branch, indev
Branches can “replay” this repair process, then directly indev
Fix the bug on the branch and “replay” it on the master branch. Sure, but you still need itgit stash
Command to save the scene fromdev
Branch over tomaster
Branch.
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 first
git stash
And then fix the bug, and then fix it, and thengit stash pop
, return to the work site;
- in
master
Bug fixed on branch that you want to merge into currentdev
Branch, you can usegit cherry-pick <commit>
Command to “copy” bug submitted changes to the current branch to avoid duplication of effort.