git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Switching branch before commit

From
Junio C Hamano <junkio@cox.net>
Date
May 10, 2007, 20:08 UTC
Message-ID
<7v3b24jvar.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<769697AE3E25EF4FBC0763CD91AB1B0201D496E7@MBGMail01.mobot.org>
"Ron Parker" <ron.parker@mobot.org> writes:
Show 11 quoted lines
> 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
Previous: Adam Roben
Message 3 of 3 in “Switching branch before commit”
  1. Ron ParkerMay 10, 2007
  2. Adam RobenMay 10, 2007
  3. Junio C HamanoMay 10, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.