ace
ace

Reputation: 12024

How to create a new branch when there are existing unstaged changes in git?

I have git repo with two branches:

master orange

While I was in orange branch, I made quite a few changes in files but they are not yet added and committed on orange. What I want to do is to create a new branch red in which I add and commit these changes as these changes must not be in orange branch. After that I should be able to easily switch to orange or red branch.

Upvotes: 0

Views: 63

Answers (1)

Schwern
Schwern

Reputation: 165145

You don't have to do anything special. Create the branch, check it out, then add and commit the unstaged changes.

  • git checkout -b red
  • git add .
  • git commit

Perhaps the confusion is the wrong idea that making a branch is a big deal, like it is in SVN. All making a new branch in Git does is put a new label onto the current commit. If you're on the orange branch and git branch red then orange and red will point to the same commit. Your working copy and staging area will remain the same.

After making red your repository will look like this.

                   * [red] [orange] [HEAD]
                   |
                   *
                   |
                   * [master]

After you checkout red, add and commit then red will advance to the new commit. orange will remain at the previous commit.

                   * [red] [HEAD]
                   |
                   * [orange]
                   |
                   *
                   |
                   * [master]

Upvotes: 2

Related Questions