且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Git历史记录中删除文件而不删除文件

更新时间:2023-12-04 20:31:40

完成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.