I had a conflict in a merge. Git creates filename.orig and filename.remote when a conflict happens. I resolved the conflict, but somehow filename.orig got added to the repository several commits ago without me noticing it until now. Is it possible to rewrite the change history such that filename.orig was never added to the repository in the first place?
I think you should have a look at what you can do with git rebase. I haven't done this before, but it seems that may be where you need to head.
Have a read of http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#rewriting-one-commit
Although beware the implications around this if there are branches created AFTER that point.
Please don't use this recipe if your situation is not the one described in the question. This recipe is for fixing a bad merge, and replaying your good commits onto a fixed merge.
Although filter-branch
will do what you want, it is quite a complex command and I would probably choose to do this with git rebase
. It's probably a personal preference. filter-branch
can do it in a single, slightly more complex command, whereas the rebase
solution is performing the equivalent logical operations one step at a time.
Try the following recipe:
# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix <sha1-of-merge>
# remove the incorrectly added file
git rm somefile.orig
# commit the amended merge
git commit --amend
# go back to the master branch
git checkout master
# replant the master branch onto the corrected merge
git rebase tmpfix
# delete the temporary branch
git branch -d tmpfix
(Note that you don't actually need a temporary branch, you can do this with a 'detached HEAD', but you need to take a note of the commit id generated by the git commit --amend
step to supply to the git rebase
command rather than using the temporary branch name.)
If you haven't committed anything since, just git rm
the file and git commit --amend
.
If you have, git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename.orig' merge-point..HEAD
will go through each change from merge-point
to HEAD
, delete filename.orig and rewrite the change. Using --ignore-unmatch
means the command won't fail if for some reason filename.orig is missing from a change. That's the recommended way from the Examples section in the git-filter-branch man page.
This is the best way: http://github.com/guides/completely-remove-a-file-from-all-revisions just be sure to backup the copies of the files first.