Sunday, January 4, 2015

Git Push After Removing Large File

Don't revert the commit and then push because the huge file will still be carried around in the history.
Given that you haven't yet pushed it and that the commit you want to redo is the most recent, remove that commit from your history:
$ git reset HEAD^
This will return your index to the state it was in on the parent commit (HEAD^). Now you have a mulligan: add and commit the way you meant to the first time around.
If you've made other subsequent commits, you'll need to git rebase -i <commit> where <commit> is the SHA-1 of the bad commit's parent. SHA-1 can be obtain by git log.
For example (and note the SHA-1 will be different in your repo)
$ git rebase -i 57d0b28
will drop you in an editor that resembles
pick 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar

# Rebase 57d0b28..121802a onto 57d0b28
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
Replace pick with edit on the line with the heavy commit
edit 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar
Save and quit your editor to return to your shell, where you'll see a message of the form
Stopped at 366eca1... This has a huge file
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue
From there, delete the offending file (--cached removes the file from the index only)
$ git rm --cached big-nasty-file
rm 'big-nasty-file'
amend the commit
$ git commit --amend
and finish the rebase
$ git rebase --continue



If you still facing some difficulty after trying all the steps above, you might already have several commits having these big files included. That's mean you'll have to remove these big file from all the commits that you failed to push instead of just the recent commit. Steps are mentioned below:

Suppose you want to remove a file (containing confidential information or copyright violation) from all commits:
git filter-branch --tree-filter 'rm filename' HEAD
However, if the file is absent from the tree of some commit, a simple rm filename will fail for that tree and commit. Thus you may instead want to use rm -f filename as the script.

other filter documentation can be founded at here

No comments:

Post a Comment