从Git历史记录中删除文件而不删除文件 [英] Remove a file from Git history without deleting the file

查看:176
本文介绍了从Git历史记录中删除文件而不删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GitHub的指令从提交历史中清除文件,但是它从我的系统中删除了相关文件.这使我感到惊讶,因为git rm --cached命令本身应该使文件本身保持不变.但是,该行为也可以在该页面上的工作示例中看到:尝试在大型git filter-branch ...命令之前和之后运行ls.

I used GitHub's instructions for scrubbing a file from the commit history, but it deleted the file in question from my system. This was surprising to me because the git rm --cached command itself should leave the file itself untouched. But this behavior can also be seen in the worked example on that page: try running ls before and after the big git filter-branch ... command.

我正在使用Git 2.6.1.

I'm using Git 2.6.1.

如何从提交历史记录中删除文件,而无需删除文件本身?显然,我可以做一个备份(就我而言),但这是一种解决方法,而不是解决方案.

How can I delete a file from the commit history without deleting the file itself? Obviously I could just make a backup (which I did in my case), but that's a workaround and not a solution.

推荐答案

完成git-filter-branch后,它将签出新的分支头.这会将您的工作目录更新为干净状态.您希望从历史记录中删除的文件已被删除.解决方案是事先备份.

When git-filter-branch is done it checks out the new branch head. This will update your working directory to a clean state. The file you wanted obliterated from history has been obliterated. Backing up beforehand is the solution.

如果忘记备份​​,仍然可以找回! Git需要很长时间才能将其扔掉,您的原始提交仍在那儿.在git-filter-branch之后,将有一个名为original/refs/heads/master的分支(如果您过滤了母版),其中包含原始提交.您可以从那里恢复文件.

If you forgot to back it up, you can still get it back! Git takes a long time to throw things out, your original commits are still in there. After git-filter-branch there will be a branch called original/refs/heads/master (if you filtered master) which contains the original commits. You can recover the file from there.

通常,您可以使用git reflog恢复过滤器并重新设置基准.这是每次HEAD更改时的日志(即,您结帐或变基,合并或过滤或...).例如,在执行Github过滤器示例后,git reflog是...

In general, you can recover filters and rebases using git reflog. It's a log of every time HEAD changes (ie. you checkout or rebase or merge or filter or...). For example, after doing the Github filter example, git reflog is...

abaabaf (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: filter-branch: rewrite
8ef0c30 (refs/original/refs/remotes/origin/master, refs/original/refs/heads/master) HEAD@{1}: clone:

我可以使用8ef0c30HEAD@{1}(即HEAD的先前位置)或original/refs/remotes/origin/masteroriginal/refs/heads/master返回过滤器运行之前的状态.

I can use 8ef0c30 or HEAD@{1} (ie. the previous location of HEAD) or original/refs/remotes/origin/master or original/refs/heads/master to get back to where things were before the filter ran.

这篇关于从Git历史记录中删除文件而不删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆