ReynierPM ReynierPM - 1 month ago 8
Git Question

Can I merge one file|commit or I need to merge the whole files?

I have two different branches:

branch_1
and
branch_2
. I made a few changes in a file
index.php
at
branch_2
among other changes in other files and I commit them. Now I am working on
branch_1
and I need the changes I have made in
index.php
only. Is there any way to bring
index.php
from
branch_2
into
branch_1
? Be aware I don't need every change I have commited on
branch_2

Answer

I think that you may find the answer at this page. The long story short:

  • Checkout the path from the branch you want to merge using

    $ git checkout source_branch -- <paths>...

  • or merge hunks selectively

    $ git checkout -p source_branch -- <paths>...

    You can eventually use reset and then add with the option -p,

    $ git reset <paths>...
    $ git add -p <paths>...

  • Commit changes

    $ git commit -m "Pick several changes from index.php"