3

I've done some changes to my code in branch1, but now I need to move those changes to another branch. Graphically, I have:

master
  |___develop
         |___branch1

and I need to move all the uncommitted changes and untracked files to a new branch branch2 (which does not exist):

master
  |___develop
         |___branch1
         |___branch2

after which I'll just delete branch1 since all its changes are already pushed, and be left with all the uncommitted changes and untracked files in branch2:

master
  |___develop
         |___branch2

There are quite a few of similar questions in SO but I'm still not sure how I should proceed in my case. Some say to use git stash (How do I merge my local uncommitted changes into another Git branch?, moving changed files to another branch for check-in) which in my case I guess would be:

git stash -u
git checkout develop
git checkout -b branch2
git stash pop

but other answers (Put current changes in a new Git branch, Moving uncommitted changes to a new branch) say to simply use checkout as:

git checkout -b branch2

but I'm not sure if this will carry my untracked files too. There's a newer answer that recommends using switch (Move existing, uncommitted work to a new branch in Git), but my git version 2.17.1 does not have this command.

Any help will be much appreciated.

Gabriel
  • 37,092
  • 64
  • 207
  • 369
  • I'm pretty sure your piece of commands describing the first method you mentioned (git stash -u etc.) will work. Because -u flag is actually --include-untracked, which is what you need. – Itai Klapholtz Jan 27 '21 at 16:27
  • `switch` doesn't really do anything different than `checkout`; rather, it's a more accurate description of a subset of `checkout`'s functionality. `restore` is the other new command that will do whatever `checkout` does that `switch` doesn't. Between `switch` and `restore`, there shouldn't be any need to use `checkout`, and it can be retired. – chepner Jan 27 '21 at 18:02

2 Answers2

3

Using stash is safer, as it avoids any problems that might pop up with switching branches with a dirty working directory. stash even has a subcommand for directly creating a branch from a stash.

git stash -u
git checkout develop
git stash branch branch2
chepner
  • 446,329
  • 63
  • 468
  • 610
  • `git stash branch` creates the new branch using the stash's parent commit, which means your `git checkout develop` step here has no effect. – torek Jan 28 '21 at 01:43
2

You can simply do

git checkout -b branch2

this will carry along all your changes. After all creating the branch is not much more than "labelling" the current commit with the name branch2.

michid
  • 9,409
  • 3
  • 29
  • 52
  • Will this carry the untracked files too? What is the difference with `stash` that involves more steps? – Gabriel Jan 27 '21 at 19:52
  • Yes it will carry untracked files. The difference to the solution involving `stash` is what @chepner mentions in his answer: problems that might pop up with switching branches with a dirty working directory. – michid Jan 27 '21 at 20:08