1

Say I've been working on files a, b, c and d in a single branch. Now I realise I'd prefer to have my changes to a and b in one branch, and my changes to c and d in another, so from each branch's perspective it's as though I've only ever modified two files.

Is this possible?

0

1 Answer 1

1

The index is the tool git uses to handle this kind of need.

All your changes are detected by git but not "staged" (as in, ready for commit) until you say so.

# in case you already added all or part of your changes, undo it
git reset

# if needed, create the new branch (or stay on the one you worked on from start)
git checkout -b new_branch

# now add only parts for branch 1
git add a b
git commit -m "Modified a and b"
# let's call this commit Y

# now for the second part
git checkout -b new_branch_2 master
git add c d
git commit -m "Modified c and d"
# let's say this commit Z

resulting in this situation (I assume here your base branch before all this was called master, whose last commit is X)

     Y <<< new_branch_1
    /
---X <<< master
    \
     Z <<< new_branch_2 <<< HEAD

Finally, to be very explicit, now your files a and b are indeed unchanged (from master) in Z.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.