I am participating in developing a Python 3 project. Let's say my branch is named "my_branch". I had to do a quick and small change in my code and because of my laziness I did it in GitLab webui. Then I made some changes in my local repository without pulling my branch. Now I cannot push the changes because my_branch is different than my local repository. How can I revert changes in a remote branch my_branch without creating new branch. I basically just want to do a step back.

git push gives an error: update were rejected because the remote contains work that you do not have locally

2 Answers

0
Community On

If you have...

Local: A->B->C->E
Remote: A->B->C->D

All you have to do is a force push. git push -f origin my_branch The D commit on remote should disappear.

1
Krantisinh On

To summarise your situation, you have one commit on your top of branch which you want to get rid of.

There are two scenarios:

  1. You want to preserve your changes in the top commit
  2. You no longer care for changes in the top commit

If you want to preserve changes and still want to remove the commit and get latest from remote:

git reset --soft HEAD~1
git stash
git pull

Soft reset with HEAD~1 will move the changes from your top commit and get those in staging area and also remove that commit from branch. Now, your changes are available in staging area. You can easily stash them and you are all set to pull from remote.

Once you pull from remote, you can pop the stash again and your changes in the last commit are back.

If you don't care about your commit any more then:

git reset --hard HEAD~1

Hard reset will just remove your commit from your branch.

Hope it helps.