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

1 messages from 2007-07-03 to 2007-07-03. Participants: linux@horizon.com.
Thread: https://gitlist.dev/t/8825

## linux@horizon.com, 2007-07-03 03:05

Subject: Re: How to create & switch to a branch after changes have already been made?
Message-ID: <20070703030535.2958.qmail@science.horizon.com>
URL: https://gitlist.dev/e/20070703030535.2958.qmail%40science.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.

```
