0

I have a repository of several kB (text). During my work, I had several temporary large files in the same folder, which I forgot to delete.

My last five commits are "aware" of those temporary files. I have already deleted the files manually, and I want GIT to "forget" the last five commits and to create a new one (i.e. I don't want to have the .git folder 1000x bigger because of remembering that useles file).

And most importantly, I don't want GIT to rewrite any of my files (I am afraid that GIT will restore all my files to a state five commits ago).

I am not very good at the GIT terminology. Do you know how can I do it?

P. S. I am not trying to prune specific files from the index. I want to make Git "forget" commits, as if I did not do them at all. There is no server and I do not share my work with anyone.

Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46
  • 1
    If you want to retain the work in the last five commits, you need to rewrite your commits to remove only the big file and retain the rest. See https://stackoverflow.com/questions/2100907/how-to-remove-delete-a-large-file-from-commit-history-in-git-repository – Schwern Oct 30 '20 at 18:18

1 Answers1

0

(i.e. I don't want to have the .git folder 1000x bigger because of remembering that useles file).

Forgetting a "big" file has been very well dealt with on Stack Overflow and there is little point in my repeating the instructions.

Now, having gotten rid of the "big" file, do you still want to remove the five "bad" commits as well? If so, read on:

And most importantly, I don't want GIT to rewrite any of my files

So just copy all your files off to some other location so they are safe.

Now use git log to find out the SHA of the last good commit, and then git reset --hard back to that SHA.

Now copy all your files back again! Add-and-commit.

Oh, and fix your .gitignore file so that you do not accidentally add-and-commit one of these temporary "big" files again. Git puts a lot of checks in your way to prevent your committing something you don't want to, and you managed to barge your way past all of them. Try to pay more attention in future to what you are doing. The best way to fix a mistake is not to make it in the first place.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Your solution makes sense. But is it possible to tell git to do "git reset --hard XYZ" without actually modifying my files? So that I don't have to copy them and then put them back? Also, are you sure that resetting to XYZ will make Git forget commits that were made after XYZ? – Ivan Kuckir Oct 30 '20 at 21:24
  • It depends what you mean by "forget". :) I am certain that a commit to which no name is pointing will eventually be deleted (sooner if you deliberately garbage-collect). So if you are on `master` and you reset back to XYZ, then `master` no longer points at any of the commits after XYZ, and if they are not on any _other_ branch as well, and they have no tags, then yes, they will die (eventually). – matt Oct 30 '20 at 21:30
  • "But is it possible to tell git to do "git reset --hard XYZ" without actually modifying my files?" No. A hard reset to XYZ checks out XYZ, thus causing your working tree to reflect XYZ. You could do a `mixed` reset instead; that will leave the working tree untouched. But I think what I'm describing is actually a safer alternative. – matt Oct 30 '20 at 21:33