How can you put uncommitted changes to a branch TEST when I am at the branch master
?

- 11,977
- 56
- 49
- 78

- 134,464
- 179
- 445
- 697
-
2See also: http://stackoverflow.com/questions/2944469/how-to-commit-my-current-changes-to-a-different-branch-in-git – Jouni K. Seppänen Nov 04 '10 at 13:59
4 Answers
Also you can create a new branch and switch to it by doing:
git checkout -b new_branch
git add .
I use this all the time because I always forget to start a new branch before I start editing code.

- 12,317
- 7
- 45
- 62
-
3same issue as @jouni noted for the other answer - you can run into difficulty merging the branch back into master if additional changes conflict with original changes. IMO this thread answers the question better: http://stackoverflow.com/questions/556923/git-how-to-merge-my-local-working-changes-into-another-branch – jpw Apr 20 '13 at 18:58
-
-
1Don't forget to do commit in the new_branch. If you switch back to the master branch and revert changed files, you will lose them in the new_branch too. – petrsyn Mar 25 '14 at 22:48
You can just checkout to the test branch and then commit. You don't lose your uncommited changes when moving to another branch.
Supposing you are at the master branch:
git checkout test
git add .
git add deletedFile1
git add deletedFile2
...
git commit -m "My Custom Message"
I am not really sure about the deleted files, but I guess they aren't included when you use git add .

- 12,317
- 7
- 45
- 62

- 17,449
- 12
- 49
- 59
-
12Sometimes the checkout will fail because your changes conflict with that branch. You can try checkout -m to merge. – Jouni K. Seppänen Nov 04 '10 at 14:00
-
2I tried this but I got an error : error: Your local changes to the following files would be overwritten by checkout. Please, commit your changes or stash them before you can switch branches. – ishwr Dec 19 '13 at 11:16
-
While this will work, the answer which says use stash should be preferred, IMO. Maybe just personal choice, but it is a cleaner workflow, logically and introduces STASH which is a useful command. – Patrick Mar 28 '16 at 22:45
-
The option "-b" is missing, as the headline suggests that the question is concerning a "new" branch. – Guntram May 09 '17 at 11:20
Why not just use git stash. I think it's more intuitive like a copy-and-paste.
$ git branch
develop
* master
feature1
TEST
$
You have some files in your current branch that you want to move.
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: awesome.py
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: linez.py
#
$
$ git stash
Saved working directory and index state \
"WIP on master: 934beef added the index file"
HEAD is now at 934beef added the index file
(To restore them type "git stash apply")
$
$ git status
# On branch master
nothing to commit (working directory clean)
$
$
$ git stash list
stash@{0}: WIP on master: 934beef ...great changes
$
Move to the other branch.
$ git checkout TEST
And apply
$ git stash apply
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: awesome.py
# modified: linez.py
#
I also like git stash
because I use git flow
, which complains when you want to finish a feature branch whilst having changes still in your working directory.
Just like @Mike Bethany, this happens to me all the time because I work on a new problem while forgetting I am still on another branch. So you can stash your work, git flow feature finish...
, and git stash apply
to new git flow feature start ...
branch.

- 21,241
- 6
- 33
- 41
-
2`git stash` is my preferred way of dealing with uncommitted changes. It's certainly an intuitive method when you think of it as cut and paste. – Matthew Mitchell Jan 22 '13 at 13:41
-
This seems a good approach to me. It worked without a problem. – Yunus Nedim Mehel May 01 '14 at 09:04
-
Didn't know you could do this and it worked nicely. Feels a bit more intuitive than the other methods. – glaucon Aug 14 '14 at 04:55
-
I think stashing it's more professional way, instead of using git checkout and then add. I think your answer should be 100+ votes. – Matrosov Oleksandr Mar 20 '15 at 22:12
-
-
1
-
In which branch stash has been applied? test or master? It seems checked out to test branch but showing master? – Mayuresh Srivastava Jul 13 '17 at 11:06