Reputation: 9476
When there is a merge conflict, you get edit a file to decide what ends up staying.
Is there any way to get that same interface even though this would be a fast-forward commit?
That would be the easiest way for me to do what I want to do.
I prematurely deleted about 20 lines of code in a file and added new code.
When I commit, I would like to commit the new code and bring back the old code.
Upvotes: 0
Views: 58
Reputation: 142312
When I commit, I would like to commit the new code and bring back the old code.
There are few ways to accomplish what you're asking:
The simple one is simply to copy the code you want to be in your file and commit it.
git add -p
You can always use the interactive add -p
. It will allow you to edit the added file according to your needs.
You will be able to choose which lines to add in which one not to.
git rebase -i
Once you have committed your files you can rebase
the content, edit your files and add any code into the file.
You can use squash which is an interactive rebase. You just have to keep in mind what is the result of a rebase. Anyone who have a copy of the branch will have to delete it and fetch it again since your history was updated.
Once you are done re-writing history you will have to delete the remote branch or push using the -f
to force the push.
In order to do a git squash follow those steps:
// X is the number of commits you wish to squash
git rebase -i HEAD~X
Once you squash your commits - choose the e
for editing the content of the desired commit, make your changes and commit it. In your case add the missing file to the B
commit.
git format-patch
git format-patch
will generate a patch (diff) file per commit in you history so again you can edit it in the way you wish and then git apply
the patch
Upvotes: 1
Reputation: 18558
a merge conflict occurs when multiple bits of the same code are changed. In your case you have changed some code and then changed it again, So no conflict.
If you do not need the code you have added you can do a revert commit.
If you want part of it you will need to do a manual merge git show {commitid}:{pathToFile} > {oldFile}
you can then pick and choose the code you need
Upvotes: 1