2

Let say I have a branch on my local A and this is exist in remote as well origin/A. Both my local and remote branch is in sync. For example in local I have commit like - C1, C2, C3 and C4 and same in my remote as well.

Now I want to revert the change that I commit for C4. I already pushed this to remote, so I want to do that in remote as well.

NOTE: I found lot of question in stackoverflow for this. Here is one. But it's not working for me!

As per this I tried:

git reset --hard HEAD~1
git push -f origin A

First command works fine and my local version does not have the C4 commit now.

Problem is in the second command. It showing remote: error: denying non-fast-forward refs/heads/A (you should pull first).

How to solve this issue?

Community
  • 1
  • 1
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
  • GitHub is not allowing you to do the force push. You need to check with your admin to ask for permission to do this (by the way, your 2 commands are spot on for what you want to do). – Tim Biegeleisen Nov 25 '15 at 07:08

1 Answers1

2

Your current approach to remove the most recent commit is completely correct:

git reset --hard HEAD~1
git push -f origin A

Unfortunately, GitHub does not seem to be allowing you to do the force push. However, there is an alternative. You could instead git revert the most recent commit. This will add a new commit on top of the branch which will undo whatever the most recent commit did. Presumably you won't have any problems in adding a new commit in GitHub. If you want to go this route, try the following:

git revert HEAD
git push origin A

Your new branch diagrams will be left looking like this:

remote: C1 -- C2 -- C3 -- C4 -- R
local:  C1 -- C2 -- C3 -- C4 -- R

where R is the commit which reverted commit C4. Functionally speaking, the two branches will behave as if neither the C4 nor R commits are there, i.e.:

local/remote: C1 -- C2 -- C3
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Hmm! Good to know BTW I'm using `beanstalk`. Let me try the second approach that you mentioned above. BRB. – Deepak Biswal Nov 25 '15 at 07:23
  • If you're wondering why you were not allowed to force push, it is likely because your admin does not to allow developers to rewrite the history of branches which are public in GitHub. You could have asked the admin for the permission to do this, but functionally speaking doing a `git revert` is just fine. – Tim Biegeleisen Nov 25 '15 at 07:36