Forgot to create new branch. How to transfer changes to new branch

前端 未结 6 1965
闹比i
闹比i 2021-02-19 13:45

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

相关标签:
6条回答
  • 2021-02-19 13:48

    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.

    0 讨论(0)
  • 2021-02-19 13:56

    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.

    0 讨论(0)
  • 2021-02-19 13:57

    Follow these steps:

    1. Create a new branch:

      git branch newfeature
      
    2. Checkout new branch: (this will not reset your work.)

      git checkout newfeature
      
    3. Now commit your work on this new branch:

      git commit -s
      
    0 讨论(0)
  • 2021-02-19 14:05
    1. In case you have committed the changes already!

    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)
    
    1. 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
    
    0 讨论(0)
  • 2021-02-19 14:07

    Would it not work to stash your changes, create a new branch and then reapply your stash to the new branch?

    0 讨论(0)
  • 2021-02-19 14:08

    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 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:

    1. 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.

    1. 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.

    0 讨论(0)
提交回复
热议问题