1

I realize that there have been similar questions posted about this subject; I'm afraid I'm not grasping the best way to recover from this situation. I'm using SourceTree on Windows.

I branched from dev, made some commits to a handful of files, pushed to the remote, and merged back into dev. It turns out that there are many pre-existing commits in dev that are not going to be ready to be promoted to master any time soon, and I really should have branched from master instead.

So I made a new branch from master, and I then cherry-picked the commits from the original branch which I know are good. One wrinkle is that two of the files that I originally modified will have different changes in the new branch.

However, I'm unsure as to best handle the process of resetting the dev branch back to the state it was in prior to the two merges that I made. Should I:

  • Check out the first branch, hard reset it to the point where it was originally branched, commit those changes, push to remote, and merge?
  • Or, check out the first branch, Log Selected on each affected file, reset each file to the prior commit, commit all changes, push, merge?
  • Or something else?

Here's the graph. The left-most branch is the original branch from dev. The next branch is the new branch from master, with cherry-picked commits. The blue line is the commit that I want to reset dev back to. Thanks! enter image description here

earachefl
  • 1,880
  • 7
  • 31
  • 55
  • do you know if anyone has already pulled from `dev` the new (but bad) commits? – mostafazh Sep 18 '17 at 17:14
  • @mostafazh, I'm pretty sure no one has... my branches are the only active branches, and the earlier commits are from May and there's been no activity on those branches since then... I can confirm with the other devs if need be. – earachefl Sep 18 '17 at 17:23

1 Answers1

1

Given that NO ONE has pulled from the remote develop branch yet, all what you need to do is to overwrite what commit the branch develop is pointing at, and follow those command to do that:

# Change your local develop to point that old version of that branch 
git checkout -B develop <commit-hash-of-blue-line-in-pic>
# FORCE push your local develop to the remote develop
git push origin -f develop
mostafazh
  • 4,144
  • 1
  • 20
  • 26
  • what if the remote prevents pushes directly to dev? – earachefl Sep 18 '17 at 17:58
  • in that case you will need to do 2 git reverts for the 2 merges you did from your branch to `develop`. Take a look at https://stackoverflow.com/a/31223198/1772245 and https://stackoverflow.com/a/7100005/1772245 – mostafazh Sep 18 '17 at 18:02
  • @earachefl any luck? – mostafazh Sep 19 '17 at 12:46
  • 1
    our sysadmin is opposed to doing force pushes to any of the holy trinity. I ended up just reverting each of the commits and merging that to dev. Definitely messier but I gotta keep the peace! Thanks for your help though. I'll give you the answer but am thinking about deleting the question, as it really is a duplicate. – earachefl Sep 19 '17 at 22:38