如何从我的本地 git 状态中删除已删除的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8085830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove deleted files from showing up in my local git status?
提问by uwe
A developer added an image directory on the server without adding that folder to the .gitignore file. When I did a git pull it pulled those files (hundreds of files). I added the folder to the .gitignore on the server but not my local system. I then deleted the files locally (permanently). When I do a git status all those deleted files still show up. How can I suppress them from showing up?
开发人员在服务器上添加了一个图像目录,而没有将该文件夹添加到 .gitignore 文件中。当我执行 git pull 时,它会拉出这些文件(数百个文件)。我将该文件夹添加到服务器上的 .gitignore 而不是我的本地系统。然后我在本地(永久)删除了这些文件。当我执行 git status 时,所有已删除的文件仍然显示。我怎样才能抑制它们出现?
Update:thanks for all the great help. To make sure there is no misunderstanding: I do want to keep the files on the server; I just want to remove them from git.
更新:感谢所有的大力帮助。为了确保没有误解:我确实想将文件保留在服务器上;我只想从 git 中删除它们。
采纳答案by Andy
Since they've already been committed to the repository it's a bit more work, but still doable.
由于他们已经提交到存储库,因此需要做更多的工作,但仍然可行。
The best walkthrough that I've found on this procedure is Github's guide on Removing Sensitive Data. Since you want to keep the files, you have a step to do before and after the guide.
我在此过程中找到的最佳演练是 Github 的删除敏感数据指南。由于您想保留文件,因此您在指南之前和之后都有一个步骤。
Before you start this process, copy the files out of your working directory. Once you have finished purging the repo of them, copy them back in and add them to your .gitignore.
在开始此过程之前,请将文件从工作目录中复制出来。完成清除它们的 repo 后,将它们复制回并将它们添加到您的 .gitignore。
回答by NightFury13
I find this to be the easiest way of doing the task.
我发现这是完成任务的最简单方法。
$> git add -u <directory-path> (or you may use --update)
Original State :
原始状态:
$> git status .
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: .ipynb_checkpoints/Untitled-checkpoint.ipynb
deleted: CVPR 1 hr tute-Backup.ipynb
deleted: cifar10-test.t7
deleted: cifar10-train.t7
deleted: cifar10torchsmall.zip
deleted: create_advr.lua
deleted: criterion_cifar10_lr_0.001_epoch_13.t7
deleted: train_cifar10.lua
deleted: trained_net_cifar10_lr_0.001_epoch_13.t7
So, to remove all deleted files from my current directory, I use.
因此,要从当前目录中删除所有已删除的文件,我使用。
$> git add -u .
$> git commit -m "removing deleted files from tracking"
$> git push origin master
Final State :
最终状态:
$> git status .
On branch master
nothing to commit, working directory clean
Hope this helps :)
希望这可以帮助 :)
回答by Matt Ball
You probably need to git rm
the deleted files. From Pro Git:
您可能需要git rm
删除已删除的文件。来自Pro Git:
Removing Files
To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The
git rm
command does that and also removes the file from your working directory so you don't see it as an untracked file next time around.If you simply remove the file from your working directory, it shows up under the “Changed but not updated” (that is, unstaged) area of your
git status
output:$ rm grit.gemspec $ git status # On branch master # # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # # deleted: grit.gemspec #
Then, if you run
git rm
, it stages the file's removal:$ git rm grit.gemspec rm 'grit.gemspec' $ git status # On branch master # # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: grit.gemspec #
The next time you commit, the file will be gone and no longer tracked. If you modified the file and added it to the index already, you must force the removal with the
-f
option. This is a safety feature to prevent accidental removal of data that hasn't yet been recorded in a snapshot and that can't be recovered from Git.
删除文件
要从 Git 中删除文件,您必须从跟踪的文件中删除它(更准确地说,从暂存区中删除它)然后提交。该
git rm
命令会执行此操作,还会从您的工作目录中删除该文件,因此您下次不会将其视为未跟踪的文件。如果您只是从工作目录中删除文件,它会显示在输出的“已更改但未更新”(即未暂存)区域下
git status
:$ rm grit.gemspec $ git status # On branch master # # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # # deleted: grit.gemspec #
然后,如果您运行
git rm
,它会暂存文件的删除:$ git rm grit.gemspec rm 'grit.gemspec' $ git status # On branch master # # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: grit.gemspec #
下次提交时,该文件将消失并且不再被跟踪。如果您修改了文件并将其添加到索引中,则必须使用该
-f
选项强制删除。这是一项安全功能,可防止意外删除尚未记录在快照中且无法从 Git 恢复的数据。
回答by Mark Fisher
Updated after your comments.
在您的评论后更新。
If you want to keep the images on the server, but remove the files from git then you need to add the --cached
flag to git rm
.
如果您想将图像保留在服务器上,但从 git 中删除文件,那么您需要将--cached
标志添加到git rm
.
cd images
git rm --cached *
cd ..
echo "images" > .gitignore
git add .
git commit -m "removed image files from git only"
git push
if you want people to create the images directory when they pull (but have no files), then add a images/.gitignore
with the single line !.gitignore
如果您希望人们在拉取时创建图像目录(但没有文件),请添加images/.gitignore
单行!.gitignore
Note, this won't remove them from the history. For that, i would look at this SO answer
请注意,这不会将它们从历史记录中删除。为此,我会看看这个 SO 答案
回答by Duncan Babbage
Add the folder to your .gitignore locally as well.
将该文件夹也添加到本地的 .gitignore 中。
回答by Tasneem Haider
git rm <deleted file path>
and then git commit
will clean the working directory.
git rm <deleted file path>
然后git commit
将清理工作目录。