简体   繁体   English

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

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

In git, I've been making commits onto the master branch, when really I should have been working on a feature branch.在 git 中,我一直在提交到 master 分支,而实际上我应该在功能分支上工作。 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.我想改变这一点,让 master 回到它开始的地方,master 上的东西现在在一个新的分支上。 Basically, my commit history looks like this:基本上,我的提交历史如下所示:

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?如何更改大师点的位置?

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

stash/unstash is not necessary if your working tree is clean.如果您的工作树是干净的,则不需要 stash/unstash。 just make sure there are no changes in your working tree, because those will be removed when you reset --hard只需确保您的工作树中没有任何更改,因为当您重置 --hard 时这些将被删除


another possibility (faster, and without the need to stash and reset):另一种可能性(更快,无需存储和重置):

  • checkout a new branch: git checkout -b new_branch master签出一个新分支: 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创建一个“新”主分支并将其指向 origin/master 的提交: git branch -f master origin/master
$ 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.此示例显示将 master 移动到不同的提交。 Note here that the tag allows us to save the old master, in case :)请注意,标签允许我们保存旧的主人,以防万一:)

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

Go to .git/refs/heads/master which has the hash of master and change that to whatever you want.转到具有 master 哈希值的 .git/refs/heads/master 并将其更改为您想要的任何内容。 I use gitg to quickly find the hash of master and afterwards to verify that the move was successful.我使用 gitg 快速找到 master 的哈希值,然后验证移动是否成功。

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

Note that at any given time you can change where a branch points to by using git update-ref refs/heads/branch id , but before you do this, you must give a name to the tip of the tree, otherwise your work will unaccessible.请注意,在任何给定时间,您都可以使用git update-ref refs/heads/branch id更改分支指向的位置,但在此之前,您必须为树尖命名,否则您的工作将无法访问. So these two commands may do the job所以这两个命令可以完成这项工作

 git update-ref refs/heads/newfeature HEAD
 git update-ref refs/heads/master XXYYY

But make sure that you do not have any uncommited changes otherwise all hell will break loose但是请确保您没有任何未提交的更改,否则一切都会崩溃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM