3387

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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164

9 Answers9

3946

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 above's 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.

boop
  • 6,033
  • 9
  • 37
  • 76
knittl
  • 197,664
  • 43
  • 269
  • 318
  • 16
    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? – Dane O'Connor Sep 08 '09 at 16:02
  • 200
    FYI: changes in working directory and changes staged in index do not belong to a branch. `git checkout -b ` changes where those changes would end in. – Jakub Narębski Sep 08 '09 at 17:00
  • 155
    If you already have a branch and want to move your changes to the existing branch, checkout http://stackoverflow.com/questions/556923/git-how-to-merge-my-local-working-changes-into-another-branch – Chirantan Jan 25 '11 at 08:41
  • 16
    If you want to push your new branch to the remote repository: http://stackoverflow.com/questions/2765421/how-to-push-a-new-local-branch-to-remote-repo-and-track-it-too – Dewayne Dec 13 '13 at 02:15
  • 2
    I dont know this basic fact.. "The changes in the working directory and changes staged in index do not belong to any branch yet". I was about to do this task manually. This answer is very handy! – Dinesh Apr 01 '14 at 06:09
  • 3
    @thedeeno, it would seem so. I just did `git checkout -b `, then without commiting, switch back to main branch and `git checkout -- ` and it blew away changes on both branches. :( No big deal, not many changes, but lesson learned. – Dave Nov 28 '14 at 21:25
  • 2
    It's worth noting that changes that have been **added** but not **committed** on the old branch will be moved painlessly to the new branch. – Fred Willmore Apr 22 '15 at 17:14
  • "This changes where those changes would end in." Can't understand this. Could anybody please explain? – Nick Volynkin Jul 24 '15 at 18:53
  • 1
    @NickVolynkin: checkout changes/switches the branch your edits will be committed to. – knittl Jul 26 '15 at 14:52
  • @knittl: Oh. Thank you, now I see it. ) "This changes the branch where those changes would end in". – Nick Volynkin Jul 26 '15 at 14:55
  • what if I get this `error: Your local changes to the following files would be overwritten by checkout:` when I try to switch branches? – maahd Oct 27 '15 at 15:26
  • @maahd: this means you have changes in files that have changed between branches. Git cannot merge these changes automatically, so you have to commit, stash, or remove them. – knittl Oct 27 '15 at 22:43
  • 4
    @DaneO'Connor, yes it appears you must commit the unfinished feature BEFORE you reset your original branch, otherwise the uncommitted changes will be wiped out in both branches when you do `git checkout -- .` or `git checkout .` I'm surprised nobody commented about this already. – JD Smith Nov 04 '15 at 21:47
  • 12
    @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 – knittl Nov 05 '15 at 06:33
  • will this answer work if I'm not working on master originally? i.e. I'm on one feature branch, realized it's the wrong one and want to move my work to another feature branch? – maahd Nov 17 '15 at 12:01
  • 1
    @maahd: you can then simply checkout the other branch (without the `-b` flag). If the files in question were not changed between the branches were not edited, it will work fine. However, if they were edited, git checkout will abort and you will have to stash/checkout/unstash. – knittl Nov 17 '15 at 12:43
  • i had a quick question, when i create a new branch, will all unstaged be still there as you said as well as the changes to be committed, or should I unstage them first? I have files staged to be added, but im not sure if i should unstage them, and then change branches, then stage everything – Fallenreaper Dec 09 '16 at 19:14
  • @Fallenreaper: all staged/unstaged/untracked files will keep their status. – knittl Dec 09 '16 at 20:42
  • What if I have already run `git add .`, how do I move the files to a new branch? – Razvan Zamfir May 03 '19 at 11:41
  • @RazvanZamfir exactly as written in the answer `git checkout -b new-branch-name` – knittl May 03 '19 at 11:51
  • What if the commit was/is _already_ made, on the master: `git commit -m "My New Feature"` and I want to move it (the commit with _all its files_) on a new branch `my_ new_feature_branch`? – Razvan Zamfir May 03 '19 at 11:59
  • 1
    @RazvanZamfir create a new branch (either `git branch new-branch` or `git checkout -b new-branch` to switch branches immediately). You will then have a new branch exactly at the commit you want. If you do not want to have the commit on the master branch, you have to move that branch back one commit, either using `git branch -f master master^` or `git checkout master && git reset (--hard) master^` (careful, `reset --hard` will delete all local changes irrevocably) – knittl May 03 '19 at 12:08
  • `Please commit your changes or stash them before you switch branches. Aborting` – ScottyBlades Feb 29 '20 at 22:51
  • @ScottyBlades is that a question or is it supposed to be a helpful comment? This message is only shown when you switch to a different existing branch (`git checkout branch`), not when creating a new branch on the current HEAD commit (`git checkout -b branch`). Note the `-b` switch – knittl Mar 01 '20 at 07:40
  • That’s the read when I attempted the steps provided without other steps. It’s my understanding that when using git checkout -b branch, not only is a new branch created, but current branch switches to the newly created branch. – ScottyBlades Mar 01 '20 at 23:24
  • @ScottyBlades, yes the new branch is created and then checked out. If you you enter the the command exactly as presented in the answer, it is not possible to get the error, because the _commit_ of HEAD remains unchanged. The only thing I can think of is if you tried to run `git checkout -b newbranch different_ref` – but that's *not* what my answer recommends. IF you executed my command as recommendend and still get the error (I doubt it), please let me know, I'd have to dig into it. Could you then provide a MWE? – knittl Mar 02 '20 at 05:55
  • seems like its worth moving the update 2020 to the top of the answer – Anupam Sep 08 '20 at 06:40
  • The latest git version in Ubuntu apt-get is 2.17.1. So, the switch command is still too bleeding edge to be the top answer, IMHO. – Sean Rasmussen Nov 05 '20 at 14:25
  • I used to use stash/unstash but this switch thing is really great – Pasha GR Apr 21 '21 at 06:33
358

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.

Robin Qiu
  • 4,781
  • 1
  • 18
  • 19
57

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.)

joeytwiddle
  • 24,338
  • 11
  • 107
  • 91
  • 6
    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](http://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git). – joeytwiddle Dec 28 '16 at 04:49
41

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
Alex Burov
  • 1,461
  • 1
  • 14
  • 12
  • "git switch -c " 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 – Bee Chow Apr 09 '21 at 02:44
20

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.

password
  • 451
  • 3
  • 11
  • 2
    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 Jan 22 '16 at 10:24
  • 1
    @Meredith, haha, ya something like that. This is great, unless you plan your changes ahead...and who does that ;) – password Jan 22 '16 at 17:10
2

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

Kristofer Doman
  • 319
  • 1
  • 4
  • 11
2

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

Przemek Struciński
  • 3,598
  • 1
  • 21
  • 17
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
NBhat
  • 133
  • 8
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
Hitesh Sahu
  • 31,496
  • 11
  • 150
  • 116