1

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.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 2
    Commit `file1` and `file2` on your branch, and then check out `master`. The uncommitted changes will be carried over. – poke Apr 27 '16 at 07:15
  • 1
    Use poke's method here as it is the simplest. Note that using `git stash` also works, but it does so by making commits. You can make commits yourself and copy things around as needed and then "un-make" the original commits, too. This does basically the same amount of work inside git as using `git stash` (since it makes commits), it's just a question of what you are personally comfortable with. – torek Apr 27 '16 at 08:59

2 Answers2

2

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.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Many thanks! `git diff fileA fileB > /tmp/changes.patch` and then `git checkout master; git checkout -b my_feature; git patch /tmp/changes.patch` made it. – fedorqui Apr 27 '16 at 08:20
0

For the record, this is the full set of commands that I used to make it work:

While in the branch <my_feature>, store the diff in a patch:

git diff fileA fileB > /tmp/changes.patch

Stash the changes to reuse them later on:

git stash -u

Create a new branch from master with that patch:

git checkout master
git checkout -b my_updates
git patch /tmp/changes.patch
git add fileA fileB
git commit -m "Updated fileA, fileB because blabla"
git push

Go back to the branch <my_feature> and recover the stashed changes:

git checkout my_feature
git stash pop

Get rid of the changes in fileA and fileB, since they are already in place:

git checkout fileA fileB
fedorqui
  • 275,237
  • 103
  • 548
  • 598