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?
If you hadn’t made any commit yet, only (1: branch) and (3: checkout) would be enough.
Or, in one command:
git checkout -b newBranchWith Git 2.23+ (Q3 2019), the new command
git switchwould create the branch in one line (with the same kind ofreset --hard, so beware of its effect):Or, as suggested in Alia‘s answer, use
git switch -m, withoutgit stash:As mentioned in the
git resetman page:master" branch. You want to continue polishing them in a topic branch, so create "topic/wip" branch off of the currentHEAD.masterbranch to get rid of those three commits.topic/wip" branch and keep working.Again: new way (since 2019 and Git2.23) to do all that in one command:
Note: due to the "destructive" effect of a
git reset --hardcommand (it does resets the index and working tree. Any changes to tracked files in the working tree since<commit>are discarded), I would rather go with:This would make sure I’m not losing any private file (not added to the index).
The
--softoption won’t touch the index file nor the working tree at all (but resets the head to<commit>, just like all modes do).