【问题标题】:How to take my local commits from master and place them in a new branch [duplicate]如何从主人那里获取我的本地提交并将它们放在新的分支中[重复]
【发布时间】:2013-03-17 11:02:27
【问题描述】:

我已经对 git 存储库创建了一些更改,并将它们提交给 master(但是我没有将更改推送到 GitHub)。我现在需要做的是创建一个新分支,并将我的提交转移到这个新分支。更改非常大,因此无法在新分支上手动重做更改 - 希望有一些命令可以将 master 倒回到我的提交之前,将我的提交移动到新分支上,然后将它们向上推送。

我确实搜索了其他问题,但我没有看到任何完全符合我的情况的问题,所以想得到一个确切的答案。

提前致谢!

【问题讨论】:

    标签: git branch commit


    【解决方案1】:

    这很容易:

    # make sure you're on master
    git checkout master
    # create a new branch that is identical to master
    git branch mystuff master
    # reset your local master branch to the state of the remote master
    git reset --hard origin/master
    # push your new branch to the remote
    git push origin mystuff
    

    【讨论】:

    • 这完美无瑕 - Fernaref 可能也有效,但因为这似乎更容易,我先尝试了这个:P
    • 是的,当您想要回滚到的提交是源/主的当前 HEAD 时会更容易。
    【解决方案2】:

    首先创建新分支,然后回退master:

    git branch new_branch
    git reset --hard <sha1-id>
    

    要查找所需的 sha1-id,请检查 git log

    之后,您可以将该分支推送到您的遥控器:

    git checkout new_branch
    git push -u origin new_branch
    

    请注意,-u 将设置一个跟踪分支,这意味着从现在开始您无需指定 origin new_branch 即可发出 git pullgit push

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2016-06-12
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多