0

How does one delete a local git commit without affecting code.

I did a commit but it will not push because a file exceeds GitHubs 100MB limit. When prevents the commit from being pushed, and all subsequent commits.

I have removed the offending file, but the commit still has it as part of the commit.

I have since done other commits (which I can lose). But I can't lose the code as it stands now.

Thanks all

TheBearF8
  • 375
  • 1
  • 3
  • 14
  • You can't cancel commit like revert it ? – Elikill58 Jul 12 '21 at 12:36
  • @Elikill58 Revert does not remove files, so if a file in a commit is too big for github, revert is not the answer. – Lasse V. Karlsen Jul 12 '21 at 12:39
  • Is it the very last commit on your current branch? If it is, can't you delete the file on disk, do `git add .`, then `git commit --amend` to update the last commit? – Lasse V. Karlsen Jul 12 '21 at 12:39
  • yes but if you go to old commit, then do another one which replace the too big commit, maybe it can works – Elikill58 Jul 12 '21 at 12:41
  • I have manually already removed the file. Never heard of revert before. Also Lasse, sadly no it's not the last commit, but I don't mind losing all commits as long as it doesn't change/affect a code file – TheBearF8 Jul 12 '21 at 12:48
  • https://stackoverflow.com/q/2100907/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+remove+large+file+history – phd Jul 12 '21 at 13:43

1 Answers1

0

I have removed the offending file, but the commit still has it as part of the commit.

This is a job for git rebase. Specifically, I think you want the -i interactive option, which allows you to remove one or more commits in the history, reorder commits, edit individual commits, etc. For example, you could:

git log

to see the commits;

git rebase -i <hash of the commit prior to adding the big file>

to start the rebase process. This will open an editor and show you a list of commits back to the one you specified, along with commands for each. It's a powerful tool and you should read more extensive instructions than I'm giving here, but for example you could edit the commit where you added the big file by changing pick for that commit to edit. git rebase will then replay the commits, stopping on the commit you want to edit. You can delete the file and 'git add' it, and then git rebase continue to replay the rest of the commits. After that, the file should have been removed from the history.

The standard warning applies: rebase is a powerful command that can really screw up your git history, so be very careful with it. When in doubt, make a copy of your branch beforehand so that you can get back to your current state.

Caleb
  • 124,013
  • 19
  • 183
  • 272