从 Git 存储库中删除文件,而不将其从本地文件系统中删除

发布于 2024-07-28 23:54:27 字数 158 浏览 14 评论 0原文

我想从我的存储库中删除一个文件。

git rm file_to_remove.txt

将从存储库中删除该文件,但也会从本地文件系统中删除该文件。 如何从存储库中删除此文件而不删除该文件的本地副本?

I want to remove a file from my repository.

git rm file_to_remove.txt

will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file from the repo without deleting my local copy of the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

少跟Wǒ拽 2024-08-04 23:54:27

git rm 文档指出:

当给出 --cached 时,暂存的内容必须与分支的尖端或磁盘上的文件匹配,从而允许仅从索引中删除文件。

因此,对于单个文件:

git rm --cached file_to_remove.txt

和单个目录:

git rm --cached -r directory_to_remove

The git rm documentation states:

When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

So, for a single file:

git rm --cached file_to_remove.txt

and for a single directory:

git rm --cached -r directory_to_remove
爱的那么颓废 2024-08-04 23:54:27

要从存储库中删除整个文件夹(如 Resharper 文件),请执行以下操作:

git rm -r --cached folderName

我已提交一些 resharper 文件,并且不希望其他项目用户保留这些文件。

To remove an entire folder from the repo (like Resharper files), do this:

git rm -r --cached folderName

I had committed some resharper files, and did not want those to persist for other project users.

坏尐絯℡ 2024-08-04 23:54:27

要从基于 .gitignore 的存储库中删除文件,而不从本地文件系统中删除它们:

git rm --cached `git ls-files -i -c -X .gitignore`

对于 Windows Powershell:

git rm --cached $(git ls-files -i -c -X .gitignore)

To remove files from the repository based on .gitignore, without deleting them from the local file system:

git rm --cached `git ls-files -i -c -X .gitignore`

For Windows Powershell:

git rm --cached $(git ls-files -i -c -X .gitignore)
小忆控 2024-08-04 23:54:27

根据我的回答:https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in- my-github-repository

要仅从 git 存储库而不是从本地删除文件夹/目录或文件,请尝试 3 个简单的步骤。


删除目录的步骤

git rm -r --cached File-or-FolderName
git commit -m "Removed folder from repository"
git push origin master

在下次提交中忽略该文件夹的步骤

要在下一次提交中忽略该文件夹,请在根目录中创建一个名为 .gitignore 的文件
并将该文件夹名称放入其中。 你想放多少就放多少

.gitignore 文件,如下所示

/FolderName

remove directory

As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository

To remove folder/directory or file only from git repository and not from the local try 3 simple steps.


Steps to remove directory

git rm -r --cached File-or-FolderName
git commit -m "Removed folder from repository"
git push origin master

Steps to ignore that folder in next commits

To ignore that folder from next commits make one file in root named .gitignore
and put that folders name into it. You can put as many as you want

.gitignore file will be look like this

/FolderName

remove directory

明月夜 2024-08-04 23:54:27

更通用的解决方案:

  1. 编辑 .gitignore 文件。

    echo mylogfile.log>> .gitignore

  2. 从索引中删除所有项目。

    git rm -r -f --cached 。

  3. 重建索引。

    git add .

  4. 进行新提交

    git commit -m“已删除 mylogfile.log”

A more generic solution:

  1. Edit .gitignore file.

    echo mylogfile.log >> .gitignore

  2. Remove all items from index.

    git rm -r -f --cached .

  3. Rebuild index.

    git add .

  4. Make new commit

    git commit -m "Removed mylogfile.log"

指尖凝香 2024-08-04 23:54:27

另外,如果您提交了敏感数据(例如包含密码的文件),您应该将其从存储库的历史记录中完全删除。 这是解释如何执行此操作的指南:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here's a guide explaining how to do that:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

锦欢 2024-08-04 23:54:27

Git 允许您忽略这些文件,假设它们没有更改。 这
通过运行 git update-index --assume-unchanged 来完成
path/to/file.txt 命令。 一旦将文件标记为这样,git 将
完全忽略对该文件的任何更改; 他们不会出现的时候
运行 git status 或 git diff,它们也不会被提交。

(来自 https://help.github.com/articles/ignoring-files

因此,不要删除它,但永远忽略它的变化。
我认为这只适用于本地,因此同事仍然可以看到它的更改,除非他们运行与上面相同的命令。 (但仍然需要验证这一点。)

注意:这不是直接回答问题,而是基于其他答案的评论中的后续问题。

Git lets you ignore those files by assuming they are unchanged. This
is done by running the git update-index --assume-unchanged
path/to/file.txt
command. Once marking a file as such, git will
completely ignore any changes on that file; they will not show up when
running git status or git diff, nor will they ever be committed.

(From https://help.github.com/articles/ignoring-files)

Hence, not deleting it, but ignoring changes to it forever.
I think this only works locally, so co-workers can still see changes to it unless they run the same command as above. (Still need to verify this though.)

Note: This isn't answering the question directly, but is based on follow up questions in the comments of the other answers.

嗼ふ静 2024-08-04 23:54:27

如果您只想取消跟踪文件并且不删除本地和远程存储库,请使用以下命令:

git update-index --assume-unchanged  file_name_with_path

If you want to just untrack a file and not delete from local and remote repo then use this command:

git update-index --assume-unchanged  file_name_with_path
痴情换悲伤 2024-08-04 23:54:27

忽略文件,从 git 中删除文件,更新 git (用于删除)。

注意:这不处理敏感信息的历史记录。

这个过程肯定需要对 git 的运行情况有一定的了解。 随着时间的推移,我学会了执行以下过程:

1)忽略文件

  • 添加或更新项目 .gitignore 以忽略它们 - 在许多情况下,例如您的情况,父目录,例如 log/ 将是要使用的正则表达式。
  • 提交并推送 .gitignore 文件更改(请注意,不确定是否需要推送,如果完成也没有什么坏处)。

2) 从 git 中删除文件(仅)。

  • 现在使用 git rm --cached some_dir/ 从 git (仅)删除文件,
  • 检查它们是否仍然保留在本地(它们应该!)。

3) 添加并提交该更改(本质上,这是对“添加”删除内容的更改,尽管“添加”命令令人困惑!)

  • git add .
  • git commit -m"removal"

Ignore the files, remove the files from git, update git (for the removal).

Note : this does not deal with history for sensitive information.

This process definitely takes some undertanding of what is going on with git. Over time, having gained that, I've learned to do processes such as:

1) Ignore the files

  • Add or update the project .gitignore to ignore them - in many cases such as yours, the parent directory, e.g. log/ will be the regex to use.
  • commit and push that .gitignore file change (not sure if push needed mind you, no harm if done).

2) Remove the files from git (only).

  • Now remove the files from git (only) with git rm --cached some_dir/
  • Check that they still remain locally (they should!).

3) Add and commit that change (essentially this is a change to "add" deleting stuff, despite the otherwise confusing "add" command!)

  • git add .
  • git commit -m"removal"
天涯沦落人 2024-08-04 23:54:27

以上答案对我不起作用。 我使用 filter-branch 删除所有已提交的文件。

使用以下命令从 git 存储库中删除文件: 使用

git filter-branch --tree-filter 'rm  file'

以下命令从 git 存储库中删除文件夹:

git filter-branch --tree-filter 'rm -rf directory'

这将从所有提交中删除目录或文件。

您可以使用以下方式指定提交:

git filter-branch --tree-filter 'rm -rf directory' HEAD

或范围:

git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD

要将所有内容推送到远程,您可以执行以下操作:

git push origin master --force

Above answers didn't work for me. I used filter-branch to remove all committed files.

Remove a file from a git repository with:

git filter-branch --tree-filter 'rm  file'

Remove a folder from a git repository with:

git filter-branch --tree-filter 'rm -rf directory'

This removes the directory or file from all the commits.

You can specify a commit by using:

git filter-branch --tree-filter 'rm -rf directory' HEAD

Or an range:

git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD

To push everything to remote, you can do:

git push origin master --force
神仙妹妹 2024-08-04 23:54:27

我想添加 @bdonlan 已接受的答案。


不要使用此答案来删除远程上存在的文件。

git rm --cached filename

答案应该做什么?

它应该从本地暂存区域中删除您在之前的某些提交中错误提交的一些文件。

  1. 并且还没有推送到远程。
  2. 如果远程推送,其他人就不会关心这些变化。

它将文件从已跟踪

I would like to add to the accepted answer of @bdonlan.


DON'T USE THIS ANSWER TO REMOVE FILE(S) THAT EXISTS ON REMOTE.

git rm --cached filename

What the answer is supposed to do?

It is supposed to remove some files from the local staged area that you have mistakenly committed in some previous commit(s).

  1. And have not pushed to the remote.
  2. And if pushed on remote, others don't care about those changes.

It moves files from Tracked ???????? Untracked state by that what I mean is, it deletes the files and adds them again.

So, git doesn't know about them anymore.


What could go wrong?

On remote, there is ???????? such thing as an ???????????????????????????????????? ????????????????????, there is ???????????????? ???????????????????????????? ???????????????????? and that results in havoc.


Why?

When collaborating with the team, if you ???????????????? such changes up to remote it will delete those changes on remote and all the team who takes a ???????????????? from ???????????????????????? ???????????????? ???????????????????? ???????????????????????????????? ???????? ???????????????????? ???????????????????? ???????????????????? ???????????? ???????????????? ???????????????? ???????????????????????????? ???????????????? ???????????????? ????????????????????????.

Summary: You removed files from staged and then pushed them will result in the deletion of files on the collaborating team's local repository as well (???????????????????? ???????????? ???????????????? ???????????????????? ???????????????????? ???????????????????????????????????? ???????? ???????????? ???????????????????????????????????? ????????????????????, ???????????? ???????????? ???????????????????? ???????? will be ????????????????. )

甜宝宝 2024-08-04 23:54:27

我使用以下简单方法从 git 中删除了一些与 IDE 相关的文件,因为它们使存储库看起来很混乱。

注意:这不会将它们从 git 历史记录中删除。

注意:如果您不小心提交了密码,首先要做的就是更改这些密码。

  • 提交或恢复任何本地更改。
  • 将文件本地备份到另一个文件夹。
  • 从本地存储库中删除文件。
  • 提交已删除文件的更改。
  • 编辑 .gitignore 文件以列出文件/文件夹。
  • 将文件本地复制回其原始位置。
  • 运行 git status 并检查文件是否未列出。
  • 将更改提交到 .gitignore

I used the following simple method to remove some IDE-related files from git as they made the repo look cluttered.

Note: This doesn't remove them from the git history.

Note: If you've accidentially committed passwords the first thing to do is change those passwords.

  • Commit or revert any local changes.
  • Backup the files locally to another folder.
  • Remove the files from your local repo.
  • Commit the change where the files are removed.
  • Edit your .gitignore file to list the files/folders.
  • Copy the files locally back into their original location.
  • Run git status and check that the files are not listed.
  • Commit the change to .gitignore
单调的奢华 2024-08-04 23:54:27

这取决于您所说的从 git 中“删除”的含义。 :)

您可以使用 git rm --cached 查看更多详细信息。 当您取消暂存某些内容时,这意味着不再跟踪它,但这不会从以前的提交中删除该文件。

如果您想做的不仅仅是取消暂存文件,例如从所有以前的提交中删除敏感数据,您将需要考虑使用 BFG 仓库清理器

This depends on what you mean by 'remove' from git. :)

You can unstage a file using git rm --cached see for more details. When you unstage something, it means that it is no longer tracked, but this does not remove the file from previous commits.

If you want to do more than unstage the file, for example to remove sensitive data from all previous commits you will want to look into filtering the branch using tools like the BFG Repo-Cleaner.

痴情换悲伤 2024-08-04 23:54:27

由于 rm --cache 会删除远程存储库中的文件,因此您可以使用 update-index 代替
请参阅: https://learn.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=visual-studio-2019

since rm --cache will delete files in the remote repository, you could use update-index instead
see: https://learn.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=visual-studio-2019

清秋悲枫 2024-08-04 23:54:27

如果您想删除存储库中的文件而不是文件系统中的文件,则意味着您不想保留该文件的跟踪。 在 Git 中,您有 4 个替代方案(如此处指定)。

  1. 您可以使用 .gitignore 文件。 在这种情况下,文件/文件夹在本地未跟踪。

  2. 您可以按照与上一个案例类似的方式编辑.git/info/exclude。 这里的区别在于该文件在本地未跟踪。

  3. 您可以使用 git update-index Should-unchanged命令来跟踪文件,但本地更改将被忽略。

  4. 您可以使用 git update-index Skip-worktree命令来跟踪文件,但本地更改将被忽略。

第3点和第4点看似做同样的事情,但也有区别。 在此链接中,您可以找到有关这些点的行为的广泛测试列表。 总结一下差异,我们可以说标志 assume-unchanged 假设开发人员不再触及文件。 标志 skip-worktree 指示 git 不再触及特定文件。 当您向存储库添加配置文件但不想跟踪其更改时,它非常有用。

If you want to delete a file in the repository but not in the filesystem, it means that you do not want to keep trace of this file. In Git you have 4 alternatives (as specified here).

  1. You can use a .gitignore file. In this case the file/folder is locally untracked.

  2. You can edit .git/info/exclude in a similar way as the previous case. Here the difference is that the file is locally untracked.

  3. You can use the git update-index assume-unchanged <filename> command to track a file, but the local changes are ignored.

  4. You can use git update-index skip-worktree <filename> command to track a file, but the local changes are ignored.

Points 3 and 4 appear to do the same thing, but there are difference. At this link you can find an extensive list of tests about the behavior of these points. To summarize the differences we can say that the flag assume-unchanged assumes that a developer does not touch a file anymore. The flag skip-worktree instructs git to not touch the specific file anymore. It is useful when you add to the repository a configuration file, but you do not want to track its changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文