this happens to every developer time-to-time. You start coding a new feature and forget to branch first locally.
So if that happens is there a way I can say hey, transf
If you haven't commited your changes yet, you can switch to a new branch prior to committing.
git checkout -b my-new-branch
This will create a new branch called my-new-branch
and immediately check it out.
Unless you have done any changes that should not enter the feature branch after you have started work on the feature, it's as simple as creating the feature branch and rewinding the erroneously advanced master branch to the last commit that should not be part of the feature branch:
MA --- MB --- MC --- FA --- FB --- FC <- master
git checkout -b feature
MA --- MB --- MC --- FA --- FB --- FC <- feature
^
|
master
git branch -f master MC
MA --- MB --- MC --- FA --- FB --- FC <- feature
^
|
master
If you have actually mixed your commits, you have a much larger problem. In that case, you need to perform a rebase to unmix the commits, so that you can proceed with the steps above:
MA --- FA --- MB --- MC --- FB --- FC <- master
git rebase -i MA
#reorder the lines by moving the commit FA down after MC
MA --- MB' --- MC' --- FA' --- FB' --- FC' <- master
#proceed as above
Be aware that you are rewriting history with this: If you have already published FA
, you are in trouble. In that case, others will notice that you screwed up one way or the other, and the correct solution will include significant amounts of communication between humans.
Follow these steps:
Create a new branch:
git branch newfeature
Checkout new branch: (this will not reset your work.)
git checkout newfeature
Now commit your work on this new branch:
git commit -s
In the following case you started off with "Hello world" at the very beginning and after a commit your head was at C4, now, you stayed on master and made commits till C0 but forgot to create different branch at C4 itself
Now in this case what you can do is
-- Make a patch of commits from C3 to C0 so that you would have changes made in respective commits saved in files like C3.patch....C0.patch in the directory of your repo itself
-- Reset your HEAD to C4
-- And checkout all the unstaged changes so that you can create a new branch
-- And apply those changes back on the newly created branch
Basically you do
-- git format-patch HEAD ~ {n} (n is 4 in this case)
-- git reset HEAD~{n} (reached master or parent branch)
-- git checkout -- .
-- git checkout -b <new-branch-name>
-- git am (or git am *.patch) (which is git apply -r C*.patch in this case)
-- If they are staged, unstage by resetting head to the previous commit where you started of with making these changes
-- Make a patch of these changes so that you have them stored on local repo directory
-- Checkout those changes and make a new branch
-- Apply that created patch to have those changes back on a new branch
Basically you do
-- git reset HEAD~1
-- git diff > unstaged_changes.patch
-- git branch -b <new-branch-name>
-- git am unstaged_changes.patch
Would it not work to stash your changes, create a new branch and then reapply your stash to the new branch?
Then there is no harm in switching to a new branch after files have been modified. Edited, non-committed files are always viewed in the context of whatever branch is checked out:
git checkout -b mytopicbranch
Following the description: here:
git branch mytopicbranch
It now has all the commits that you wanted to make.
git reset abc5b0de1 --hard
Assuming abc5b0de1
is the fingerprint of the commit right before you made the accidental commits.
Now you can switch back to mytopicbranch
as needed.