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

相關文章
相關標籤/搜索