0

I am new to git, I am trying to revert back about 20 commits that are already committed to the master branch by someone else. I thought I could use got rebase and pick and chose which commits to revert and which ones to edit and which ones to leave. Is this not the case? Can rebase only be done in my local repo? Is there a way to go back 20 commits with one single script? Or does it have to be done with git revert each time? Thanks in advance

artemisia480
  • 77
  • 2
  • 7
  • if you trying to just revert all your 20 commits, you could do: "git reset --hard HEAD~20", then "git push -f" to push your changes to remote – Rahul R. Jul 18 '18 at 17:54
  • Thank you for your respond. Yes but they're not my commits, their in the master branch, that other people committed over the past week. – artemisia480 Jul 18 '18 at 17:56
  • so you are trying to modify your master and others commits?, are you sure it wont affect others? – Rahul R. Jul 18 '18 at 17:59
  • Remember that there is no single Source of Truth repository: there are *many* repos (your clone and everyone else's clones) and all are equally masterful. If you remove some commits and provide new different ones, you have to get everyone *else* to do the same thing. That's what rebase is about: discard some old commits in favor of shiny new copies. – torek Jul 18 '18 at 17:59
  • well that's what I am asking, can I do it without affecting others? I have been doing it one at a time, but is there a way to do all 20 at once? – artemisia480 Jul 18 '18 at 18:00
  • when you say, "Remember that there is no single Source of Truth repository" isn't the master branch the one source of truth that everyone needs pull from and keep their stuff updated? – artemisia480 Jul 18 '18 at 18:01
  • git rebase will affect others, so as far as I know you have to go with git revert as @mb21 suggested. Refer this: https://stackoverflow.com/questions/1463340/how-to-revert-multiple-git-commits – Rahul R. Jul 18 '18 at 18:01
  • I see! Ok thank you! thank you! – artemisia480 Jul 18 '18 at 18:04
  • No: everyone's `master` is their own! The name `master` just remembers one commit hash ID. The *commits* are what matter; Git uses the names to find the commit hash IDs. Rebase means "copy some commits to new ones with new, different hash IDs, then discard the originals in favor of the new copies." You have to get everyone else to do the same! – torek Jul 18 '18 at 18:04
  • The reason to use `git revert` is that Git is built around the idea of taking *new* commits (contributed from anyone anywhere) and *adding* them to your existing collection of commits. Revert adds a new commit that undoes whatever was done in a previous commit. So there's no actual discarding: you're just adding new commits. – torek Jul 18 '18 at 18:08
  • Thank you for expanding that. I now understand. Not sure why I thought I could somehow rewrite history in git using rebase. Thank you again! – artemisia480 Jul 18 '18 at 18:09

2 Answers2

0

No, if you change the history of the repo locally and then want to push it normally, this will not work. You'd have to force push it. Then you have the next problem: If the history of someone else differs from the history of the remote repo, he can no longer pull. That means he would have to delete his repo and clone it again.

qräbnö
  • 2,722
  • 27
  • 40
  • thanks for your response. I don't understand though, I am trying to edit about 20 commits that are part of the master, not local, that other people committed. Can I do that with git rebase? – artemisia480 Jul 18 '18 at 17:51
  • master is not synonymous with remote. How many people use the repo? Do you know everyone personally? – qräbnö Jul 18 '18 at 17:56
  • About 30 people use the repo....I guess I need to read about remote vs master. I thought master was the main branch everyone cloned and worked out of and committed to. – artemisia480 Jul 18 '18 at 18:02
0

You can rework your feature branch however you see fit. git rebase -i HEAD~20. An editor pops up where you drop those commits you want to drop and save. Finally to make the changes affect the remote: git push --force.

And indeed, you should only do that if you're sure no-one else was using that remote branch, because you just removed commits from it and that would make it incompatible with other people's work on that same branch. If not you can use git revert, which adds a commit that undos the changes by a previous commit.

mb21
  • 34,845
  • 8
  • 116
  • 142
  • Thanks for your response. I did used git revert for each commit, is there a way to git revert multiple commits at once? And my goal was to use git rebase not on my local repo but on the main master repo. Is that a thing? – artemisia480 Jul 18 '18 at 18:04
  • answer is here https://stackoverflow.com/questions/1463340/how-to-revert-multiple-git-commits, adding again just for others reference. – Rahul R. Jul 18 '18 at 18:07