如何撤销提交,放到新的分支中并创建 PR?

5

我有一个被保护的master分支,只能通过PR进行推送。

假设我在master上不经思考地进行操作:

git add .
git commit -m "bunch of changes"

但是我推送到分支时被拒绝了,因为该分支受到保护。 我该如何回退、保留我的更改并发起一个PR(Pull Request)呢?


只需从 origin/master 拉取,然后在最新的提交上进行变基。 - Mateen Ulhaq
可能是使用Git将最近的提交移动到新分支的重复问题。 - Clint
2个回答

6
  1. Undo the last commit of master branch.

    $ git reset --soft HEAD~1      # undo the last commit and keep the changes in working tree   
    
  2. Checkout to new branch (say, feature), then Add, Commit and Push to remote feature branch.

    $ 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。

备选:

  1. Checkout to new branch (say, feature) and push the feature branch to remote.

    $ git checkout -b feature
    $ git push origin HEAD
    
  2. Switch to master branch and undo the last commit.

    $ 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
    
现在,从feature分支创建PR(Pull Request)。

1
git reset HEAD~
git checkout -b "name-of-new-branch"
git -am "my fancy new commit"
git push origin "name-of-new-branch"

reset HEAD~会撤销您的最后一次提交。Checkout -b创建一个新分支并检出它,然后您只需在该分支上添加和提交更改,然后推送即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接