![](/img/trans.png)
[英]Remove unstaged, uncommitted files in git when checking out a different branch
[英]Files getting deleted as unstaged changes when checking out to another branch
合並后,我面臨一個奇怪的問題,即簽出到另一個分支會導致未暫存的更改 - 刪除某些文件。
PS>git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
PS>git checkout master_test
Switched to branch 'master_test'
Your branch is up to date with 'origin/master_test'.
PS>git status
On branch master_test
Your branch is up to date with 'origin/master_test'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: Modules/CimCmdlets/CimCmdlets.psd1
...
由於它僅在我執行git checkout
時發生,因此我假設文件已被 git 本身刪除。 如何確定這些文件被刪除的原因?
由於默認情況下 Windows 不區分大小寫,因此您可能有兩個名稱相同但大小寫不同的文件。 WinMerge 等 Windows 工具無法發現這一點,因為只有兩個文件中的一個被檢出。
我將在默認情況下也不區分大小寫的 OS X 上演示這一點。 我在區分大小寫的卷上創建了一個 Git 存儲庫,其中包含FOO
和foo
文件。 現在我正在一個不區分大小寫的文件系統上檢查它。
$ git clone /Volumes/case_sensitive/tmp/test
Cloning into 'test'...
done.
warning: the following paths have collided (e.g. case-sensitive paths
on a case-insensitive filesystem) and only one from the same
colliding group is in the working tree:
'FOO'
'foo'
$ ls
.git foo
$ git status
On branch master
Your branch is up to date with 'origin/master'.
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: FOO
no changes added to commit (use "git add" and/or "git commit -a")
您可以使用git ls-files
獲得文件的真實列表。
$ git ls-files
FOO
foo
我們可以通過將一個文件重命名為唯一的文件來解決這個問題...
$ git mv foo foo2
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: foo -> foo2
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: FOO
現在,我們可以檢出另一個文件。
$ git co FOO
Updated 1 path from the index
$ ls
.git FOO foo2
$ git st
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: foo -> foo2
在您將使用foo
任何代碼修復為現在使用foo2
,提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.