git 新建分支修复bug学习笔记

我的的几个疑问:git

为何不直接切换分支修改bug,而后合并?

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git add bug.txt

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git checkout dev
error: Your local changes to the following files would be overwritten by checkout:
        bug.txt
Please commit your changes or stash them before you switch branches.
Aborting

在没有进行commit的时候没法切换分支。网站

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git checkout -b dev1
Switched to a new branch 'dev1'
D       bbbbug.txt
M       bug.txt

可是新建并切换就能够,具体并不知什么缘由。this

为何不直接经过新建一个分支在新的分支上修复bug而后合并?

1.新建/切换dev分支
2.新建文件
3.切换到master分支
4.两个分支内容同样
1.新建/切换dev分支
2.修改文件
3.切换到master分支
4.两个分支内容同样
1.新建切换到dev分支
2.修改文件
3.使用add和commit命令
4.切换到master分支
5.提示找不到文件了

借用别人的理解:工做区和暂存区是一个公开的工做台,任何分支都会用到,并能看到工做台上最新的内容,只要在工做区、暂存区的改动未可以提交到某一个版本库(分支)中,那么在任何一个分支下均可以看获得这个工做区、暂存区的最新实时改动。 使用git stash就能够将暂存区的修改藏匿起来,使整个工做台看起来都是干净的。因此要清理整个工做台,那么前提是必须先将工做区的内容都add到暂存区中去。以后在干净的工做台上能够作另一件紧急事件与藏匿起来的内容是彻底独立的code

dung!dung!dung!BUG!

刚刚输错了命令git 奔溃了,出现了一个问题,致使不能切换分支而且当前分支也没法删除:blog

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (dev1)
$ git checkout master
fatal: Unable to create 'C:/Users/Super Girl/learngit/gitskills/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (dev1)
$ git branch -d dev1
error: Cannot delete branch 'dev1' checked out at 'C:/Users/Super Girl/learngit/gitskills'

解决办法:
删除.git文件目录下的index.lock文件,注意这里的.git文件是隐藏的事件

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (dev1)
$ cd .git

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills/.git (GIT_DIR!)
$ rm index.lock

因此咱们仍是须要使用git stash

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git add bug.txt

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git status
On branch featurel
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   bug.txt


Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git stash
Saved working directory and index state WIP on featurel: 97ba581 delete

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git status
On branch featurel
nothing to commit, working tree clean

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (master)
$ git checkout -b issue-101
Switched to a new branch 'issue-101'

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (issue-101)
$ git add bug.txt

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (issue-101)
$ git commit -m "fix bug 101"
[issue-101 7fb8d7e] fix bug 101
 1 file changed, 2 insertions(+), 2 deletions(-)

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (issue-101)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (master)
$ git merge --no-ff -m "merge bug fix 101" issue-101
Merge made by the 'recursive' strategy.
 bug.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (master)
$ git checkout featurel
Switched to branch 'featurel'

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git status
On branch featurel
nothing to commit, working tree clean

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git stash list
stash@{0}: WIP on featurel: 97ba581 delete

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git stash pop
On branch featurel
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   bug.txt

Dropped refs/stash@{0} (0c4702219ab240d8686b21b6934bec908307e3eb)

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git branch -d issue-101
error: The branch 'issue-101' is not fully merged.
If you are sure you want to delete it, run 'git branch -D issue-101'.

Super Girl@DESKTOP-NOEE1D4 MINGW64 ~/learngit/gitskills (featurel)
$ git branch -D issue-101
Deleted branch issue-101 (was 7fb8d7e).

小结:

修复bug时,咱们会经过建立新的bug分支进行修复,而后合并,最后删除。
当手头工做没有完成时,先把现场工做git stash一下,而后再去修复bug,修复后,再git stash pop,回到工做现场,若是有多个stash,可用git stash clear清除。rem

版权声明

做者:赵洁钰Amyit

  出处:http://www.cnblogs.com/zhaojieyu/io

  您的支持是对博主深刻思考总结的最大鼓励。ast

  本文版权归做者全部,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,尊重做者的劳动成果。

  参考:廖雪峰的官方网站 Bug分支-----:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178794d966923e5c4134bc8bf98dfb03aea3000

相关文章
相关标签/搜索