'Forgot to create new branch. How to transfer changes to new branch
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, transfer these uncommitted (or committed - yea I know those are both two scenarios which I'd like to cover with this) to a new branch for me locally so I don't have to back them out and copy my changes to a new branch to be able to carry on my way?
Solution 1:[1]
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.
Solution 2:[2]
If you have not yet committed:
Then there is no harm in switching to a new branch after files have been modified. Edited, non-committed files (whether staged for commit or not) are always viewed in the context of whatever branch is checked out:
git checkout -b mytopicbranch
If you've already committed:
Following the description: here:
- Create the branch you wished you had made (but don't switch to it):
git branch mytopicbranch
It now has all the commits that you wanted to make.
- Reset the master branch back to before these commits:
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.
Solution 3:[3]
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.
Solution 4:[4]
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
Solution 5:[5]
- In case you have committed the changes already!
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)
- In case where you have not committed changes
-- 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
Solution 6:[6]
Would it not work to stash your changes, create a new branch and then reapply your stash to the new branch?
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 | mason |
| Solution 2 | |
| Solution 3 | cmaster - reinstate monica |
| Solution 4 | Abhishek Aryan |
| Solution 5 | maow |
| Solution 6 | beginnercoder123 |
