3

I just found out that, in a past commit, I included files that should not be in the repository (.idea Webstorm files and others). This commit "sits in the middle" with unpushed commits before it and after it. Also, within this commit are files that do need to be committed and that were furher modified in later (unpushed) commits.

I have seen these answers: Completely remove file from all Git repository commit history and Remove files from Git commit but they don't seem to answer my specific issue. What is the solution?

Community
  • 1
  • 1
yar1
  • 1,331
  • 2
  • 15
  • 26

2 Answers2

4

Your situation

I'm creating the situation you described with 3 un-pushed commits:

$ git clone <url>
$ touch file1
$ git add file1
$ git commit -m "Correct 1st commit"
$ touch file2
$ touch webstormfile
$ git add .
$ git commit -m "Wrong 2nd commit with by mistake included WebStorm file"
$ touch file3
$ git add file3
$ git commit -m "Correct 3rd commit"
$ git log --oneline
$
$ ad73bfa Correct 3rd commit
$ eddae38 Wrong 2nd commit with by mistake included WebStorm file
$ ad219bf Correct 1st commit

Removing committed WebStorm files

Beware: the following steps are going to change the history, meaning, if the commits have already been pushed, changing the history will break the repository for all team members. If you haven't pushed yet, you can follow the steps without bothering your team members.

Start interactive rebasing including the hash eddae38 of the wrong commit:

$ git rebase -i eddae38^

The text editor pops up with the following content:

pick eddae38 Wrong 2nd commit with by mistake included WebStorm file
pick ad73bfa Correct 3rd commit

# Rebase ad219bf..ad73bfa onto ad219bf
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

In the first line, change pick to edit, save the file and close the text editor.

Attention! Make a copy of your WebStorm file if want to keep it. Then, remove the WebStorm file from the commit:

$ git rm .\webstormfile

Commit the cleaned up index:

$ git commit --amend --no-edit

Finish the rebasing:

$ git rebase --continue
Lernkurve
  • 20,203
  • 28
  • 86
  • 118
0

Delete the files with another commit, then run git rebase -i on the last pushed commit and squash the wrong commit with the one that deletes the files

Make sure you backup your files, as you'll need them. After the commit fix, restore the files and add them to .gitignore to prevent further commits

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • 1
    don't do this lightly; it will **rewrite history** and anyone else using your repository will be very confused and annoyed. – Eevee Oct 28 '13 at 07:40
  • @Eevee, you are right, but the OP specified that the faulty commit and the surrounding commits are _unpushed_ ... I think there is no problem here. – Rerito Oct 28 '13 at 07:52