1

I want to revert code to an older commit in my remote repo's master branch. But the problem is I do not have administrator rights to directly commit/push anything to the master branch. All I can do is create a new branch from master, do my changes there and then merge it with a pull request to master.

I took a new branch from master, reverted the code to where I wanted but when I try to merge it back to master it says no differences found.

I did the below to revert the code in branch and the revert happens exactly how I want

git reset --hard <commit>
git push --force

How can I merge these changes to master? Is there any other way by which I can revert the code in master?

derek
  • 59
  • 2
  • 10
  • Are you sure you branched from an updated master? You may be comparing a modified or out of date local copy. Try pulling master first. You may also need to delete your local master and fetch the remote again. – isherwood Apr 08 '21 at 14:04
  • Yes I did, infact I branched out twice from the master, reverted it using the code I just pasted above and then when I raised a PR, it says no differences found. – derek Apr 08 '21 at 14:10
  • The commands you show there don't tell the whole story, so we're left to speculate what you did. Also add your branching and commit commands. – isherwood Apr 08 '21 at 14:12
  • 1
    If you went back in time in your new branch, don't expect the merge to master to pick up new changes, since it has *more* than your branch. Merging is not syncing, only commits new to your master will be detected as 'to be merged'. – Romain Valeri Apr 08 '21 at 14:17
  • Does this answer your question? [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/a/21718540/761202) – AD7six Apr 08 '21 at 15:30
  • 1
    In Git, `revert` and `reset` mean two completely different things. I think it would help if you [understood the difference](https://stackoverflow.com/q/8358035/184546) and updated your question accordingly. Are you really trying to reset, or are you just trying to undo (revert) a specific set of commits in the middle of the branch? – TTT Apr 08 '21 at 16:57

1 Answers1

2

You cannot remove commits from master using a separate branch. Branches are meant for adding commits on the tip of master — that is why your git merge does not work.

Since you do not have permission to force-push directly to master, you would have to create revert commits on a separate branch and merge them in.

git revert COMMIT_HASH_1 COMMIT_HASH_2

This will add a new commit that reverses the changes made in the commits you specify. You can then merge this back into master.

Without permission to force-push directly to master, I believe this would be your best bet. Force pushing to master is usually disabled to prevent exactly what you are trying to do — rewrite history.

Ali Samji
  • 479
  • 2
  • 7
  • `Without permission to force-push directly to master` it is worth reinforcing in the answer that even if it were possible to force push, that would be abnormal, problematic, not recommended etc. and reverting is the absolutely-normal action to take. It's not a compromise to revert, it's the primary choice with very (very) few circumstances where that wouldn't be the answer. – AD7six Apr 08 '21 at 15:27
  • @Ali really appreciate the great explanation you gave around it. Is there a way I can revert back to a commit which is ~ 70 commits before what I currently have? Instead of specifying hashes for each one of them? – derek Apr 08 '21 at 16:01
  • [The answer AD7six linked](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit/21718540#21718540) shows how to specify a range of commits and other nuances of the revert command. – Ali Samji Apr 08 '21 at 20:54