tags:

views:

54

answers:

2

Hi,

I create a working repository in HG. And I have modified some files.

How can i move my all my modification to a branch (a branch that I have not created)? (kind of 'git stash' and the move the stash away change to a branch. Actually, I am not sure how I can do that in git either. If you know, I appreciate if you can tell me in git as well)

Thank you.

+3  A: 

For Git, if you have not yet committed your files, just type:

$ git checkout -b newbranch
$ git commit -m "a message"

Every file staged at this point will be committed in the new branch.

For Mercurial, branches are not names by default, and a new branch only occurs (within one repo) if a commit has a parent which has already a child commit:

alt text

But you can create a new branch name for your next commit:

hg branch branch-1          # start a new branch name
                            # modify something in the repository
hg commit -m "branch-1"     # the changeset has branch name "branch-1"

(See also a Guide to branching in Mercurial, and Git & Mercurial models)

VonC
OP asked for checking into an _existing_ branch, so instead of `git checkout -b` and `hg branch`, you should use `git checkout newbranch` and `hg update branch-1` to switch to the branch.(And I think neither of those commands should mess with modified files in working tree.)
pydave
+2  A: 

First check to make sure that neither the ShelveExtension nor AtticExtension do exactly what you want.

If you're doing it manually in mercurial I'd avoid a named branch, and just use another head. If for example, you already made the changes you want to "put away" for a little bit.

hg commit -m 'working on Xxx' # you created a new tip
hg update -r -2               # switch to the revision before the tip

and now just start working there. You can find that anonymous branch later with hg heads and merge it in with hg merge.

Ry4an
+1. You can tell by my answer that I am still too much into the "named-branch" Git model and not enough in the "one branch (and some closely-related anonymous branches) per repo" Mercurial model;) I have still trouble adapting to creating a new repo to create a new branch...
VonC
Thanks, VonC, you're a class act. I'm a dwindling minority in the Mercurial world since I"m still a "clones as branches" guy by the definitions in the "Guide to branching in Mercurial" to which you wisely linked, but it certainly always helps me find the soln that doesn't involve typing "hg branch". :)
Ry4an