How to remedy for accidently making a commit into the master branch instead of my feature branch

后端 未结 2 1505
一向
一向 2021-01-28 08:57

Locally, I forgot to create a feature branch from the master branch, and then made a commit of my changes into the master branch.

How can i correct the mistake, so that

相关标签:
2条回答
  • 2021-01-28 09:11
    git checkout master
    git checkout -b my-feature-branch
    git checkout master
    git reset --hard HEAD~1
    git checkout my-feature-branch
    

    Explanation:

    1. First we make sure we are on the master branch.
    2. Now we create a new feature branch, bringing along the latest commit.
    3. We move back to the master branch.
    4. We reset the master branch to the commit before the last one.
    5. We go back to the feature branch to continue working on it.
    0 讨论(0)
  • 2021-01-28 09:27

    Let's consider the situation

    before committing

    ... -- A
           ^
           |
           master
    

    after commit

    ... -- A -- B
                ^
                |
                master
    

    To resolve situation, you need to perform the following steps:

    method 1

    1. rename "master" branch to "feature" branch with git branch -m feature
    ... -- A -- B
                ^
                |
                feature
    
    1. recreate "master" from the previous commit with git checkout -b master A
    ... -- A -- B
           ^    ^
           |    |
           |    feature
           |
           master
    

    method 2

    1. create new branch with git checkout -b feature
    ... -- A -- B
                ^
                |
                feature *
                master
    
    1. return to master branch with git checkout master
    ... -- A -- B
                ^
                |
                feature
                master *
    
    1. reset changes in master with git reset --hard HEAD~1
    ... -- A -- B
           ^    ^
           |    |
           |    feature
           |
           master
    
    0 讨论(0)
提交回复
热议问题