當(dāng)當(dāng)前分支上有未提交的更改時,簽出另一個分支大多數(shù)情況下,當(dāng)我嘗試簽出另一個現(xiàn)有分支時,Git不允許我在當(dāng)前分支上有一些未提交的更改。因此,我必須首先提交或保存這些更改。但是,偶爾Git允許我簽出另一個分支,而無需提交或保存這些更改,并且它會將這些更改攜帶到第一分支簽出。這里的規(guī)矩是什么?這些改變是階段性的還是非階段性的?把這些變化帶到另一個分支對我來說沒有任何意義,為什么git有時會允許它呢?也就是說,它在某些情況下有用嗎?
3 回答

蝴蝶刀刀
TA貢獻(xiàn)1801條經(jīng)驗 獲得超8個贊
$ echo 'hello world' > file.txt $ git add file.txt $ git commit -m "adding file.txt" $ git checkout -b experiment $ echo 'goodbye world' >> file.txt $ git add file.txt $ git commit -m "added text" # experiment now contains changes that master doesn't have # any future changes to this file will keep you from changing branches # until the changes are stashed or committed $ echo "and we're back" >> file.txt # making additional changes $ git checkout master error: Your local changes to the following files would be overwritten by checkout: file.txt Please, commit your changes or stash them before you can switch branches. Aborting
$ git checkout -b experimental # creates new branch 'experimental' $ echo 'hello world' > file.txt $ git add file.txt $ git commit -m "added file.txt" $ git checkout master # master does not have file.txt $ echo 'goodbye world' > file.txt $ git checkout experimental error: The following untracked working tree files would be overwritten by checkout: file.txt Please move or remove them before you can switch branches. Aborting
$ echo 'experimental change' >> file.txt # change to existing tracked file # I want to save these, but not on master $ git checkout -b experiment M file.txt Switched to branch 'experiment' $ git add file.txt $ git commit -m "possible modification for file.txt"
- 3 回答
- 0 關(guān)注
- 2122 瀏覽
添加回答
舉報
0/150
提交
取消