1

I did a bad commit with a directory full of files (/vendor/bundle/*). After that I tried to fix it with changing .gitignore, so I did another 2 commits.

How can I remove /vendor/bundle/* directory from all the previous commits (without affecting any working files or other committed files)?

all jazz
  • 2,007
  • 2
  • 21
  • 37

1 Answers1

3

You can use an interactive rebase to remove the directory from your past commits:

# Go back 3 commits
git rebase -i HEAD~3

You'll see your editor open with with list of commits:

pick <HEAD~2> Commit /vendor/bundle/*
pick <HEAD~1> OMG ignore /vendor/bundle/*!
pick <HEAD>   OMG ignore /vendor/bundle/* again!

Select the commit where you committed the directory and replace pick with edit:

edit <HEAD~2> Commit /vendor/bundle/*
pick <HEAD~1> OMG ignore /vendor/bundle/*!
pick <HEAD>   OMG ignore /vendor/bundle/* again!

The rebase will rewind to that commit and stop. Then do the following:

git rm -r -- /vendor/bundle
git commit --amend --no-edit
git rebase --continue

You can read more about rewriting git history from the FREE online Pro Git book.