7

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?

6
  • You can use git stash to stash your changes and keep them in your memory, change your branch to master, create another branch from there, push that branch and do git stash pop to get your stashed changes back. Commented Sep 20, 2019 at 16:22
  • Which branch are you currently on? Which branch do you want 'B' to branch off of? Commented Sep 20, 2019 at 16:23
  • @OJ7: Currently I am on Feature Branch(A) which is deleted in remote. This feature branch has committed and untracked file changes. I do not want to loose these changes. Commented Sep 20, 2019 at 16:27
  • @NicolaeMaties: git stash, does it stash committed changes too? Commented Sep 20, 2019 at 16:31
  • It doesn't, only uncommited changes. Commented Sep 20, 2019 at 16:34

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I did what you said. While merging A branch from new_branch , I got fatal error: saying, refusing to merge unrelated histories.
That should not happen as long as branch A was originally created off of master branch.. Can you copy the output of git log A here? Are you sure you are merging 'A' into 'newBranch' and not the other way around?
Also, can you clarify whether we are indeed talking about unstaged changes or untracked changes?
I already took back up of branch 'A', so after getting fatal error, I copied back up files of branch 'A' to new branch and added, committed to new branch. Easy, but this is first time dealing with such issues. Thanks @alon-k and all who helped out.
I see. Doing it manually is one way to go but is generally not what you should do (otherwise, why use git / version control in the first place?). Also, if A came from another branch that came from master, the merge still should have worked... I guess another way you could have done it without merging is by using git cherry-pick (documentation) to apply the commits you wanted to the new branch. A third way is checking out a new branch from A, committing what you wanted, and then rebasing the new branch onto master.
|
6
  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

2 Comments

master branch is empty. If I check out empty master branch, doesn't it wipe out my current feature branch(A) committed and untracked changes?
I do not want to push my current branch to remote. I want those changes and local changes in a new branch from master.
2

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.

3 Comments

I am on feature branch(A) which is in my local repo and deleted on remote. I am a bit scared to go to master branch, because master branch is empty. My current feature branch(A) has committed and untracked file changes. I do not want to loose those changes.
1. Commit your work; If your commit doesn't build, prefix it with WIP: so others know. 2. You should never be afraid of your repo. Untrack changes are only modified if they are tracked on a different branch. 3. Using git branch newbranch sourcebranch does not change your current working branch, it just creates another one for you to use later based on the source branch.
I want to create new branch out of master(which is empty) with Feature branch( A ) committed and untracked files. Currently Master -> Feature Branch(A in local) . I want Master -> New Branch( include feature branch local repo committed files and untracked files.)
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.