271

I've been editing some modules on the master branch but I haven't committed them. I'm now thinking that these changes should really be on an experimental branch and not the master branch.

How can I get these edits into an experimental branch? Copy them to some temp location, create the branch, and then copy them back in?

And how do I configure things so that when I do a git push from the new experimental branch it will it go into a branch of the same name in my GitHub repo?

random
  • 9,774
  • 10
  • 66
  • 83
Noam
  • 3,333
  • 3
  • 18
  • 8

1 Answers1

436

You can simply check out a new branch, and then commit:

git checkout -b my_new_branch
git commit

Checking out the new branch will not discard your changes.

Community
  • 1
  • 1
Antoine Pelisse
  • 12,871
  • 4
  • 34
  • 34
  • 2
    @antoine-pelisse what would happen if I did not include the `-b` flag and/or if I switched to an existing branch where I wanted to continue working with those changes? – Web User Jun 13 '16 at 23:15
  • Changes are not there when you go back and checkout "master" -- nice -- exactly what I would want. – wcochran Jul 06 '16 at 23:48
  • 1
    @WebUser - `git checkout my-branch` refuses to do the checkout if it would overwrite uncommitted changes. To start working with your existing changes on a different branch, you can do `git stash`, then `git checkout my-branch` and then either `git stash pop` (removes the stash if it applies without conflicts) or `git stash apply` (does not remove the stash even if it applies without conflicts). – MikeBeaton Jan 20 '20 at 10:30
  • To push the branch to upstream, you need to do git push --set-upstream origin – sachin May 08 '20 at 18:37