Reputation: 610
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
Reputation: 8951
For newer versions of Git i.e version 2.23 onwards, you can use the switch command
Stash current changes git stash
Go to your main or master git checkout main
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
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:
master
branch remains unchangedA
remains unchangedB
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:
git branch newBranch master
to create a new branch off of master
branch.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.git merge A
to merge the committed changes from branch A
to the new branch.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
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
Reputation: 94
git stash
git checkout master
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.
git merge <initialbranch>
git stash pop
Upvotes: 6