We 2 developers are working on a branch. I did 2 commits, and pushed them, and we later decided we didn't want them anymore.
git reset -i <sha> to the sha before the 2 commits, and then force pushed it to the branch on github.
The other developer has a local copy of the code, with my 2 commits, and also with some commits of his own that he hasn't been pushed to github.
We are not sure what to do to preserve his code and commits but remove the two commits that I have erased from the pushed github branch.
Best How To :
If I understand this correctly, the other developer's local branch looks like this (reverse chronological order):
+- (F) His last commit (Required)
+- (E) His commits 2, 3, 4, etc ... (Required)
+- (D) His commit 1 (Required)
+- (C) Your commit 2 (To be discarded)
+- (B) Your commit 1 (To be discarded)
+- (A) Base commit (one that you reset to)
You want to pick his commits D, E, F, and throw away your commits B and C. If so, do the following in his branch:
- Create a label to commit F.
- Reset to commit A.
- Cherry-pick commits D, E, F. (You may need to resolve merge-conflicts.)
- Force push to remote branch.
This should achieve what you want, however, I would recommend the following unless you have some strong reason to not do so:
- You make another commit undoing your changes, and push to remote.
- Other developer pulls this new commit, and rebases his change over it.
- Then he pushes this to remote.
The end result would be the same, except for the three "extra" commits, B, C and the "Undo commit". This does avoid force-pushing, which is not "wrong" per se, but can causes sometimes cause problems. It also retains your unwanted code in history, should you ever need it in future, perhaps in a different context.