Git 如何撤消提交,放入新分支,然后执行PR?

Git 如何撤消提交,放入新分支,然后执行PR?,git,Git,我有一个master分支,该分支仅受PR的push保护 我在大师上漫不经心地说: git add . git commit -m "bunch of changes" 但是我推到了分支,被拒绝了,因为分支受到了保护。 我如何回溯、保留我的更改以及进行PR git reset HEAD~ git checkout -b "name-of-new-branch" git -am "my fancy new commit" git push origin "name-of-new-branch"

我有一个
master
分支,该分支仅受PR的push保护

我在
大师
上漫不经心地说:

git add .
git commit -m "bunch of changes"
但是我推到了分支,被拒绝了,因为分支受到了保护。 我如何回溯、保留我的更改以及进行PR

git reset HEAD~
git checkout -b "name-of-new-branch"
git -am "my fancy new commit"
git push origin "name-of-new-branch"
重置头~将撤消上次提交。Checkout-b创建一个新分支并将其签出,然后您只需在那里添加和提交您的更改并推送

  • 撤消
    master
    分支的最后一次提交

    $ git reset --soft HEAD~1      # undo the last commit and keep the changes in working tree   
    
    $ git checkout -b feature      # create and checkout to new 'feature' branch
    $ git add -A                   # add the changes 
    $ git commit -m 'message'      # commit 
    $ git push origin HEAD         # push to remote 'feature' branch
    
  • 签出到新分支(例如,
    功能
    ),然后添加、提交并推送到远程
    功能
    分支

    $ git reset --soft HEAD~1      # undo the last commit and keep the changes in working tree   
    
    $ git checkout -b feature      # create and checkout to new 'feature' branch
    $ git add -A                   # add the changes 
    $ git commit -m 'message'      # commit 
    $ git push origin HEAD         # push to remote 'feature' branch
    
  • 现在,从
    feature
    分支创建一个PR

    $ git reset --soft HEAD~1      # undo the last commit and keep the changes in working tree   
    
    $ git checkout -b feature      # create and checkout to new 'feature' branch
    $ git add -A                   # add the changes 
    $ git commit -m 'message'      # commit 
    $ git push origin HEAD         # push to remote 'feature' branch
    

    备选方案:

  • 签出到新分支(例如,
    功能
    )并将
    功能
    分支推到远程

    $ git checkout -b feature
    $ git push origin HEAD
    
  • 切换到
    master
    分支并撤消上次提交

    $ git checkout master
    $ git reset --hard HEAD~1
    
    Or, (reset the local 'master' with 'origin/master')
    $ git checkout master 
    $ git fetch origin
    $ git reset --hard origin/master
    

  • 现在,从
    功能
    分支创建PR。

    只需从
    源代码/主代码
    提取PR,然后重新基于最新提交。可能重复
    $ git reset --soft HEAD~1      # undo the last commit and keep the changes in working tree   
    
    $ git checkout -b feature      # create and checkout to new 'feature' branch
    $ git add -A                   # add the changes 
    $ git commit -m 'message'      # commit 
    $ git push origin HEAD         # push to remote 'feature' branch