简体   繁体   中英

Restore lines deleted in the last commit but keep added lines. Combine files line-wise unified

I made some changes in several files by editing lines (as usual) and made a commit. Then assumptions changed and I figured out I need to actually use both the deleted lines AND added lines nicely together. How to restore the deleted lines (exactly as git show displays in red) but keep also the added (green) lines without manual copy-pasting from the previous revision?

Preferable way would be to trigger a "merge-like" conflict resolution between the previous and the current commit. I would then clean markers keeping everything else (from both sources). Of course I can't do it since commits are on a merged path.

Another way would be extracting both versions of a given file and combine them "line-wise" (as a unified diff format) into a single file with some Bash text-processing tool.

I may have overlooked something trivial. What is the easiest work-flow to aggregate up those all diff-affected lines (either by conflict resolution or some other trick)?

There are a couple ways that I would go about doing this. You can do a git revert <sha of deleted commit> . This will create a new commit that is the inverse of the commit.

Or you can git checkout <sha of deleted commit>~ -- <files changed> and the manually make a new commit adding the lines using git add -p to stage.

Depending on where in the file your lines were added, you may end up with a conflict but this should be relatively minor to handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM