4030

I started some work on a new feature and after coding for a bit, I decided this feature should be on its own branch.

How do I move the existing uncommitted changes to a new branch and reset my current one?

I want to reset my current branch while preserving existing work on the new feature.

1

14 Answers 14

4724

Update 2020 / Git 2.23

Git 2.23 adds the new switch subcommand in an attempt to clear some of the confusion that comes from the overloaded usage of checkout (switching branches, restoring files, detaching HEAD, etc.)

Starting with this version of Git, replace the checkout command with:

git switch -c <new-branch> 

The behavior is identical and remains unchanged.


Before Update 2020 / Git 2.23

Use the following:

git checkout -b <new-branch> 

This will leave your current branch as it is, create and checkout a new branch and keep all your changes. You can then stage changes in files to commit with:

git add <files> 

and commit to your new branch with:

git commit -m "<Brief description of this commit>" 

The changes in the working directory and changes staged in index do not belong to any branch yet. This changes the branch where those modifications would end in.

You don't reset your original branch, it stays as it is. The last commit on <old-branch> will still be the same. Therefore you checkout -b and then commit.

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

34 Comments

Just to make sure, I need to commit the unfinished feature BEFORE I reset my original branch? Or will those uncommitted files be preserved regardless of committing?
FYI: changes in working directory and changes staged in index do not belong to a branch. git checkout -b <new branch> changes where those changes would end in.
If you already have a branch and want to move your changes to the existing branch, checkout stackoverflow.com/questions/556923/…
If you want to push your new branch to the remote repository: stackoverflow.com/questions/2765421/…
@JDSmith: uncommit changes do NOT belong to any branch. They only reside in the working directory git checkout ./git reset --hard will unrecoverably remove them
|
429

Alternatively:

  1. Save current changes to a temp stash:

    $ git stash

  2. Create a new branch based on this stash, and switch to the new branch:

    $ git stash branch <new-branch> stash@{0}

Tip: use tab key to reduce typing the stash name.

18 Comments

If the other branch already exists, you can just switch to it with checkout, then git stash apply.
I don't understand the tip "Tip: use tab key to reduce typing the stash name.". Isn't "stash@{0}" the name? I cannot run it successfully.
Why is this better than the accepted answer stackoverflow.com/a/1394804/754997 ?
I don't understand why this is better then the accepted answer of git checkout -b <new branch name>
You don't need to git add -A before stashing.
|
67

If you have been making commits on your main branch while you coded, but you now want to move those commits to a different branch, this is a quick way:

  1. Copy your current history onto a new branch, bringing along any uncommitted changes too:

     git checkout -b <new-feature-branch> 
  2. Now force the original "messy" branch to roll back: (without switching to it)

     git branch -f <previous-branch> <earlier-commit-id> 

    For example:

     git branch -f master origin/master 

    or if you had made 4 commits:

     git branch -f master HEAD~4 

Warning: git branch -f master origin/master will reset the tracking information for that branch. So if you have configured your master branch to push to somewhere other than origin/master then that configuration will be lost.

Warning: If you rebase after branching, there is a danger that some commits may be lost, which is described here. The only way to avoid that is to create a new history using cherry-pick. That link describes the safest fool-proof method, although less convenient. (If you have uncommitted changes, you may need to git stash at the start and git stash pop at the end.)

1 Comment

This answers a question which is slightly different from what the op asked. I decided to put this answer here because this is where Google brought me when I was searching for an answer. The actual question that deals with this situation is here.
63

The common scenario is the following: I forgot to create the new branch for the new feature, and was doing all the work in the old feature branch. I have commited all the "old" work to the master branch, and I want my new branch to grow from the "master". I have not made a single commit of my new work. Here is the branch structure: "master"->"Old_feature"

git stash git checkout master git checkout -b "New_branch" git stash apply 

2 Comments

"git switch -c <new-branch>" cleared the changes of unstaged files (with only new files retains); stash way is better if you're thinking to have exactly all local files back
That was not my experience -- as others have written above, your local unstaged changes are retained in the new branch. (I must admit that I had to use checkout -b not switch -c, but those two are supposed to be identical)
23

If you commit it, you could also cherry-pick the single commit ID. I do this often when I start work in master, and then want to create a local branch before I push up to my origin/.

git cherry-pick <commitID> 

There is alot you can do with cherry-pick, as described here, but this could be a use-case for you.

2 Comments

Nicer solution for moving partial changes to a new branch... since you can commit what you want for now, stash all other changes, check out the branch you want to branch from, cherry-pick that commit onto the new branch, go back to the original branch, hard reset back a commit, then do a stash pop, add, commit, and sing hallelujah.
@Meredith, haha, ya something like that. This is great, unless you plan your changes ahead...and who does that ;)
10

Option 1 (with an existing branch)

git stash (from main/any-branch) git checkout your-existing-branch git stash apply 

Option 2 (creating a new branch)

git switch -c your-new-branch 

Comments

8

For those using Visual Studio Community 2022 (and possibly earlier versions) when you have uncommitted changes then create a new branch, you'll see a dialog like this:

enter image description here

Just select the first option Bring the changes to '[your-new-branch-name]' and click Continue checkout. The new branch will be created and you can proceed to commit your changes there.

Comments

5

There is actually a really easy way to do this with GitHub Desktop now that I don't believe was a feature before.

All you need to do is switch to the new branch in GitHub Desktop, and it will prompt you to leave your changes on the current branch (which will be stashed), or to bring your changes with you to the new branch. Just choose the second option, to bring the changes to the new branch. You can then commit as usual.

GitHub Desktop

Comments

4

This may be helpful for all using tools for GIT

Command

Switch branch - it will move your changes to new-branch. Then you can commit changes.

 $ git checkout -b <new-branch> 

TortoiseGIT

Right-click on your repository and then use TortoiseGit->Switch/Checkout

enter image description here enter image description here

SourceTree

Use the "Checkout" button to switch branch. You will see the "checkout" button at the top after clicking on a branch. Changes from the current branch will be applied automatically. Then you can commit them.

enter image description here

Comments

2

This is the only answer that tells you to use git stash -k, which you will need...

if you already spent an hour with git add -p

and then decided you want to test what you added to the index before doing the actual commit. In that case do not use plain git stash!

Instead do:

git stash -k 

That keeps the index and removes the rest that is still in the working directory and wasn't added to the index yet. Exactly what you want.

Now you can try to compile/test and commit. I.e.

make git commit -m 'Yay!' 

Then get back the uncommitted changes with

git stash pop 

If you discover that it does NOT compile however, then making changes and adding those also the index and committing that might confuse git stash pop. It isn't that good when it comes to merging. In that case you probably should just commit anyway; thus:

make git commit -m 'Grrrr' 

Then create a new branch,

git switch -c tmpbranch 

do your work there (changing code, doing testing and more commits)

/* blood sweat and tears */ 

Once everything works commit it to the new branch

commit -a -m 'Finally!' 

go back to the old branch and then do the git stash pop with the same working directory as where you was when you pushed to the stash.

git checkout youknowwhatbranchyouwereonright git stash pop 

Commit that too, otherwise you can't merge the tmpbranch. Then merge the temporary branch that you created.

git commit -a -m 'Still working on this.' git merge tmpbranch /* fix collisions and commit */ 

Now you can do a rebase to put the 'Still working on this' at the top and squash/fixup the rest into a single comment. For example

git rebase -i 

might give you:

pick 540623a Grrr pick a8589d3 Still working on this. pick d3b602c Finally 

Then change that to:

reword 540623a Grrr fixup d3b602c Finally pick a8589d3 Still working on this. 

And finally undo the last commit (the 'Still working on this')

git reset HEAD~1 

Comments

2

For me, moving "uncommitted existing work" to a new branch that does not yet exist (the original question) required moving both changes not staged for commit as well as untracked files. The following worked for me:

git stash -u git switch -c <new-branch> git stash pop 

Where git stash is used to move the changes, with the '-u' flag (include untracked) moving changes that are staged for commit, as well as those that are not.

Where git switch -c creates a new branch and switches to it (as described in other answers).

And, where git stash pop then applies the stashed changes to the new branch.

Comments

1

I used @Robin answer & listing all that I did,

git status <-- review/list uncommitted changes git stash <-- stash uncommitted changes git stash branch <new-branch> stash@{1} <-- create a branch from stash git add . <-- add local changes git status <-- review the status; ready to commit git commit -m "local changes ..." <-- commit the changes git branch --list <-- see list of branches incl the one created above git status <-- nothing to commit, working tree (new-branch) is clean git checkout <old-branch> <-- switch back 

! If the repo has more than one stash, see which one to apply to the new-branch:

git stash list stash@{0}: WIP on ... stash@{1}: WIP on ... 

and inspect the individual stash by,

git stash show stash@{1} 

Or inspect all stashes at once:

git stash list -p 

Comments

1

3 Steps to Commit your changes

Suppose you have created a new branch on GitHub with the name feature-branch.

enter image description here

FETCH

 git pull --all Pull all remote branches git branch -a List all branches now 

Checkout and switch to the feature-branch directory. You can simply copy the branch name from the output of branch -a command above

git checkout -b feature-branch

VALIDATE

Next use the git branch command to see the current branch. It will show feature-branch with * In front of it

git branch 

COMMIT

git add . add all files git commit -m "Rafactore code or use your message" 

Take update and the push changes on the origin server

 git pull origin feature-branch git push origin feature-branch 

Comments

-1

You can also create bash aliases to do all of this.

This will create new commands...

  • gitco <branch> - checks out the specified branch, taking your current changes with you
  • gitconew <new branch name> - creates a new branch with the specified name (branched off of master) taking your current changes with you

Here are the steps to set up the aliases...

  1. Add the following to ~/.bash_profile
gitco () { git stash && git checkout $1 && git stash apply } gitconew () { git stash && git checkout master && git checkout -b $1 && git stash apply } 
  1. Run source ~/.bash_profile to reload the profile

Now you can run the gitco and gitconew alias commands.

Here is some more info on bash aliases -> https://linuxize.com/post/how-to-create-bash-aliases/

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.