Git create branch from current checked out master?
GitVersion ControlDvcsGit BranchGit Problem Overview
There is a git controlled folder on a server where the main branch is checked out and a whole pile of files have been modified and not committed. Is there a way for me to commit the changes to a separate branch so I can go back to a clean version?
ie I want to effecitvely undo all this persons changes but store them in another chance so if that person wants their changes they can switch to that branch.
(Yes I know this is not how git is designed to work but that is my situation!) Any ideas very much appreciated.
Git Solutions
Solution 1 - Git
First of all moving to a different branch based in the current HEAD is performed like this:
git checkout -b newbranch
Commit all the changes (assuming no newly added files, otherwise git add
them):
git commit -a
Go back to the master branch:
git checkout master
The previously uncommitted changes will all be on the newbranch branch, and master will still be at the state it was without those changes.
Solution 2 - Git
You can always stash your changes.
git stash
git checkout -b bravenewmaster
git stash apply
Also keep in mind, that if you commit to the "wrong" branch you can always move that branch back, because branch is nothing but a pointer to a commit.
Solution 3 - Git
This method is useful:
git checkout -B <new_branch> <start point>
Where:
<new_branch>
is your new branch (e.g.my_branch
)<start point>
is your starting branch (master
in your case)-B
creates new branch starting from<start point>
, if it already exists, then reset it to(it won't fail as -b
when branch already exists)- sometimes
-m
can useful to specify when switching branches, this will perform a three-way merge between the current branch, your working tree contents (useful for scripting).
See: man git-checkout
for more details.