Git 忽略 &改变过去
我最近才添加了一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将是一个两步过程:
引入 .gitignore 文件
为此,早期提交必须拥有
.gitignore
文件。因此,您需要:git checkout -b temp $(git rev-list HEAD | tail -1)
.gitignore
文件。git commit --amend
重写历史记录
现在,您可以通过以下命令自动从所有分支中删除这些文件:
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:git checkout -b temp $(git rev-list HEAD | tail -1)
.gitignore
file.git commit --amend
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.
我刚刚完成了此操作以及其他一些答案/评论几乎让我到达那里,但我不得不做一些变化。
我使用
filter-branch
在历史记录开始时引入我的.gitignore
文件,并将其应用于所有以前的提交。这是命令:
如您所见,它假设您的桌面上有所需的
.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:
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 wouldgit 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, astree-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.请参阅从每次提交中删除文件部分。这正是你的情况。
但要小心,因为此功能与每一次历史重写一样,都会改变每次提交。因此,如果您有公共存储库并且有人可以基于它进行一些工作 - 这会导致问题。
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.
使用
filter-branch
查看如下内容http://progit.org /book/ch6-4.html
Look at something like below using
filter-branch
http://progit.org/book/ch6-4.html
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.