Git 忽略 &改变过去

发布于 2024-11-25 13:35:39 字数 106 浏览 0 评论 0原文

我最近才添加了一个 .gitignore 文件来忽略我的子目录中的一些特定数据文件。我真的很想回到过去并将它们从以前的提交/历史记录中删除。

这很容易实现吗?我应该从哪里/如何开始?

I only recently added a .gitignore file to ignore some specific data files in a subdirectory of mine. I'd really like to go back in time and remove them from previous commits/history as well.

Is this easily accomplished? Where/how should I start?

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

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

发布评论

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

评论(5

叫思念不要吵 2024-12-02 13:35:39

这将是一个两步过程:

引入 .gitignore 文件

为此,早期提交必须拥有 .gitignore 文件。因此,您需要:

  1. 检查根提交:git checkout -b temp $(git rev-list HEAD | tail -1)
  2. 添加.gitignore 文件。
  3. git commit --amend
  4. 将所有现有分支重新设置为“temp”。这可能会或可能不会无缝工作,因此如果出现任何冲突,您可能需要摆弄它。
  5. 检查您的工作分支并删除临时分支。

重写历史记录

现在,您可以通过以下命令自动从所有分支中删除这些文件:

git filter-branch --tree-filter 'git clean -f -X' -- --all

这将重写所有分支以删除忽略的文件。这可能需要一段时间,具体取决于存储库的大小。

This will be a two-step procedure:

Introduce the .gitignore file

For this to work, the early commits are going to have to have your .gitignore file. So, you need to:

  1. Check out the root commit: git checkout -b temp $(git rev-list HEAD | tail -1)
  2. Add the .gitignore file.
  3. git commit --amend
  4. Rebase all of your existing branches onto "temp". This may or may not work seamlessly, so you might need to fiddle with it if any conflicts spring up.
  5. Check out your working branch and delete the temp branch.

Rewrite history

Now you can automatically remove these files from all of the branches by this command:

git filter-branch --tree-filter 'git clean -f -X' -- --all

This will rewrite all of your branches to remove ignored files. It may take a while depending on the size of your repository.

酒几许 2024-12-02 13:35:39

我刚刚完成了此操作以及其他一些答案/评论几乎让我到达那里,但我不得不做一些变化。

我使用 filter-branch 在历史记录开始时引入我的 .gitignore 文件,并将其应用于所有以前的提交。

这是命令:

git filter-branch --tree-filter 'git rm --cached -rf . >/dev/null ; cp ~/Desktop/new-gitignore .gitignore ; git add . ; git clean -d -X -f >/dev/null' -- --all

如您所见,它假设您的桌面上有所需的 .gitignore 文件的副本 (~/Desktop/new-gitignore)。

我发现只有在删除并重新添加(git rm ...git add .)之后,所有文件才会git clean 删除新的 .gitignore 告诉它忽略的所有文件。请注意,清理后无需再次添加文件,因为 tree-filter 只会按原样提交所有内容。

我添加了 >/dev/null 来减少脚本的输出,但如果您想查看整个过程,可以安全地删除它。

I've just done this and some of the other answers/comments nearly got me there, but I had to make some changes.

I've used filter-branch to introduce my .gitignore file at the start of history, and apply it to all previous commits.

This is the command:

git filter-branch --tree-filter 'git rm --cached -rf . >/dev/null ; cp ~/Desktop/new-gitignore .gitignore ; git add . ; git clean -d -X -f >/dev/null' -- --all

As you can see, it assumes you have a copy of the wanted .gitignore file on your Desktop (~/Desktop/new-gitignore).

I found that only after I've removed and re-added (git rm ..., git add .) all the files would git clean remove all the files that the new .gitignore is telling it to ignore. Notice its not necessary to add the files again after clean, as tree-filter will just commit everything as is.

I've included >/dev/null to reduce the output of the script, but you can safely remove this if you want to see the whole process.

帅哥哥的热头脑 2024-12-02 13:35:39

请参阅从每次提交中删除文件部分。这正是你的情况。

但要小心,因为此功能与每一次历史重写一样,都会改变每次提交。因此,如果您有公共存储库并且有人可以基于它进行一些工作 - 这会导致问题。

See removing file from every commit section. It is exactly your case.

But be careful, as this feature as every history rewriting one DOES change every commit. So if you have public repository and someone could have based some work on it - it would lead to problems.

野稚 2024-12-02 13:35:39

使用 filter-branch 查看如下内容

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

http://progit.org /book/ch6-4.html

Look at something like below using filter-branch

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

http://progit.org/book/ch6-4.html

一抹苦笑 2024-12-02 13:35:39

git filter-branch 是一个可能的解决方案。在使用它之前,请确保您了解其含义,因为这些更改是永久性的。您将无法轻松地将过滤后的分支与旧分支合并。

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

将文件名交换为您想要消失的特定文件。

git filter-branch is a possible solution. Make sure you understand the implications before you utilize it, as the changes are permanent. You won't be easily able to merge your filtered branch with the old one.

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

Swap filename for the specific file you'd like gone.

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