开发者

Removing a specific file with its history from the Git history

I want to delete the revision history for a specific开发者_C百科 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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜