Thread (1 message) 1 message, 1 author, 2016-06-15

Re: Switching branch before commit

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:43:09

"Ron Parker" [off-list ref] writes:
I know this is probably a FAQ and I thought I found it somewhere once,
but... How do I commit changes from in my working directory to another
(possibly non-existent) branch?
 
All too often I am working on changes and realize I am sitting on master
or a topic branch and I need to commit my mods to different branch.  I
really don't like:
 
    git commit
    git branch <other-branch>
    git reset --hard HEAD^
I do not like that either, and I wouldn't do that.  In fact that
would not work, as "git branch" would not switch to the other
branch you just created.

I would do:

	$ git checkout -b other-branch

which would create and switch to other-branch, based on the
current HEAD (in your case, 'master'), WITHOUT disrupting what
is in your working tree and the index.

and then (perhaps after working on it some more to perfection):

	$ git commit

to commit that work I (mistakenly) started on 'master' in that
other branch.  Then I would come back to master:

	$ git checkout master
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help