2

Essentially I want to roll back two commits in my local repo. Nothing has been pushed to remote yet.

So in my GIT repo I accidentally added a huge zip file in a commit (A). I realized this, removed the zip file using finder and made another commit (B).

But now I realize that if I were to push this to remote, it would include that entire zip file...

And to make things more complicated I also had a bunch of important changes to several files in that first commit (A) that I don't want to lose.

How do I remove that one zip file from history before pushing to remote? Can I move back to the state of my files before these two commits without losing all the changes made in several other files?

Matt Welander
  • 8,234
  • 24
  • 88
  • 138

2 Answers2

4

The easiest way to fix it would be to use the git reset <commit> command.

In your case, since you want to rollback two commits, you can do :

git reset HEAD~2

From git help reset :

git reset [<mode>] [<commit>]

--mixed Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

After that, your files in your repos won't be changed at all, but the two latest commits will no longer exist.

You can then select which files you want to commit just how you normally would.

Community
  • 1
  • 1
edi9999
  • 19,701
  • 13
  • 88
  • 127
0

Link: How to move your HEAD/ revert changes

Read the above link for full details.

You can use :


Taken from the above post here is a the flow of what can be done to reset your HEAD

enter image description here


Other options:

How to remove big files from the repository

You can use git filter-branch or BFG. https://rtyley.github.io/bfg-repo-cleaner/

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