当当前分支上有未提交的更改时,签出另一个分支大多数情况下,当我尝试签出另一个现有分支时,Git不允许我在当前分支上有一些未提交的更改。因此,我必须首先提交或保存这些更改。但是,偶尔Git允许我签出另一个分支,而无需提交或保存这些更改,并且它会将这些更改携带到第一分支签出。这里的规矩是什么?这些改变是阶段性的还是非阶段性的?把这些变化带到另一个分支对我来说没有任何意义,为什么git有时会允许它呢?也就是说,它在某些情况下有用吗?
3 回答
蝴蝶刀刀
TA贡献1801条经验 获得超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 关注
- 2096 浏览
添加回答
举报
0/150
提交
取消