AndreKR
AndreKR

Reputation: 33678

Is there a way to commit changes to multiple branches without stashing?

Often I have a bunch of changes in my working copy that I would like to commit to different new branches, like this:

    o   third
  o |   second
o | |   first
| | |
\ | /
  o     base

What I do then is:

Git wouldn't just let me check out the base branch without stashing the rest of the changes, even though it wouldn't (and I think cannot) create a conflict.

Is there an easier way to do this?

Upvotes: 4

Views: 358

Answers (2)

Greg Bacon
Greg Bacon

Reputation: 139471

In your question, you wrote that you don’t expect merge conflicts. The test with git checkout is more conservative: do the originals (that is, as committed) of the locally modified files in the current and destination branches differ at all?

After running the following to create the first branch

$ git checkout -b first

$ edit ..

$ git add -p .

$ git commit -m first
 1 files changed, 1 insertions(+), 0 deletions(-)

you are likely seeing an error similar to

$ git checkout base
error: Your local changes to the following files would be overwritten by
checkout:
        [files ..]
Please, commit your changes or stash them before you can switch branches.
Aborting

when attempting to switch back to base.

The --merge option to git checkout works in this sort of situation.

-m
--merge

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).

Running git checkout -m base will get you back where you started—but perhaps further than you want. You will be on the base branch with all your edits rather than only the changes intended for second and third. For each commit, you will need to discard the changes for the other two branches as opposed to progressively whittling down your changes.

Upvotes: 1

Shahbaz
Shahbaz

Reputation: 47523

If you write:

$ git checkout -b new_branch base

git will create a new branch starting from base and keep your local changes. It's equivalent to

$ git stash
$ git branch new_branch base
$ git checkout new_branch 
$ git stash apply

So your workflow now becomes:

$ git checkout -b first master
$ git add -p; git commit         # commit changes for first branch
$ git checkout -b second master
$ git add -p; git commit         # commit changes for second branch
# etc

Upvotes: 3

Related Questions