'Create Git branch with current changes
I started working on my master branch thinking that my task would be easy. After a while I realized it would take more work and I want to do all this work in a new branch.
How can I create a new branch and take all these changes with me without dirtying master?
Solution 1:[1]
Like stated in this question: Git: Create a branch from unstagged/uncommited changes on master: stash is not necessary.
Just use:
git checkout -b feature/newbranch
Any uncommitted work will be taken along to the new branch.
If you try to push you will get the following message
fatal: The current branch feature/newbranch has no upstream branch. To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/newbranch
Just do as suggested to create the branch remotely:
git push --set-upstream origin feature/newbranch
Solution 2:[2]
Follow these steps:
Create a new branch:
git branch newfeatureCheckout new branch: (this will not reset your work.)
git checkout newfeatureNow commit your work on this new branch:
git commit -s
Using above steps will keep your original branch clean
and you dont have to do any 'git reset --hard'.
P.S. -s parameter for commit is for --signoff
Solution 3:[3]
Since you haven't made any commits yet, you can save all your changes to the stash, create and switch to a new branch, then pop those changes back into your working tree:
git stash # save local modifications to new stash
git checkout -b topic/newbranch
git stash pop # apply stash and remove it from the stash list
Solution 4:[4]
To add new changes to a new branch and push to remote:
git branch branch/name
git checkout branch/name
git push origin branch/name
Often times I forget to add the origin part to push and get confused why I don't see the new branch/commit in bitbucket
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paul Verest |
| Solution 2 | Paul Verest |
| Solution 3 | Six |
| Solution 4 | Patrick Schaefer |
