The git push command is not working after removing a commit

Asked

Viewed 2,437 times

3

I needed to reverse a commit and locally it works well. Now when I push to my repository, it presents me with error.

My attempt

git fetch origin c6f1668e2fac57401a99a2184a47f0b58c15e403
git reset --hard FETCH_HEAD
git add .
git commit -m "revrt"
git push

ERROR

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitlab.com/XXX'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Now, if I give PULL back to a version I don’t want. What I do?

Thank you! :)

1 answer

4

This is because the HEAD of the local repository is behind the remote, this means that the git will not let you upload the new content without you making it clear that this is what you want.

Solution 1 (Recommended)

Work on a branch separate (or if you have already worked on branch main, use the git stash to separate the branchs). Assuming you’re working at branch develop and the branch main be master, the flow would be that:

Get the latest changes from master:

git checkout master
git pull

Switch to branch develop and make merge with branch leading:

git checkout develop
git merge master

Basically it will bring the changes of master for develop. Finally commit to your branch. In case you are working in a group, you will need to open a merge request to merge the changes in the main.

Solution 2

This flag disables these checks and can cause the remote repository to lose commits; use it carefully.

because it will forcibly overwrite the last state without merge.

git push -f #irá forçar o push

Solution 3

That answer is complete in the third option, git rebase.

  • 2

    It’s become a great knowledge base that your answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.