8

I understand that a command to start a new branch is

git checkout -b <mybranch>

However, that assumes I had the foresight to have created the new branch before making the new changes that I want to commit.

Usually however that's not the case. Usually, I start coding, then may or may not do git add . then want to commit in a new branch. How do I do that?

3 Answers 3

7

Do it anwyay:

git checkout -b <new_branch>

What you'll see is something that looks like this:

git checkout -b new_branch
M   <changed_file>
Switched to a new branch 'new_branch'

You haven't committed anything into Git; with git add, you've only moved the files into staging. You can still create a new branch with your staged work. Any changes that aren't staged yet will have the opportunity to become staged on the new branch.

3
  • Confused. Thought checkout checks out code from git? As in, overriding the file tree? Jun 14, 2015 at 19:49
  • 2
    If you're doing a straight-up checkout of an existing branch, yes. If you're creating a new branch, this isn't the case; both the reference of your new branch and where you were doing the work are still the same.
    – Makoto
    Jun 14, 2015 at 19:50
  • 3
    @ТаняТ. Note that Git will usually never let you do a checkout that results in you losing uncommitted changes. When Git notices uncommitted changes that would be overwritten by a checkout, it will stop, and ask you to do something about it (e.g. commit them, undo them). Checking out a new branch that points to the same commit as the current branch does nothing other than changing the currently checked out branch; it will not touch your working directory at all.
    – poke
    Jun 14, 2015 at 20:42
2

What's important here: Git branches do not contain any files or revisions by themselves. A branch is just a pointer to some commit. When you create a new branch, it takes the commit to point at from the current branch. Both will always (well, almost always) share common history, but may diverge after creation.

Branches bring in some utility features:

  • You can checkout a branch by its name (but you can also checkout a commit by its sha1, and in many other ways)
  • Say, a branch points to commit A. When you run git commit in that branch, a new commit B is created, which points to A. And the branch now points to B, the new recent commit.
  • Branches make it easier to visualize development paths and treat them as abstract objects.
  • Branches make it easier to merge revisions. (While it's also possible to merge raw commits).

To understand them better, I recommend you this course, which I'd taken myself. (Shame on me for recommending tutorials on SO. :)

1

Go ahead and create the new branch, which essentially 'saves' what is on your current branch to the new branch.

Switch back to the previous branch and 'undo' your recent actions to restore that first branch to where you wanted it to be if you started the new branch at an ideal place.

(If it's only a few things - command z, or maybe go back to the prior commit, or re-pull from the last version on Github.) Then git add and commit that first branch, and you can switch back to the second to continue working.

Maybe not ideal, but I've used it as a work-around.

2
  • Seems wrong to me. Since the new changes were not commited, there is nothing to undo on the previous branch. Once git checkout -b newname , git add <filenames>, and git commit are done, the commit goes to the new branch. When you checkout the previous branch, there shouldn't be anything to undo. Jun 14, 2015 at 20:24
  • But in order to switch to the new branch, git will make you add and commit the first branch. They will be identical content at first. If you want to just work forward on the second branch from there, that's fine. But if you were experimenting and wanted to restore the first branch to a previous working spot, you could do undo and re-add and re-commit. I could be wrong - I'm very new at this. Just trying to offer what I experienced.
    – tpeters0
    Jun 14, 2015 at 20:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.