Zelgadis February 2016

Git - Porting changes to the HEAD from an old branch

I'm trying to apply some changes made in a specific commit onto the HEAD of my Git repository. The branch this commit was made in is almost two years old and was never merged into the main branch of the repository for a few reasons.

However, there are a few changes made in that commit that I'd like to port over, in particular the additions made to two files: a method signature that was added to a Java interface and its implementation in the concrete class. No lines were changed or removed those two files.

My problem is, if I try to merge or cherry-pick this commit onto my current branch, I get all sorts of conflicts, since the commit is so old. Even in those two files, a lot of code has been changed in the meantime, and now Git asks me if I want the lines in that commit or the lines that have been added in the past two years. What I'd actually like is if Git could just replay on the HEAD the same additions that were made in that commit, without replacing any content.

In this particular instance, being only two files, I can port over the changes manually, but I was wondering if I was missing a specific Git command/option that could automatically fix analogous situations.

Thanks.

Answers


alextercete February 2016

You can check out specific hunks from a file in a commit:

git checkout -p <commit> -- <path/to/file>

Git would then go through all the differences in that file between <commit> and HEAD and ask you whether you want to stage a change or not. This can be quite overwhelming, though, depending on the size of the file and on how much it has changed.

Post Status

Asked in February 2016
Viewed 1,044 times
Voted 14
Answered 1 times

Search




Leave an answer