1

I am pretty sure the answer to this question may be found on SO, at least in bits and pieces on several different posts. I have a branch that was created of a different branch. I have made some commits in my branch and then the owner of the other branch has force pushed some commits onto my branch. After that happened, I have made a few more commits.

Now I am asked to get his clean version (by which I think he meant to replace my local branch) and then rebase my branch on top of the new branch (his?) so that his commits (the "force-pushed" ones) will go away. By that I think what he wants is to never see his commits in the history of my branch.

What would be the steps to do this? I found a lot of references to

git fetch
git reset --hard @{u}
git rebase -i origin/master

but not sure of the outcome as I don't seem to be able to get rid of his commits. For anyone needing a visual picture of what I am talking about, here it is:

enter image description here

Any suggestions would be greatly appreciated. I am using Git Bash, btw.

TIA, Ed

Edit: as requested, here is the output of the "git log --graph --oneline --all" command (Sorry, I had to cover all the comments as some may contain sensitive info):

enter image description here

Eddie
  • 271
  • 4
  • 18
  • If I understand correctly, it sounds like it should be a fairly straightforward interactive rebase, dropping the unwanted commits. Likely just `git rebase -i FIRST_COMMIT_TO_REMOVE~`, and mark the offending commits as `drop`. See https://stackoverflow.com/a/42522493 – Hasturkun Dec 02 '21 at 16:39
  • @Hasturkun Thanks for your suggestion. See my second comment to Nikola. I need to get rid of the commits but preserve the code changes contained within. – Eddie Dec 03 '21 at 13:15
  • If you want to preserve his changes, you can `squash` or `fixup` to unify these into a new commit (Though I have to say, your intention is somewhat unclear, It sounded like you want these removed from history, which implies that they won't exist). I'll mention in advance that you also have the option of reordering commits, if the intention is to have his commits happen after yours (or the other way around). – Hasturkun Dec 03 '21 at 17:45

1 Answers1

0

One tip, post the output of git log --graph --oneline --all with your HEAD, so we have a better insight on what is going on.

If I understood correctly, you should try the following: (make sure you don't force push something. Use hash codes for arguments on reset)

git reset his-last-commit (to uncommit recent edits after his commits)

git stash (to save them)

git reset --hard your-commit (this deletes his commits)

git stash pop (this applies your recent edits on top)

If this doesn't work, you should try git cherry-pick

Nikola Diklich
  • 409
  • 1
  • 7
  • 15
  • Which one is my "your-commit" in the "git reset --hard your-commit" command? Would it be my last commit before he added his 4 commits? Also, if it is my last commit before he added his stuff, wouldn't this "reset --hard" cause his changes to be lost? – Eddie Dec 02 '21 at 17:35
  • "By that I think what he wants is to never see his commits in the history of my branch." Doesn't that mean it was supposed to lose his changes? – Nikola Diklich Dec 03 '21 at 00:37
  • No, he does not want to lose his changes, as they are important. He just wants those 4 commits to go away from the history of the branch. He did mention that what he wants can be achieved with a rebase, so maybe the suggestion @Hasturkun has made makes sense – Eddie Dec 03 '21 at 13:13