tags:

views:

39

answers:

2

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  A: 

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

Tim Henigan
Thanks, that looks like exactly what I need. Forgive the ignorance, but is HEAD in that example the revision number I want to delete?
Alexander
@Alexander: HEAD specifies which ref's history to rewrite. That example tries to delete the "bad" file from all ancestors of HEAD (that is described just after the example itself).
wRAR
That example won't actually rewrite any branches. If you really want all branches rewritten, you can replace `HEAD` with `-- --all`.
Jefromi
@Jefromi: Thanks for the correction. I updated the answer to match.
Tim Henigan
Note also the caveat from the documentation, that if that file isn't present in some of the commits, `rm filename` will fail; thus, you likely want `rm -f filename`.
Brian Campbell
A: 

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.

Jefromi