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

Re: How to create & switch to a branch after changes have already been made?

From
Llinux@horizon.com <linux@horizon.com>
Date
Jul 3, 2007, 03:05 UTC
Message-ID
<20070703030535.2958.qmail@science.horizon.com>
The magic you're looking for is "git checkout -b <branch>"

You can do that even with uncommitted changes hanging around. Because you don't specify a commit to check out, it just "checks out the HEAD again", which does nothing. The only thing left is the part that creates and switches to a new branch.

Now, if you've already created the branch, you can either do: git checkout <newbranch> which, since <newbranch> equals HEAD, just switches the HEAD symlink without actually changing the checked-out commit.

Or, if you want to be really low-level,
git-update-ref HEAD <newbranch>

Which is the underlying tool that git checkout uses. It bypasses all the safety checkes, but it might be easiest to understand.

Now, if you've already committed, you can just do:

git checkout -b <newbranch> git branch -f <oldbranch> HEAD^

Admittedly, I sometimes with for a "git commit -b <branch>" shortcut.
Message 1 of 1 in “Re: How to create & switch to a branch after changes have already been made?”
  1. linux@horizon.comJul 3, 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.