如何在 git 中更改哪个提交主指向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4166019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:28:18  来源:igfitidea点击:

How can I change which commit master points to in git?

git

提问by Rudd Zwolinski

In git, I've been making commits onto the master branch, when really I should have been working on a feature branch. I want to change this so that master is back to where it started, and what was on master is now on a new branch. Basically, my commit history looks like this:

在 git 中,我一直在提交到 master 分支,而实际上我应该在功能分支上工作。我想改变这一点,让 master 回到它开始的地方,master 上的东西现在在一个新的分支上。基本上,我的提交历史如下所示:

A -- B -- C -- D -- E
          |         |
          |       master
     origin/master

And I want it to look like this:

我希望它看起来像这样:

        master
          |
A -- B -- C -- D -- E
          |         |
          |       new_branch
     origin/master

How can I change where master points?

如何更改大师点的位置?

采纳答案by knittl

  • stash your uncommitted: git stash
  • create a new branch: git branch new_branch
  • reset master to origin/master: git reset --hard origin/master
  • checkout the new branch again: git checkout new_branch
  • unstash your changes: git stash pop
  • 隐藏你的未提交: git stash
  • 创建一个新分支: git branch new_branch
  • 将 master 重置为 origin/master: git reset --hard origin/master
  • 再次结帐新分支: git checkout new_branch
  • 取消隐藏您的更改: git stash pop

stash/unstash is not necessary if your working tree is clean. just make sure there are no changes in your working tree, because those will be removed when you reset --hard

如果您的工作树是干净的,则不需要 stash/unstash。只需确保您的工作树中没有任何更改,因为当您重置 --hard 时这些将被删除



another possibility (faster, and without the need to stash and reset):

另一种可能性(更快,无需存储和重置):

  • checkout a new branch: git checkout -b new_branch master
  • create a 'new' master branch and point it to origin/master's commit: git branch -f master origin/master
  • 结帐新分支: git checkout -b new_branch master
  • 创建一个“新”主分支并将其指向 origin/master 的提交: git branch -f master origin/master

回答by J-Dizzle

$ git checkout master
$ git reset --hard <commit-id-for-master-to-sit-at>

for example try this

例如试试这个

$ mkdir example; cd example
$ git init
$ vi testFile.txt
(now add "test commit 1" to line 1 of file)
$ git add *
$ git commit
(add message "(+) 1st commit" to git commit)
$ vi testFile.txt
(now add "test commit 2" to line 1 of file)
$ git add *
$ git commit
(add message "(+) 2nd commit" to git commit)
$ vi testFile.txt
(now add "test commit 3" to line 1 of file)
$ git add *
$ git commit
(add message "(+) 3rd commit" to git commit)
$ git tag final_head
$ git reset --hard HEAD~1

this example shows moving the master to a different commit. Note here that the tag allows us to save the old master, in case :)

此示例显示将 master 移动到不同的提交。请注意,标签允许我们保存旧的主人,以防万一:)

回答by Ioannis Filippidis

Like outlined here, but even simpler, no resets involved, just create a new branch where master was, then painlessly delete master, checkout again the place which you want to move master to and create a new master branch there:

就像这里概述的那样,但更简单,不涉及重置,只需在 master 所在的位置创建一个新分支,然后轻松删除 master,再次签出您要将 master 移动到的位置并在那里创建一个新的 master 分支:

git stash
git checkout -b old_master_was_here
git branch -d master
git checkout origin/master
git checkout -b master

回答by gescho

Go to .git/refs/heads/master which has the hash of master and change that to whatever you want. I use gitg to quickly find the hash of master and afterwards to verify that the move was successful.

转到具有 master 哈希值的 .git/refs/heads/master 并将其更改为您想要的任何内容。我使用 gitg 快速找到 master 的哈希值,然后验证移动是否成功。

回答by Mot

Create a new branch new_branchat the current HEAD (assuming HEAD = master), reset master to C and switch to new_branchagain (speaking in terms of SmartGit).

new_branch在当前 HEAD 处创建一个新分支(假设 HEAD = master),将 master 重置为 C 并new_branch再次切换到(以 SmartGit 而言)。