2

So I recently did the silly thing of doing

git add -A

in my working directory which added some large data files (> 1 GB) that I didn't want to add. Stupidly enough, I committed, and when trying to push to Github, Github complained that I didn't have enough space and hence could not push (completely reasonable). Given I was in the middle of some code, I pushed on with my project, making a few more commits on top of the original commit that had the large data files. So now, I'm stuck with 4 local commits that can not be pushed, because I have those large data files in the first commit. Is there any clean way to resolve this without changing anything in my current working directory (I've added a lot of changes to code since the first commit with the large files).

Thanks!

user941401
  • 323
  • 1
  • 3
  • 8
  • Git branches are *entirely disposable* until you push/share. Don't worry about creating a new local commit (it is actually the safest method!). Just move off 'down stream' of the large commit - one method is to rebase. – user2864740 Apr 18 '15 at 02:14
  • For rebasing, see http://git-scm.com/docs/git-rebase since you've commit the current working directory first, with all the changes, you can get back to it. – user2864740 Apr 18 '15 at 03:04

2 Answers2

3

First stash/commit whatever code you have left, then rebase back to the commit you want to edit, (an easy way i use is getting the hash from git log and adding a ~ to indicate i want the parent)

git rebase -i a1b2c3~ #  replace with your hash and don't forget the ~

You'll get an interactive editor, change the bad commit from pick to edit or just e, then save and exit

The rebase will stop at the bad commit, now you want to remove the big file but at the same time you don't want to delete it, so you can add a --cached to the git rm

git rm --cached /path/to/huge/file

This will remove it from the index but not from the local system, then we want to save this change to the commit

git commit --amend # or just --am

now the commit no longer contains the huge file, so we tell git to continue the rebase

git rebase --continue

This will add the remaining commits and now you'll find that you have the big file as untracked.

If you stashed then now would be the time to unstash.

Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • Is there a simple modification to the answer if I do not want to remove the **addition** of a file from a commit but the **modification**? I.e., a commit changed several files and I want to keep the changes to some of the files in the commit, but the other changes I want to keep in the working directory so I can commit them later separately? – Viktor Dick Feb 05 '19 at 05:26
  • You can try using `git reset HEAD -p`, which will generate an inverse diff or all your changes, use `yes` and `no` to decide which changes you want to keep and which you want to remove – Mohammad AbuShady Feb 05 '19 at 17:22
  • I ended up setting the commits in question on `edit` in the rebase, then, when the rebase stopped after these commits, took them back and commited the changes in two separate commits instead (those I wanted to keep and those that I wanted to drop). Then I continued the rebase and at the end I did another rebase, this time only resorting the commits - those that I wanted to drop I put at the end. Finally, using `git reset`, I removed those commits without modifying the working tree. – Viktor Dick Feb 09 '19 at 06:31
  • @ViktorDick Sorry I misunderstood you, I thought you just added the changes but didn't commit. What I usually do with this case is you do a rebase, then do an `edit` just like you did, but instead of resetting the whole commit you do a git `reset HEAD~ -p` and you'll choose `yes` only for the changes you want to remove, then you do a `git commit --amend` and your commit will be fixed, and the changes you removed will be in the unstaged changes, you either `checkout` them or `add` them, depends on what you want, then `git rebase --continue` – Mohammad AbuShady Feb 12 '19 at 09:28
1

Having had exactly the same question (to the letter) and it taking a whole day to find the answer. Found it here...

How do I delete unpushed git commits?

Instead of a hard reset where you lose working dir changes, it's a soft one. Easy as pie (once you know how, the 1 means go back 1 commit..)

git reset --soft HEAD~1
Community
  • 1
  • 1
Kickaha
  • 3,680
  • 6
  • 38
  • 57