How do you fix a bad merge, and replay your good commits onto a fixed merge?

git rebase
git filter-branch
git amend
git rebase interactive remove file from commit
git reset to commit
git remove file from commit
git undo commit
git remove previously committed file

I accidentally committed an unwanted file (filename.orig while resolving a merge) to my repository several commits ago, without me noticing it until now. I want to completely delete the file from the repository history.

Is it possible to rewrite the change history such that filename.orig was never added to the repository in the first place?

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.)

How To Undo Last Git Commit – devconnected, How could you squash multiple commits together without using git merge? It sounds like you need to be asking how to avoid getting into a bad merge, not how to fix a bad merge. It definitely sounds like you've fudged up at some point, and are now having to rectify this fudge. Probably why none of the tutorials you're looking at cover it. When you say you're a team of 1, what do you mean?

Squash my last X commits together using Git, How do you fix a bad merge, and replay your good commits onto a fixed merge? git filter-branch git rebase bfg repo-cleaner git amend git filter-branch remove  M is the merge that brings these premature changes into master, x are changes unrelated to what the side branch did (so maybe some bug fixes, other merges, or whatever). So we need to get rid of M. On your local development environment, you need to first pull in all changes from origin (Bitbucket, or wherever your remote repo is), make sure you

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 path/to/file/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.

Note for Windows users: The file path must use forward slashes

Resolving merge conflicts after a Git rebase, When you perform a git rebase operation, you're typically moving commits around. Because of this, you might get into a situation where a merge conflict is access to your user account's project boards · Best practices for leaving your company error: could not apply fa39187 something to add to patch A When you have  Finally, whenever you want to merge your branch with master, make sure you do the first two steps right before, and then finally from master you can run git merge my-branch. Since your branch is

This is the best way:

Just be sure to backup the copies of the files first.


The edit by Neon got unfortunately rejected during review. See Neons post below, it might contain useful information!

E.g. to remove all *.gz files accidentally committed into git repository:

$ du -sh .git ==> e.g. 100M
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch *.gz' HEAD
$ git push origin master --force
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now

That still didn't work for me? (I am currently at git version

$ du -sh .git ==> e.g. 100M

Not sure why, since I only had ONE master branch. Anyways, I finally got my git repo truely cleaned up by pushing into a new empty and bare git repository, e.g.

$ git init --bare /path/to/newcleanrepo.git
$ git push /path/to/newcleanrepo.git master
$ du -sh /path/to/newcleanrepo.git ==> e.g. 5M 


Then I clone that to a new directory and moved over it's .git folder into this one. e.g.

$ mv .git ../large_dot_git
$ git clone /path/to/newcleanrepo.git ../tmpdir
$ mv ../tmpdir/.git .
$ du -sh .git ==> e.g. 5M 

(yeah! finally cleaned up!)

After verifying that all is well, then you can delete the ../large_dot_git and ../tmpdir directories (maybe in a couple weeks or month from now, just in case...)

Fix conflicts only once with git rerere, Picture this: you're working on a long-lived branch; perhaps a heavy feature branch. So to relieve some of that tension and ease up the final merge you're heading I can't quite fathom a situation where having rerere enabled is a bad idea, Automatic merge failed; fix conflicts and then commit the result. How to fix Git rebase merge conflicts Introduction. Dealing with merge conflicts that arise from a Git rebase can certainly be intimidating, especially for beginners. The merge conflict alone can be tricky enough to deal with, and handling this from a rebase attempt differs slightly from a normal Git merge.

Rewriting Git history demands changing all the affected commit ids, and so everyone who's working on the project will need to delete their old copies of the repo, and do a fresh clone after you've cleaned the history. The more people it inconveniences, the more you need a good reason to do it - your superfluous file isn't really causing a problem, but if only you are working on the project, you might as well clean up the Git history if you want to!

To make it as easy as possible, I'd recommend using the BFG Repo-Cleaner, a simpler, faster alternative to git-filter-branch specifically designed for removing files from Git history. One way in which it makes your life easier here is that it actually handles all refs by default (all tags, branches, etc) but it's also 10 - 50x faster.

You should carefully follow the steps here: - but the core bit is just this: download the BFG jar (requires Java 6 or above) and run this command:

$ java -jar bfg.jar --delete-files filename.orig my-repo.git

Your entire repository history will be scanned, and any file named filename.orig (that's not in your latest commit) will be removed. This is considerably easier than using git-filter-branch to do the same thing!

Full disclosure: I'm the author of the BFG Repo-Cleaner.

git-rebase Documentation, Another option is to bypass the commit that caused the merge failure with git not be the best idea to keep rebasing on top of the upstream but to keep the base a rebase merge works by replaying each commit from the working branch on top perfect commit it fixes, because that commit is buried deeply in a patch series. Imagine your team is working on various feature branches at the same time. Now you want to merge all of them together into one testable pre-release branch. As expected, there are several merge conflicts, which you resolve. Unfortunately it turns out that one of the branches isn’t quite there yet, so you decide to un-merge it again.

How (and why!) to keep your Git commit history clean, Git commit history is very easy to mess up, here's how you can fix it! bugs being fixed, architecture being refactored), commit messages are the place where by your peer before the changes are merged into repo's master branch. At this point, Git will replay all the commits that followed after the commit  You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you can merge. When I try the git status, it gave me the following: On branch master # Your branch and 'origin/master' have diverged, # and have 1 and 13 different commit(s) each, respectively.

Problems with Pull Requests and How to Fix Them, Furthermore I regard the terms pull request and merge request to be used by your version control tool (e.g. git pull or git merge . As implemented on GitHub, a pull request is tracked by a Git branch. (I'd love to be proven wrong.) Git, and Mercurial - practice the series of individually-good commits  But the merge is the problem, so you want a merge commit as a bisect result. It's easy enough to find the individual commits with a blame or a different bisect if you want to dig deeper. With a linear history, the merge is done off book, so you can no longer tell a bad merge was the problem.

Too much fun with “git pull –rebase” – Doing Git Wrong, Thanks to the git patch-id command it's easy to imagine how this mechanism might work. I tried squashes, merge-squashes, dropped commits, merge-base ones on origin/feature, and thus it only replays the final 'gf' commit. If your branch has many commits, and you find yourself in rebase hell,  Currently slnx.sqlite is part of the tracked files. It was probably added by mistake before .gitignore was added. It updates automatically by VS and thus prevent some Git commands from executing since they require all tracked files to be

  • Related: How to remove/delete a large file from commit history in Git repository?.
  • related…
  • Wouldn't a git rebase -i be faster and still as easy? $ git rebase -i <sh1-of-merge> Mark the correct one as "edit" $ git rm somefile.orig $ git commit --amend $ git rebase --continue However for some reason I still have that file somewhere the last time I did that. Probably missing something.
  • git rebase -i is very useful, especially when you have multiple rebase-y operations to perform, but it's a right pain to describe accurately when you're not actually pointing over someone's shoulder and can see what they're doing with their editor. I use vim, but not everyone would be happy with: "ggjcesquash<Esc>jddjp:wq" and instructions like "Move the top line to after the current second line and change the first word on line four to 'edit' now save and quit" quickly seem more complex than the actual steps are. You normally end up with some --amend and --continue actions, as well.
  • I did this but a new commit was reapplied on top of the amended one, with the same message. Apparently git did a 3 way merge between the old, unamended commit containing the unwanted file, and the fixed commit from the other branch, and so it created a new commit on top of the old one, to re-apply the file.
  • @UncleCJ: Was your file added in a merge commit? This is important. This recipe is designed to cope with a bad merge commit. It's not going to work if your unwanted file was added in a normal commit in history.
  • I'm amazed how I could do all this using smartgit and no terminal at all! Thanks for the recipe!
  • Does filter-branch cause recalculating of hashes? If a team works with a repo where a big file should be filtered, how do they do this so that everybody ends up with the same state of the repo?
  • @YakovL. Everything recalculates hashes. Actually commits are immutable. It creates an entirely new history, and moves your branch pointer to it. The only way to ensure everyone has the same history is a hard reset.
  • Thanks! git filter-branch worked for me where the rebase example given as an answer didn't: The steps seemed to work, but then pushing failed. Did a pull, then pushed successfully, but the file was still around. Tried to redo the rebase steps and then it went all messy with merge conflicts. I used a slightly different filter-branch command though, the "An Improved Method" one given here: git filter-branch -f --index-filter 'git update-index --remove filename' <introduction-revision-sha1>..HEAD
  • I'm not sure which one is the improved method. Git official documentation of git-filter-branch seem to give the first one.
  • Check out… I find it the most complete and straight forward solution that involves filter-branch