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

后端 未结 2 1531
一向
一向 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: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
    

提交回复
热议问题