Removing a specific file with its history from the Git history

僤鯓⒐⒋嵵緔 提交于 2019-12-11 06:15:31

问题


I want to delete the revision history for a specific file in my Git repository that was made long ago. The goal is to make it look like the file was never committed for a specific revision. Does anyone know how to do this? Thanks!


回答1:


Sounds like you want git filter-branch.

However, please be sure to consider the warning on the man page:

WARNING! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for further information about rewriting published history.)

The "Examples" section of the man page shows how to remove history for a given file. This should be a good starting point for you.

Suppose you want to remove a file (containing confidential information or copyright violation) from all commits:

git filter-branch --tree-filter 'rm filename' -- --all




回答2:


If you truly do want to remove the commit from history, filter-branch is the way to go, as Tim Henigan suggests. However, the warning he quotes from the manpage comes with good reason, and very likely you could find another solution.

To reverse the effects of a single commit, you can simply use:

git revert <commit>        # that's the SHA1 hash of the commit

That will create a new commit, on top of your current branch, which reverses all changes made in the specified commit.



来源:https://stackoverflow.com/questions/3685857/removing-a-specific-file-with-its-history-from-the-git-history

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!