0

I have committed a file >100MB (github's limit) 3 commits ago, and it won't let me push my new commits.

I want to delete those 3 commits while keeping my work, so I can re-commit them, but without the massive file.

What's the easiest way?

PS I only have access to git command line.

cjm2671
  • 18,348
  • 31
  • 102
  • 161

1 Answers1

2

Full information can be found in this post:
How to move HEAD back to a previous location? (Detached head)

The best option is to do something which is not desrivbed there,

git reset HEAD --mixed HEAD^3

It will checkout the 3rd commit and use the above post to continue working from this point on.

Follow the checkout as its described there:

git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point you can create a branch and start to work from this point on.

# Checkout a given commit. 
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
#in order to be able to update the code.
git checkout <commit-id>

# create a new branch forked to the given commit
git checkout -b <branch name>

In order to do a git squash follow those steps:

// X is the number of commits you wish to squash, in your case 6
git rebase -i HEAD~X

Once you squash your commits - choose the e for edit the commit.

enter image description here


If you just want to remove big files from the repo use this: You should use this tool:

https://rtyley.github.io/bfg-repo-cleaner/

It the prefect tool for this kind of task

BFG Repo-Cleaner

an alternative to git-filter-branch.

The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:

  • Removing Crazy Big Files
  • Removing Passwords, Credentials & other Private data

Examples (from the official site)

In all these examples bfg is an alias for java -jar bfg.jar.

# Delete all files named 'id_rsa' or 'id_dsa' :
bfg --delete-files id_{dsa,rsa}  my-repo.git
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    won't this erase all my work in those 3 commits in folder? – cjm2671 Mar 23 '16 at 19:59
  • Nope, if you checkout and then create a new branch from it they will still be there and you can later on do a git rebase -i to edit the commit. – CodeWizard Mar 23 '16 at 20:00
  • You shouldn't have just posted `git reset --hard HEAD^3` on its own before you edited your post - that does delete OPs three most recent, un-pushed commits. – user234461 Mar 23 '16 at 20:02
  • 1
    Or, you can `git reset --mixed HEAD~3` to reset the index but leave your working tree (actual visible files) alone. Then, just add and re-push only the files you actually want. – Useless Mar 23 '16 at 20:02
  • @user234461 No, it doesn't _delete_ anything, it just updates the working copy to reflect that commit. You can immediately reset back to the previous HEAD and everything will still be there. – Useless Mar 23 '16 at 20:03
  • I agree and did a downvote even when this will do the PO asked for :-( – CodeWizard Mar 23 '16 at 20:04
  • Looks like `git reset --mixed HEAD~3` was the right answer, thanks @Useless! – cjm2671 Mar 23 '16 at 20:05
  • It was, since reset doesn not delete commits, you can always do a git cherry-pick to add them back. – CodeWizard Mar 23 '16 at 20:08
  • Updating the answer to be more correct with the --mixed instead of the --hard – CodeWizard Mar 23 '16 at 20:09
  • Glad to help, read the comments under your question, i posted some helpfull inks for you, – CodeWizard Mar 23 '16 at 20:15