Reputation: 3369
Current situation:
Two developers are working in the same remote branch titled Sprint1:
Developer 1: Changes some code in say 2 files, commits (accidentally adding 3 other files as well), and pushes to Sprint1.
Developer 2: Realizes somehow Developer 1 also accidentally added 3 files we did not want pushed. So he Reverse-merges the changes back to the commit before Developer 1's changes.
At this point Developer 1 wants to attempt to commit his two files again (and not the extra 3 which he is still working on) and push to the remote branch (Sprint1). The problem is a pull will overwrite Developers 1 local files and effectively "delete" his local changes in all 5 files.
What is the proper way for Developer 1 to get to the up to date head but to be able to push the correct files this time without losing the changes?
Upvotes: 2
Views: 371
Reputation: 47618
The good thing about git is that it's pretty hard to completely delete smth you once commited. If you don't want to perform force push to the repository in order to remove both these commits (the original one and the one that reverted it) and commit your changes from scratch there's what Developer 1 can do:
git pull
- ok, we pull the revert commit and have history like that:
ca798ca revert 'commit1'
cf76cf7 commit1
Well, we 'lost' our changes, but don't worry - next step will bring them back.
git revert -n HEAD
(or explicit git revert -n ca798ca
) to revert revert commit. Now all the changes from the original commit (both necessary and unnecessary changes) are in the index, but not yet commited (thanks to -n
option).
git reset HEAD
to move all the changes from the index back into the working directory.
git add necessary_file && git commit -m 'necessary changes'
to add only necessary changes to index and commit them. All the changes you don't want in this commit will stay in your working directory.
git push
to share your results with the colleague.
These steps are provided AS IS, so if you don't understand something from the above, please, ask before doing it. Otherwise, things might become even more messy.
Upvotes: 1
Reputation: 5004
Have Developer 1 create another branch with his current local edits and then make a pull request on the Sprint1 branch on his local file to his Sprint1 branch (not the new branch he created). He can then run git rm
on the three files he didn't want committed in the new branch, and then merge that branch back into Sprint1.
Upvotes: 1