Vipul
Vipul

Reputation: 610

Create a new branch

I want to create new branch, 'B'. Currently, I have a master branch (local and remote) and feature branch 'A' (local).

Feature branch(A) is deleted in remote. Also, I have some files committed and unstaged files in my local feature branch. I want to go to master without losing any of changes and create another branch, commit the branch first, and then commit my new changes. How can I do it?

Upvotes: 6

Views: 68809

Answers (4)

Mwiza
Mwiza

Reputation: 8951

For newer versions of Git i.e version 2.23 onwards, you can use the switch command

  1. Stash current changes git stash

  2. Go to your main or master git checkout main

  3. Create new branch and switch to it git switch -c new-branch-name

Alternative to 3. To simply create a new branch without switching: git branch new-branch-name

Upvotes: 1

alon-k
alon-k

Reputation: 438

In some places you wrote that you had untracked changes, and in some that you had unstaged changes. These are two different things and should be handled differently. If you have tracked, but unstaged changes, you should git stash your changes before checking out the new branch, and git stash pop them after merging A into the new branch.

For untracked changes, another way to ensure that they are also stashed and then just apply them to the new branch is to use git stash with the flag -u, which also stashes untracked changes. The flow in that case would be:

git branch newBranch master
git stash -u
git checkout newBranch
git merge A
git stash pop
git add && git commit

I believe the state you are trying to arrive at is the following:

  1. master branch remains unchanged
  2. feature branch A remains unchanged
  3. There is a new branch B branching off of master, which contains both the committed and untracked changes from branch A

First, note: When you use git checkout to checkout a different branch or commit, or when you execute commands that manipulate other branches, whatever was committed on the branch / commit you previously had checked out is not lost, and you can go back to it at any time using git checkout again. Additionally, untracked files will not be modified unless they are tracked in the commit / branch you are checking out.

Now, I would use the following commands:

  1. git branch newBranch master to create a new branch off of master branch.
  2. git checkout newBranch to checkout the newly created branch. Remember that untracked files wont be modified unless they are tracked on the new branch, which from your description I understand they are not. Also remember that whatever was committed on branch A will stay there and will not be lost by switching to another branch.
  3. git merge A to merge the committed changes from branch A to the new branch.
  4. git add and git commit to add the untracked changes and commit them to the new branch.

Finally, you can either keep working on the new branch or if you want to go back to branch A, you can execute git checkout A to go back. Note that since the changes which were untracked on branch A are now tracked on branch B, if you change them again and checkout B you will get the version before the new changes from A.

Upvotes: 7

John Pavek
John Pavek

Reputation: 2665

The simple answer is to use the following to create a new branch from master without switching.

git branch newBranch master

git branch accepts a second argument of the source branch.

Alternatively, you can use git stash or more thoroughly git stash save "name description" to save your code in a pseudo commit. This does not handle untracked files.

Upvotes: 2

Udo
Udo

Reputation: 94

  1. Stash current changes git stash
  2. Checkout master git checkout master
  3. Create new branch git checkout -b <newbranchname>

At this point I am slightly confused about where you want to commit your current branch. I am assuming that you are trying to commit it to the new branch you created in #3.

  1. Merge changes from initial branch onto new branch git merge <initialbranch>
  2. Retrieve stored changes from stash git stash pop
  3. Add your local changes and commit

Upvotes: 6

Related Questions