4

P.S: Please note that i have read almost all other related questions about this but it still not working at all.

I have the following issue on master branch:

git status

On branch master
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

after reading most of the articles here about this case and asking the company i work for, i am not allowed to:

rebase

or use

git fetch origin git reset --hard origin/master

so i tried:

git fetch origin
git pull

but i have the same result after doing git status

What should i do to fix my local master branch exactly?

my goal:

  • not to push because i am on master

  • get rid of the current commit without affecting master branch

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
John D
  • 285
  • 3
  • 22
  • That's not an issue. It's a common occurrence when sharing remotes. A pull won't solve it as you need to push, like the git message says _"use "git push" to publish your local commits"_. – evolutionxbox Oct 24 '20 at 14:50
  • Do you want to keep the 5 commits you have locally? Or do you want to wipe them and keep in sync with remote master? – RoadRunner Oct 24 '20 at 14:53
  • @evolutionxbox this is `master` branch and the i need to cancel the current change, not to push them – John D Oct 24 '20 at 14:53
  • Are these 5 commits you added and now want to remove them? if so, this might help https://stackoverflow.com/questions/10153486/how-to-delete-the-last-n-commits-on-github-and-locally – Joy Terence Oct 24 '20 at 14:55
  • @RoadRunner I want to wipe them and keep in sync with remote master, the most important without affecting master branch – John D Oct 24 '20 at 14:55
  • @JohnD Then do `git reset --hard HEAD~5`. This will rolback 5 commits. – RoadRunner Oct 24 '20 at 14:55
  • 1
    Does this answer your question? [How to delete the last n commits on Github and locally?](https://stackoverflow.com/questions/10153486/how-to-delete-the-last-n-commits-on-github-and-locally) – evolutionxbox Oct 24 '20 at 15:13
  • Spot on @JoyTerence – evolutionxbox Oct 24 '20 at 15:19

1 Answers1

4

To roll back 5 commits locally to keep in sync with remote master branch, you can do this:

git reset --hard HEAD~5

Or run git log and reset to the HEAD commit of origin/master

git reset --hard <commit id>

You could also just reset to origin/master

git reset --hard origin/master
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • 2
    hopefully this is help someone else in the future, better solution that everything else i have read so far. – John D Oct 24 '20 at 15:03