Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - working on wrong branch - how to copy changes to existing topic branch

Tags:

git

I've been working on a project, but unfortunately, I forgot to switch to my branch, and as such have been working on master

How can I copy the work (3 files) I've done here from master, to my branch (called, for example branch123) without comitting to master?

like image 706
Alex Avatar asked May 11 '11 12:05

Alex


2 Answers

Sounds like all you need is the following:

git stash git checkout branch123 git stash apply 

Then you should be back on your own branch without touching the master branch.

like image 106
gnab Avatar answered Sep 26 '22 15:09

gnab


The accepted answer is the most thorough, but there is a special case where you can simplify. If the files you have modified in the working directory are identical in both master and branch123 you can simply do

git checkout branch123 

No need to stash anything, since the default behavior of checkout is to NOT overwrite modified files in your working directory, so you won't lose anything. (This was actually mentioned in the comments first by Cascabel)

As other people have mentioned in the comments, if branch123 doesn't exist yet, you can do

git checkout -b branch123 

Based on what I found here.

like image 25
Russel Dirks Avatar answered Sep 23 '22 15:09

Russel Dirks