Search code examples
gitgit-commitcherry-pick

How to cherry pick uncommited changes in some files into a new branch?


Say I have branch and branch . In I have been doing several changes in file1, file2. But, also, I ended up doing refactoring on things that were apparently fixed, say in fileA, fileB.

Now I would like to create a different branch that isolates these changes in <my_feature> to have say <my_updates> with commits to just fileA, fileB.

Currently I have changes not staged for commit in file1, file2, fileA and fileB.

I have been reading about git cherry-pick and git apply but it involves things that are already commited.

Now that none of these changes are commited, I don't know if I should be commiting this and cherry picking or if there is a better way to handle.

To make sure the picture is clear, let's make some visual out of it:

I wanted to do:

master ------------------
    \--- my_feature ----/
          file1
          file2

But now I have

master ------------------
         my_feature ----/
          file1
          file2
          fileA
          fileB

and I want to merge this (changes in fileA, fileB) before going ahead with <my_feature>:

master ------------------
    \--- my_updates ----/
          fileA
          fileB

So that <my_feature> will just contain the changes in file1, file2.


Solution

  • You could git stash, move to a new branch and then git stash pop. Then you can do further work or commit the result.

    You could also create a patch by directing output of git diff into a file (git diff > changes.patch) or the clipboard (git diff | pbcopy for OS X or git diff | xclip -selection clipboard in Ubuntu). Then you can apply that patch with git apply to any other branch.