0

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

M.Urbanek
  • 19
  • 1
  • 1
  • Possible duplicate of [git error: failed to push some refs to](https://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to) – evolutionxbox May 10 '19 at 10:53
  • Possible duplicate of [git: updates were rejected because the remote contains work that you do not have locally](https://stackoverflow.com/questions/24357108/git-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-have) – phd May 10 '19 at 12:44
  • https://stackoverflow.com/search?q=%5Bgit%5D+update+were+rejected+because+the+remote+contains+work+that+you+do+not+have+locally – phd May 10 '19 at 12:44
  • It's either `git pull [--rebase]` or `git push --force`. – phd May 10 '19 at 12:44

2 Answers2

1

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.

Krantisinh
  • 1,579
  • 13
  • 16
  • How do you know the OP even made a commit? – Tim Biegeleisen May 10 '19 at 11:25
  • @TimBiegeleisen Infered from question. He says 'Now I cannot push the changes because my_branch is different than my local repository.' and 'git push gives an error: update were rejected because the remote contains work that you do not have locally'. This can only happen if you commit and try to push. – Krantisinh May 10 '19 at 12:11
0

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.

EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
  • 1
    For a public branch which is shared, this is _seriously_ bad advice. In general, _never_ force push a branch which is shared by other people. – Tim Biegeleisen May 10 '19 at 11:25