如何在不刪除內容的情況下取消大量文件

本文翻譯自:How to unstage large number of files without deleting the content

I accidentally added a lot of temporary files using git add -A 我不小心使用git add -A添加了很多臨時文件

I managed to unstage the files using the following commands and managed to remove the dirty index. 我設法使用以下命令取消暫存文件,並設法刪除髒索引。

git ls-files -z | xargs -0 rm -f
git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached

The above commands are listed in the git help rm . 上面的命令列在git help rm But sadly, my files were also deleted on execution, even though I had given cache option. 但遺憾的是,我的文件在執行時也被刪除了,即使我已經給出了緩存選項。 How can I clear the index without losing the content? 如何在不丟失內容的情況下清除索引?

Also it would be helpful if someone can explain the way this pipe operation works. 如果有人能解釋這個管道操作的工作方式,也會有所幫助。


#1樓

參考:https://stackoom.com/question/Tnyh/如何在不刪除內容的情況下取消大量文件


#2樓

git stash && git stash pop

#3樓

If HEAD isn't set, you can also do 如果未設置HEAD,您也可以這樣做

git rm -rf --cached .

to unstage everything. 展開一切。 This is effectively the same as sehe's solution, but avoids mucking with Git internals. 這實際上與sehe的解決方案相同,但避免與Git內部結合。


#4樓

git reset

If all you want is to undo an overzealous "git add" run: 如果你想要的是撤消一個過於熱心的“git add”運行:

git reset

Your changes will be unstaged and ready for you to re-add as you please. 您的更改將無法進行,並隨時可以重新添加。


DO NOT RUN git reset --hard . 不要運行git reset --hard

It will not only unstage your added files, but will revert any changes you made in your working directory. 它不僅會取消暫存您添加的文件,還會還原您在工作目錄中所做的任何更改。 If you created any new files in working directory, it will not delete them though. 如果您在工作目錄中創建了任何新文件,它將不會刪除它們。


#5樓

If you want to unstage all the changes use below command, 如果要使用以下命令取消暫存所有更改,

git reset --soft HEAD

In the case you want to unstage changes and revert them from the working directory, 如果您要取消暫停更改並將其從工作目錄還原,

git reset --hard HEAD

#6樓

Warning: do not use the following command unless you want to lose uncommitted work! 警告:除非您想丟失未提交的工作,否則請勿使用以下命令!

Using git reset has been explained, but you asked for an explanation of the piped commands as well, so here goes: 已經解釋了使用git reset ,但是你也要求解釋管道命令,所以這裏有:

git ls-files -z | xargs -0 rm -f
git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached

The command git ls-files lists all files git knows about. 命令git ls-files列出了git知道的所有文件。 The option -z imposes a specific format on them, the format expected by xargs -0 , which then invokes rm -f on them, which means to remove them without checking for your approval. 選項-z對它們施加特定的格式, xargs -0期望的格式,然後在它們上調用rm -f ,這意味着刪除它們而不檢查您的批准。

In other words, "list all files git knows about and remove your local copy". 換句話說,“列出git知道並刪除本地副本的所有文件”。

Then we get to git diff , which shows changes between different versions of items git knows about. 然後我們得到git diff ,它顯示了git知道的不同版本項之間的變化。 Those can be changes between different trees, differences between local copies and remote copies, and so on. 這些可以是不同樹之間的更改,本地副本和遠程副本之間的差異,等等。
As used here, it shows the unstaged changes; 如此處所示,它顯示了未分級的變化; the files you have changed but haven't committed yet. 您已更改但尚未提交的文件。 The option --name-only means you want the (full) file names only and --diff-filter=D means you're interested in deleted files only. 選項--name-only表示--name-only需要(完整)文件名,而--diff-filter=D表示您只對已刪除的文件感興趣。 (Hey, didn't we just delete a bunch of stuff?) This then gets piped into the xargs -0 we saw before, which invokes git rm --cached on them, meaning that they get removed from the cache, while the working tree should be left alone — except that you've just removed all files from your working tree. (嘿,我們不是剛剛刪除了一堆東西嗎?)然後通過管道輸入我們之前看到的xargs -0 ,它會調用git rm --cached在它們上面,這意味着它們會從緩存中移除,同時工作樹應該保持不變 - 除了你剛從工作樹中刪除了所有文件。 Now they're removed from your index as well. 現在它們也從索引中刪除了。

In other words, all changes, staged or unstaged, are gone, and your working tree is empty. 換句話說,所有已暫存或未暫存的更改都將消失,並且您的工作樹爲空。 Have a cry, checkout your files fresh from origin or remote, and redo your work. 哭泣,從原產地或遠程檢查你的文件,並重做你的工作。 Curse the sadist who wrote these infernal lines; 詛咒寫下這些地獄線的虐待狂; I have no clue whatsoever why anybody would want to do this. 我不知道爲什麼有人想要這樣做。


TL;DR: you just hosed everything; TL; DR:你只是灌輸了一切; start over and use git reset from now on. 重新開始並從現在開始使用git reset

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章