Search code examples
gitgithubgit-pushgit-checkoutgit-revert

why cant I push reverted commit in github?


I'm pretty new in git.I was working on a project and I used new commit that ruined my project.

so I used git log and git check out to go back to last commit.that was successful on my computer but when I try to push it on github repository I see this error:

error: failed to push some refs to 'https://github.com/bamdadghoori/courses.git'

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

bamdad@DESKTOP-MJ4O8KO MINGW64 /e/courses (master)

these are some ways I tried

1-reinitializing and using new commit

2-using git revert


Solution

  • If that's really what you want to do git push --force should do the trick. Be aware that it might erase your old work if you misuse it.

    Just like you suggested git revert is probably the better option as it does not delete or overwrite any history.
    Let's say your branches git log looks like this:

    commit 95c374fca46bee52b9fd18e090e6afdfbe73813d (HEAD -> main, origin/main)
    Author: Git User <[email protected]>
    Date:   Fri Mar 19 17:02:49 2021 +0100
    
        Latest commit message
    
    commit 1177cab6490264e922075f28804404f4e1c08f76 (origin/main, main)
    Author: Git User <[email protected]>
    Date:   Wed Mar 17 12:35:33 2021 +0100
    
        Commit message of the one you would like to keep
    

    If you want to revert the latest commit you can just do git revert HEAD (HEAD is always referring to the last commit of your branch) or to revert any commit every you can copy paste the commit hash. In this case git revert 95c374fca46bee52b9fd18e090e6afdfbe73813d.
    Then accept the suggested commit message which is probably opened in vim by typing ZZ (mind the uppercase) or :wp and then press Enter.