Rammohan Bellamkonda
Rammohan Bellamkonda

Reputation: 21

How to cherry-pick the specific commits of a file?

I have 2 branches: FeatureBranch and ReleaseBranch.

I did cherry-pick a file of commit c1 from FeatureBranch to ReleaseBranch. Later I did some modifications on the same file on FeatureBranch & committed with id c2.

Again I did enhance the same file on FeatureBranch and committed with id c3.

Now I do not want c2 onto ReleaseBranch, I only need c3. I tried to cherry-pick directly with the c3 id but Git returned an error:

could not apply c3...

I know Index in Git is incremental, as I do not want the c2 changes and I need only c3. Is there any way to achieve this?

Upvotes: 2

Views: 236

Answers (2)

nayana
nayana

Reputation: 3932

What abou reverting c2 on Feature branch (or make separate branch from Feature branch and do revert there) making c4 and cherry pick that. I assume the Release branch can be reset to the state before c1.

git checkout -b RevertFeatureC2 FeatureBranch
git revert C2

OPTION1 - with clean MasterBranch - no C1 C2 (but maybe it will work even if there is C1 C2 already)

git checkout MasterBranch
# then cherry pick or merge from RevertFeatureC2

OPTION2 - if its ok to overwrite whole file - no separate changes just in MasterBranch(please double check):

git checkout ResetFeatureC2 the_file.xyz

Upvotes: 1

LeGEC
LeGEC

Reputation: 51780

First : you are maintaining two different versions of your code, one on FeatureBranch, one on ReleaseBranch. You are starting to see the negative effects of having two "live" versions of the code.
I don't know what your constrains are, but "integrating Feature into Release, or ditching Feature" should become a priority milestone in your immediate plans.


You are hitting a conflict on that file. git cannot apply c3 in a straightforward manner, because c2 introduced some changes, which turn the patch c2 -> c3 into something which cannot be applied on Release.

You will have to manually edit the conflict to fix it, then commit the result.

If the changes in c2 are simple to discard, another way is to discard the current cherry-pick, get the complete content of the file, and edit it to remove the changes you don't want to commit on Release :

# -- from ReleaseBranch

# cancel the cherry-picking :
git cherry-pick --abort

# get the content of the file stored in c3 (will contain modifs from c1+c2+c3) :
git checkout c3 -- path/to/file

# edit the file to remove unwanted code ...

# add and commit
git add path/to/file
git commit

Upvotes: 2

Related Questions