如何从 Git 存储库中删除文件?

发布于 2024-08-17 17:18:45 字数 43 浏览 14 评论 0原文

如何从我的存储库中删除“file1.txt”

How can I delete "file1.txt" from my repository?

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

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

发布评论

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

评论(26

渡你暖光 2024-08-24 17:18:45

使用 git rm

如果您想从 Git 存储库和文件系统中删除文件,请使用:

git rm file1.txt
git commit -m "remove file1.txt"

但如果您只想从 Git 存储库中删除文件而不从文件系统中删除它,请使用:

git rm --cached file1.txt
git commit -m "remove file1.txt"

并推送对远程仓库的更改

git push origin branch_name

Use git rm.

If you want to remove the file from the Git repository and the filesystem, use:

git rm file1.txt
git commit -m "remove file1.txt"

But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:

git rm --cached file1.txt
git commit -m "remove file1.txt"

And to push changes to remote repo

git push origin branch_name
飘过的浮云 2024-08-24 17:18:45

git rm file.txt 会从存储库中删除该文件,但也会从本地文件系统中删除该文件

要从存储库中删除文件并且从本地文件系统中删除它,请使用:
git rm --cached file.txt

下面的具体情况是我使用 git 维护公司网站的版本控制,但“mickey”目录是一个 tmp 文件夹,用于与 CAD 共享私有内容开发商。当他需要巨大的文件时,我创建了一个私有的、未链接的目录,并将文件上传到那里,以便他通过浏览器获取。我忘记了我这样做了,后来我从网站的基本目录中执行了 git add -A 。随后,git status 显示需要提交的新文件。现在我需要从 git 的跟踪和版本控制中删除它们...

下面的示例输出来自我刚刚发生的事情,我无意中删除了 .003 文件。值得庆幸的是,我不在乎 .003 的本地副本发生了什么,但当前更改的其他一些文件是我刚刚对网站进行的更新,如果在本地文件系统! “本地文件系统”=实时网站(不是一个很好的做法,但确实是现实)

[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   shop/mickey/mtt_flange_SCN.7z.001
#   modified:   shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001  shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001  shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    shop/mickey/mtt_flange_SCN.7z.002
#   deleted:    shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
#   modified:   shop/mickey/mtt_flange_SCN.7z.001
[~/www]$

更新:这个答案获得了一些流量,所以我想我应该提到我的其他 Git 答案分享一些很棒的资源:此页面有一个图形可以帮助揭开神秘面纱给我的。 “Pro Git”一书已上线,对我帮助很大。

git rm file.txt removes the file from the repo but also deletes it from the local file system.

To remove the file from the repo and not delete it from the local file system use:
git rm --cached file.txt

The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a git add -A from the website's base directory. Subsequently, git status showed the new files needing committing. Now I needed to delete them from git's tracking and version control...

Sample output below is from what just happened to me, where I unintentionally deleted the .003 file. Thankfully, I don't care what happened to the local copy to .003, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).

[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   shop/mickey/mtt_flange_SCN.7z.001
#   modified:   shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001  shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001  shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ 
[~/www]$ 
[~/www]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    shop/mickey/mtt_flange_SCN.7z.002
#   deleted:    shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
#   modified:   shop/mickey/mtt_flange_SCN.7z.001
[~/www]$

Update: This answer is getting some traffic, so I thought I'd mention my other Git answer shares a couple of great resources: This page has a graphic that help demystify Git for me. The "Pro Git" book is online and helps me a lot.

那片花海 2024-08-24 17:18:45

首先,如果您使用 git rm,特别是对于多个文件,请考虑任何通配符将由 shell 解析,而不是由 git 命令解析。

git rm -- *.anExtension
git commit -m "remove multiple files"

但是,如果您的文件已在 GitHub 上,您可以(自 2013 年 7 月起)直接从Web GUI中删除它!

只需查看存储库中的任何文件,单击顶部的垃圾桶图标,然后像任何其他基于 Web 的编辑一样提交删除。

然后在本地存储库上“git pull”,这也会在本地删除该文件。
这使得这个答案成为从 git repo 删除文件的(迂回)方式?
(更不用说 GitHub 上的文件位于“git 存储库”中)


< img src="https://i.sstatic.net/259au.jpg" alt="删除按钮">

(提交将反映该文件的删除):

提交删除

就这样,它就消失了。

如需有关这些功能的帮助,请务必阅读我们关于创建, 移动, 重命名,以及删除 文件。

注意:由于它是一个版本控制系统,如果您稍后需要恢复文件,Git 始终会为您提供支持。

最后一句意味着删除的文件仍然是历史记录的一部分,您可以轻松恢复它(但还不能通过 GitHub Web 界面):

请参阅“在 Git 存储库中恢复已删除的文件"。

First, if you are using git rm, especially for multiple files, consider any wildcard will be resolved by the shell, not by the git command.

git rm -- *.anExtension
git commit -m "remove multiple files"

But, if your file is already on GitHub, you can (since July 2013) directly delete it from the web GUI!

Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.

Then "git pull" on your local repo, and that will delete the file locally too.
Which makes this answer a (roundabout) way to delete a file from git repo?
(Not to mention that a file on GitHub is in a "git repo")


delete button

(the commit will reflect the deletion of that file):

commit a deletion

And just like that, it’s gone.

For help with these features, be sure to read our help articles on creating, moving, renaming, and deleting files.

Note: Since it’s a version control system, Git always has your back if you need to recover the file later.

The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):

See "Restore a deleted file in a Git repo".

ヤ经典坏疍 2024-08-24 17:18:45

这是唯一对我有用的选择。

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'

注意:将 *.sql 替换为您的文件名或文件类型。请务必小心,因为这将遍历每次提交并删除此文件类型。

编辑:
注意 - 执行此命令后,您将无法推送或拉取 - 您将看到“不相关的历史记录”被拒绝,您可以使用“git push --force -u origin master”来推送或拉取

This is the only option that worked for me.

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'

Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out.

EDIT:
pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull

等风也等你 2024-08-24 17:18:45

此外,如果它是要删除的文件夹及其后续子文件夹或文件,请使用:

git rm -r foldername

Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:

git rm -r foldername
爱的十字路口 2024-08-24 17:18:45

更一般地说,git help 至少可以帮助解决如下简单问题:

zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   :
   rm         Remove files from the working tree and from the index

More generally, git help will help with at least simple questions like this:

zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   :
   rm         Remove files from the working tree and from the index
听不够的曲调 2024-08-24 17:18:45

Greg Hewgill 的答案,由 Johannchopin 帮助了我,因为我不关心将该文件从历史记录中完全删除。
就我而言,它是一个目录,因此我所做的唯一更改是使用:

git rm -r --cached myDirectoryName

而不是“git rm --cached file1.txt”
..其次:

git commit -m "deleted myDirectoryName from git"
git push origin branch_name

感谢Greg Hewgill约翰肖平

The answer by Greg Hewgill, that was edited by Johannchopin helped me, as I did not care about removing the file from the history completely.
In my case, it was a directory, so the only change I did was using:

git rm -r --cached myDirectoryName

instead of "git rm --cached file1.txt"
..followed by:

git commit -m "deleted myDirectoryName from git"
git push origin branch_name

Thanks Greg Hewgill and Johannchopin!

海未深 2024-08-24 17:18:45

如果您想从存储库中删除文件,但将其保留在文件系统中(将不被跟踪):

bykov@gitserver:~/temp> git rm --cached file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"

如果您想从存储库和文件系统中删除文件,则有两个选项:

  1. 如果该文件在索引中没有暂存的更改:

    bykov@gitserver:~/temp> git rm 文件1.txt
    bykov@gitserver:~/temp> git commit -m“删除file1.txt”
    
  2. 如果文件在索引中暂存更改:

    bykov@gitserver:~/temp> git rm -f file1.txt
    bykov@gitserver:~/temp> git commit -m“删除file1.txt”
    

If you want to delete the file from the repo, but leave it in the the file system (will be untracked):

bykov@gitserver:~/temp> git rm --cached file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"

If you want to delete the file from the repo and from the file system then there are two options:

  1. If the file has no changes staged in the index:

    bykov@gitserver:~/temp> git rm file1.txt
    bykov@gitserver:~/temp> git commit -m "remove file1.txt"
    
  2. If the file has changes staged in the index:

    bykov@gitserver:~/temp> git rm -f file1.txt
    bykov@gitserver:~/temp> git commit -m "remove file1.txt"
    
丑疤怪 2024-08-24 17:18:45

从现在开始, git rm 只会删除该分支上的文件,但它仍然保留在历史记录中,并且 git 会记住它。

正确的方法是使用 git filter-branch ,正如其他人在这里提到的那样。它将重写分支历史记录中的每个提交以删除该文件。

但是,即使这样做之后,git 仍然可以记住它,因为可以在 reflog、远程、标签等中引用它。

如果你想一步彻底抹掉它,我推荐你使用gitforget-blob

https://ownyourbits.com/2017/01/18/completely-remove-a-file-from -a-git-repository-with-git-forget-blob/

这很简单,只需执行 gitforget-blob file1.txt 即可。

这将删除所有引用,执行 git filter-branch,最后运行 git 垃圾收集器 git gc 以完全删除存储库中的此文件。

git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.

The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.

But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.

If you want to completely obliterate it in one step, I recommend you to use git forget-blob

https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

It is easy, just do git forget-blob file1.txt.

This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.

叶落知秋 2024-08-24 17:18:45

根据文档

git rm --cached file1.txt

当涉及敏感数据时,最好不要说您删除了该文件,而只是将其包含在最后一次已知的提交中:

0。修改最后一次提交

git commit --amend -CHEAD

如果要从所有 git 历史记录中删除该文件,请根据 文档 您应该执行以下操作:

1.将其从本地历史记录中删除

git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \  --prune-empty --tag-name-filter cat -- --all
# Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
  1. 不要忘记将此文件包含在 .gitignore (如果它是您不想共享的文件(例如密码...):
echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"

3. 如果您需要从远程删除

git push origin --force --all

如果您还需要从标签版本中删除它:

git push origin --force --tags

According to the documentation.

git rm --cached file1.txt

When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:

0. Amend last commit

git commit --amend -CHEAD

If you want to delete the file from all git history, according to the documentation you should do the following:

1. Remove it from your local history

git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \  --prune-empty --tag-name-filter cat -- --all
# Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
  1. Don't forget to include this file in .gitignore (If it's a file you never want to share (such as passwords...):
echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"

3. If you need to remove from the remote

git push origin --force --all

4. If you also need to remove it from tag releases:

git push origin --force --tags
勿忘初心 2024-08-24 17:18:45

如果您想使用 rm 命令从本地文件夹中删除文件,然后将更改推送到远程服务器,则还有另一种方法。

rm file1.txt

git commit -a -m "Deleting files"

git push origin master

Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.

rm file1.txt

git commit -a -m "Deleting files"

git push origin master
怪我鬧 2024-08-24 17:18:45

注意:如果您只想从 git 中删除文件,请使用以下命令:

git rm --cached file1.txt

如果您还想从硬盘中删除:

git rm file1.txt

如果您想删除一个文件夹(该文件夹可能包含几个文件),那么您应该使用递归命令删除,如下所示:

git rm -r foldername

如果您想删除另一个文件夹中的文件夹

git rm -r parentFolder/childFolder

那么,您可以照常commitpush。但是,如果您想恢复已删除的文件夹,可以按照以下步骤操作:恢复已删除的文件从 git 是可能的。

来自文档:

<前><代码>git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <文件>...

选项

<文件>...

要删除的文件。可以给出文件团(例如*.c)来删除所有匹配的文件。如果您希望 Git 扩展文件全局字符,您

可能需要对它们进行外壳转义。前导目录名称(例如 dir to
删除 dir/file1 和 dir/file2) 可以删除所有文件
目录,以及递归所有子目录,但这需要
显式给出的 -r 选项。
<代码>-f
--force

覆盖最新检查。

<代码>-n
--dry-run

实际上不删除任何文件。相反,只需显示它们是否存在于索引中,否则将被命令删除。

-r

在给定主目录名称时允许递归删除。

<代码>--

此选项可用于将命令行选项与文件列表分开(当文件名可能被误认为是时很有用)

命令行选项)。
--缓存

使用此选项仅从索引中取消暂存和删除路径。工作树文件,无论是否修改,都将被保留。

--忽略不匹配

即使没有匹配的文件,也会以零状态退出。

-q
--安静

git rm 通常为每个删除的文件输出一行(以 rm 命令的形式)。该选项会抑制该输出。

阅读官方文档了解更多信息。

Note: if you want to delete file only from git use below:

git rm --cached file1.txt

If you want to delete also from hard disk:

git rm file1.txt

If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:

git rm -r foldername

If you want to remove a folder inside another folder

git rm -r parentFolder/childFolder

Then, you can commit and push as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.

From doc:

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​

OPTIONS

<file>…​

Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you

may need to shell-escape them. A leading directory name (e.g. dir to
remove dir/file1 and dir/file2) can be given to remove all files in
the directory, and recursively all sub-directories, but this requires
the -r option to be explicitly given.
-f
--force

Override the up-to-date check.

-n
--dry-run

Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.

-r

Allow recursive removal when a leading directory name is given.

--

This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for

command-line options).
--cached

Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

--ignore-unmatch

Exit with a zero status even if no files matched.

-q
--quiet

git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.

Read more on official doc.

我一直都在从未离去 2024-08-24 17:18:45

https://stackoverflow.com/a/2047477/14508423
https://stackoverflow.com/a/19666677/14508423
很相似,两个都试过了。没有什么。 Git 不断跟踪该文件。 (我使用 vscode,因此文件立即被标记为 U(未跟踪)或 A(已添加))

这样做 https://stackoverflow.com /a/53431148/14508423 到整个类别解决了问题。

执行此操作可以从 git 中删除文件并使 git 忘记它:

**manually add the file name to .gitignore**

git rm --cached settings.py    
git commit -m "remove settings.py"
git push origin master
git rm -r --cached .
git add .
git commit -m "Untrack files in .gitignore"

ps:每次失败时,我都会尝试删除带有令牌(.env 和 settings.py 文件)等敏感信息的文件。但它可以很好地处理随机文件 oO

https://stackoverflow.com/a/2047477/14508423
https://stackoverflow.com/a/19666677/14508423
Quite similar, tried both. Nothing. Git keeps tracking the file. (I use vscode so the file got instantly marked as U(untracked) or A(added))

Doing this https://stackoverflow.com/a/53431148/14508423 to whole category solves the problem.

Do this to delete a file from git and make git forget about it:

**manually add the file name to .gitignore**

git rm --cached settings.py    
git commit -m "remove settings.py"
git push origin master
git rm -r --cached .
git add .
git commit -m "Untrack files in .gitignore"

ps: every time it failed I tried to delete file with sensitive info like tokens (.env and settings.py files). But it works fine with random files oO

淡淡の花香 2024-08-24 17:18:45

就我而言,我尝试在几次提交后删除 github 上的文件,但保存在计算机上

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master

,后来该文件被忽略

In my case I tried to remove file on github after few commits but save on computer

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master

and later this file was ignored

娇俏 2024-08-24 17:18:45

删除特定文件

git rm 文件名

单次递归地清除目录中所有未跟踪的文件

git clean -fdx

To delete a specific file

git rm filename

To clean all the untracked files from a directory recursively in single shot

git clean -fdx

演出会有结束 2024-08-24 17:18:45

如果您有适用于 Windows 的 GitHub 应用程序,则只需 5 个简单步骤即可删除文件:

  • 单击“同步”。
  • 单击文件所在的目录并选择该文件的最新版本。
  • 单击工具并选择“在此处打开 shell”。
  • 在 shell 中,输入:“rm {filename}”并按 Enter 键。
  • 提交更改并重新同步。

If you have the GitHub for Windows application, you can delete a file in 5 easy steps:

  • Click Sync.
  • Click on the directory where the file is located and select your latest version of the file.
  • Click on tools and select "Open a shell here."
  • In the shell, type: "rm {filename}" and hit enter.
  • Commit the change and resync.
〃温暖了心ぐ 2024-08-24 17:18:45
  1. 首先,从本地存储库中删除文件。

    <块引用>

    git rm -r 文件名

    或者,仅从本地存储库中删除文件,但从文件系统中删除文件

    <块引用>

    git rm --缓存文件名

  2. 其次,将更改提交到本地存储库。

    git commit -m“不需要的文件或一些内联注释”   
    
  3. 最后,将本地更改更新/推送到远程存储库。

    <前><代码>git推送

  1. First,Remove files from local repository.

    git rm -r File-Name

    or, remove files only from local repository but from filesystem

    git rm --cached File-Name

  2. Secondly, Commit changes into local repository.

    git commit -m "unwanted files or some inline comments"   
    
  3. Finally, update/push local changes into remote repository.

    git push 
    
澉约 2024-08-24 17:18:45

对于 git rm 不够并且需要从历史记录中删除文件的情况:正如 git filter-branch 手册页现在本身建议使用 git-filter-repo,我今天必须这样做,这是一个使用该工具的示例。它使用示例存储库 https://example/eguser/eg.git

  1. 将存储库克隆到新目录git clone https://example/eguser/eg.git< /代码>


  2. 保留除不需要的文件之外的所有内容。 git-filter-repo --path file1.txt --invert-paths

  3. 添加远程存储库源:
    git 远程添加源 https://example/eguser/eg.git
    git-filter-repo 工具按设计删除远程远程信息,并建议 一个新的远程存储库(参见第 4 点)。这对于大型共享存储库来说是有意义的,但对于删除单个新添加的文件(如此例中所示)可能有点过分了。

  4. 当对本地的内容感到满意时,用它替换远程。

    git push --force -u origin master。由于历史记录已更改,因此需要强制。

另请注意有用的 --dry-run 选项以及链接手册中关于团队和项目动态的良好讨论,然后再充电和更改存储库历史记录。

For the case where git rm doesn't suffice and the file needs to be removed from history: As the git filter-branch manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repo https://example/eguser/eg.git

  1. Clone the repository into a new directory git clone https://example/eguser/eg.git

  2. Keep everything except the unwanted file. git-filter-repo --path file1.txt --invert-paths

  3. Add the remote repository origin back :
    git remote add origin https://example/eguser/eg.git.
    The git-filter-repo tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.

  4. When happy with the contents of local, replace remote with it.

    git push --force -u origin master. Forcing is required due to the changed history.

Also note the useful --dry-run option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.

不一样的天空 2024-08-24 17:18:45

转到您的项目目录并输入:

git filter-branch --tree-filter 'rm -f <deleted-file>' HEAD

之后 push --force 从所有提交中删除文件。

git push origin --force --all

go to your project dir and type:

git filter-branch --tree-filter 'rm -f <deleted-file>' HEAD

after that push --force for delete file from all commits.

git push origin --force --all
相对绾红妆 2024-08-24 17:18:45

2022 年有效的新答案。

请勿使用:

git filter-branch

此命令在推送后可能不会更改远程存储库。如果您在使用后进行克隆,您会发现没有任何变化,并且存储库仍然很大。这个命令现在已经很旧了。例如,如果您使用 https://github.com/18F/C2/issues 中的步骤/439,这行不通。

您需要使用

git filter-repo

步骤:

(1)查找.git中最大的文件:

git rev-list --objects --all | grep -f <(git verify-pack -v  .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)

(2)Strat过滤这些大文件:

 git filter-repo --path-glob '../../src/../..' --invert-paths --force

 git filter-repo --path-glob '*.zip' --invert-paths --force

 git filter-repo --path-glob '*.a' --invert-paths --force


无论您在步骤 1 中找到什么。

(3)

 git remote add origin [email protected]:.../...git

(4)

git push --all --force

git push --tags --force

完成!

New answer that works in 2022.

Do not use:

git filter-branch

this command might not change the remote repo after pushing. If you clone after using it, you will see that nothing has changed and the repo still has a large size. This command is old now. For example, if you use the steps in https://github.com/18F/C2/issues/439, this won't work.

You need to use

git filter-repo

Steps:

(1) Find the largest files in .git:

git rev-list --objects --all | grep -f <(git verify-pack -v  .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)

(2) Strat filtering these large files:

 git filter-repo --path-glob '../../src/../..' --invert-paths --force

or

 git filter-repo --path-glob '*.zip' --invert-paths --force

or

 git filter-repo --path-glob '*.a' --invert-paths --force

or
whatever you find in step 1.

(3)

 git remote add origin [email protected]:.../...git

(4)

git push --all --force

git push --tags --force

DONE!

无声情话 2024-08-24 17:18:45

我尝试了很多建议的选项,但似乎都不起作用(我不会列出各种问题)。
我最终所做的,有效的,简单直观(对我来说)是:

  1. 将整个本地存储库移到其他地方,
  2. 再次将存储库从主控克隆到本地驱动器,
  3. 将文件/文件夹从#1中的原始副本复制回来自 #2 的新克隆
  4. 确保问题大文件不存在或排除在 .gitignore 文件中
  5. 执行通常的 git add/git commit/git push

I tried a lot of the suggested options and none appeared to work (I won't list the various problems).
What I ended up doing, which worked, was simple and intuitive (to me) was:

  1. move the whole local repo elsewhere
  2. clone the repo again from master to your local drive
  3. copy back the files/folder from your original copy in #1 back into the new clone from #2
  4. make sure that the problem large file is either not there or excluded in the .gitignore file
  5. do the usual git add/git commit/git push
放肆 2024-08-24 17:18:45

使用 git rm 从存储库中删除文件后,您可以使用 BFG Repo-Cleaner 可以轻松地从存储库历史记录中彻底删除该文件。

After you have removed the file from the repo with git rm you can use BFG Repo-Cleaner to completely and easily obliterate the file from the repo history.

夜雨飘雪 2024-08-24 17:18:45

只需转到 github 存储库中的文件,您就可以看到 Raw|Blame 旁边的删除图标,并且不要忘记单击提交更改按钮。您可以看到您的文件已被删除。

Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.

等风来 2024-08-24 17:18:45

我有 obj 和 bin 文件意外地进入了存储库,我不想污染我的“已更改文件”列表

之后我注意到它们去了远程,我通过将其添加到 < code>.gitignore

/*/obj
/*/bin

问题是它们已经在远程中,当它们发生更改时,它们会弹出为已更改并污染已更改的文件列表。

要停止查看它们,您需要从远程存储库中删除整个文件夹。

在命令提示符下:

  1. CD 到 repo 文件夹(即 C:\repos\MyRepo
  2. 我想删除 SSIS\obj。看来你只能在顶层删除,所以你现在需要CD进入SSIS:(即C:\repos\MyRepo\SSIS
  3. 现在输入魔法咒语git rm -r -f 对象
    • rm=删除
    • -r = 递归删除
    • -f = 表示力量,因为你是认真的
    • obj 是文件夹
  4. 现在运行 git commit -m "remove objfolder"

我收到一条令人震惊的消息说13个文件更改了315222个删除

然后因为我不想查找CMD行,所以我进入Visual Sstudio并进行同步以将其应用到远程

I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list

After I noticed they went to the remote, I ignored them by adding this to .gitignore

/*/obj
/*/bin

Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list.

To stop seeing them, you need to delete the whole folder from the remote repository.

In a command prompt:

  1. CD to the repo folder (i.e. C:\repos\MyRepo)
  2. I want to delete SSIS\obj. It seems you can only delete at the top level, so you now need to CD into SSIS: (i.e. C:\repos\MyRepo\SSIS)
  3. Now type the magic incantation git rm -r -f obj
    • rm=remove
    • -r = recursively remove
    • -f = means force, cause you really mean it
    • obj is the folder
  4. Now run git commit -m "remove obj folder"

I got an alarming message saying 13 files changed 315222 deletions

Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote

高冷爸爸 2024-08-24 17:18:45

如果您需要从确定的扩展名中删除文件(例如,已编译的文件),您可以执行以下操作来一次性删除它们:

git remove -f *.pyc

If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:

git remove -f *.pyc
天生の放荡 2024-08-24 17:18:45

如果您不在本地存储库中归档,而是在 git 存储库中归档,则只需通过 Web 界面打开 git 存储库中的文件,然后在界面右上角找到“删除”按钮即可。
单击此处,查看界面删除选项

Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface.
Click Here, To view interface Delete Option

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