1

The situation is as follows: we have a file A.out in our git repository that grew over time and now became too large to be pushed to github. I only realized the latter several commits after it became too large, so here is how the repo looks like:

  • COMMIT5 (HEAD) contains A.out exceeding 100mb
  • COMMIT4 does not contain A.out
  • COMMIT3 does contain A.out exceeding 100mb
  • COMMIT2 (origin/HEAD) does not contain A.out
  • COMMIT1 does contain A.out not exceeding 100mb

COMMIT2 is the last commit that I pushed to github, and I now cannot push after COMMIT5.

How can I remove A.out from commits 3 and 4, while keeping it in its newest >100mb version locally (and untracked from git)?

EDIT: The question originates in a large file issue with github, this specific case can probably be handled as explained at How to import git repositories with large files? (pointed out by @phd). But the question as stated is, of course, independent of involved file sizes. In my specific case, the git version is too old to support git-lfs currently, but I don't want to wait until the server's version is updated.

Christian
  • 527
  • 6
  • 19
  • Possible duplicate of [How to import git repositories with large files?](https://stackoverflow.com/questions/37986291/how-to-import-git-repositories-with-large-files) – phd Dec 11 '18 at 14:41
  • https://stackoverflow.com/search?q=%5Bgithub%5D+migrate+file+to+lfs – phd Dec 11 '18 at 14:41
  • @phd: The question originates in a large file and this specific case might be solved as explained in the links. But the rebasing is of course completely independent of the involved file sizes. Also, our server has git version 1.7.10.4 installed, while, according to https://github.com/git-lfs/git-lfs/wiki/Installation, git lfs needs version >= 1.8.2. – Christian Dec 11 '18 at 16:45

2 Answers2

2

git rebase -i HEAD~4 // rebase your git history for last 4 commits

It will show you a list of those 4 commits. You can choose to edit commit3 or even commit2, if you want to change history of remote (not recommending)

While you are rebasing commit3, just add A.out to .gitignore. Add it to commit and continue rebase.

If you didn't use git rebase before, I'd recommend to get familiar with it first

Doğancan Arabacı
  • 3,934
  • 2
  • 17
  • 25
2

I'm assuming the name of the branch you're currently on is 'master'

  1. Copy a.out to a folder outside the git repo
  2. git checkout -b new-branch COMMIT2
  3. Add a.out to your .gitignore, then run git rm a.out. Commit these changes
  4. git checkout master
  5. git rebase new-branch -i

This should result in a few rebase conflicts, because the commits you are rebasing (from master) contain a.out, while the branch you are rebasing onto (new-branch) has made a.out an untracked file

In every conflict that mentions a.out, run git rm a.out, then git rebase --continue to proceed with the rebase

At the end of this process, you can safely delete new-branch with git branch -d new-branch, and copy the a.out from outside the git repo back in.

ack_inc
  • 1,015
  • 7
  • 13