Search code examples
gitgithubversion-controlgit-bash

Delete previous commits and push just the actual commit


Yesterday I tried to commit a repository to GitHub but it had a big file, so it returned an error. After this, today I deleted the big file and tried to commit and push the repository once again.

However, the Git Bash continues to commit and push the first version with the big file, so it's not working.

I have tried to fix this error using git revert and git reset but it didn't work.

How can I commit and push just the actual state of my repository? Not considering previous commits.


Solution

  • You can first squash all the commits hat handled the big file. To do so, you can juste reset (soft) to the commit before the one you tried to push the big file.

    For instance, if your tree is like this:

    --> c0 ---> c1 (commit big file) ---> c2 (revert commit big file) --> c3 (other changes)
    

    You can just reset soft to c0, and re commit your changes under a new commit without integrating the big file that caused problems

    git reset --soft <c0-commit-hash>

    Then, add the files you want to track and commit them:

    git add myfile1.txt myfile2.txt
    git commit -m 'my commit message'
    

    I suppose remote branch stayed at c1.If you want to push to actual/local state of your repository without considering what has been pushed in c1.

    git push -f

    But be careful, force push will rewrite/overwrite the remote git commits that are affected, so it is possible you lose some last changes if they werent took into account in the local commit tree.