让git master HEAD指向当前分支的HEAD

发布于 2024-10-04 10:57:53 字数 381 浏览 0 评论 0原文

我必须承认我还没有使用过 git 的高级功能,但在我当前的项目中我不得不使用。

情况: 有人试图实现一些功能并将它们提交给master,现在我被要求做其他人试图做的事情(但失败了),因此,我做的第一件事是

git checkout -b clean_start HASH

哈希是之前大约20次提交的正确SHA1哈希当前的主人并且有效。我现在对此分支进行了一些更改,现在我想将远程存储库的当前主分支(具有其他人所做的更改)更改为我的本地分支。

换句话说,我想将 master 20 次提交的头部移回来,然后将我的新干净分支合并到其中。

这正是我必须做的吗?通过恢复 HEAD~20 等,或者是否有一个命令可以精确地进行这样的头部移动?

I have to admit that I haven't played with gits advanced features but on my current project I had to.

The situation:
Someone tried to implement some features and comitted them to the master, now I was called to do what this other person tried to do (but failed), therefore, the first thing I did was

git checkout -b clean_start HASH

Hash is a correct SHA1 hash of about 20 commits before the current master and that worked. I now made some changes to this branch and am now at a point where I'd like to change the current master branch of remote repository (that has the changes made by the other person) to my local branch.

In other words, I'd like to move the head of the master 20 commits back and then merge my new clean branch into it.

Is that exactly what I have to do? With revert HEAD~20 etc. or is there a command that makes exactly such a head move?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说不完的你爱 2024-10-11 10:57:53

如果远程存储库接受强制推送,您可以执行此操作:

git push --force origin clean_start:master

请注意,如果其他人克隆了存储库,来自他们的推送可能会撤消此操作。如果您想合并本地 master 分支和远程 master 分支,但保留分支中的文件树(丢弃原始 master 的文件树),您可以这样做:

git merge -s ours --no-commit origin/master
git commit  # Separate step so that you can use your own commit message.
git checkout master
git merge clean_start  # Fast-forward
git push origin master

这将创建一个包含两个分支的合并提交(你的主人和起源的主人)作为它的父级,但树将与你当前的分支尖端相同。换句话说,它将创建一个符号合并,而没有发生实际的代码合并。

如果您不介意在存储库中工作的其他人会被打扰,您可以使用第一种方法。但是,如果您与已经进行了这些提交的其他人一起工作,则第二种方法将更加万无一失,并且会保留历史记录。

You can do this, if the remote repository accepts forced pushes:

git push --force origin clean_start:master

Note that if anyone else has the repository cloned, a push from them could potentially undo this. If you want to merge your local master branch and the remote master branch, but keep the file tree from your branch (discarding the file tree of origin's master), you can do so like this:

git merge -s ours --no-commit origin/master
git commit  # Separate step so that you can use your own commit message.
git checkout master
git merge clean_start  # Fast-forward
git push origin master

This will create a merge commit with both branches (your master and origin's master) as its parent, but the tree will be identical to your current branch tip. In other words, it will create a symbolic merge where no actual code merge occurred.

If you don't mind that other people working in the repo will be interrupted, you can use the first approach. But if you work with other people who already have these commits, the second approach will be more fool-proof and will keep history.

記憶穿過時間隧道 2024-10-11 10:57:53
  1. 您可以明确地将 master 指向您想要的位置:

    git update-ref refs/heads/master clean_start
    

    (如果您正在跟踪 clean_start 中的新更改并希望 master 指向那里)

    请注意,master 所指向的任何内容(大约 20 次提交的价值)都将“丢失”。

    因此,您需要强制主控推送:

    git Push origin master -f
    
  2. 如果您想将本地 master 保留在原处,并将 clean_start 的位置推送到远程 master,只需执行以下操作:

    git push origin clean_start:master -f

    希望这有帮助。

    PS。首先运行 gitk --all & ,这样您就可以在执行此操作时直观地看到发生了什么。

  1. You can specifically point master to where you want it to be with:

    git update-ref refs/heads/master clean_start
    

    (if you are tracking the new changes in clean_start and want master to point there)

    Beware that whatever master was pointing to (about 20 commits worth) will be "lost".

    You will need to force the push of master because of this:

    git push origin master -f
    
  2. If you want to leave your local master where it is instead and push clean_start's place to the remote master, just do this:

    git push origin clean_start:master -f

    hope this helps.

    PS. Run gitk --all & first so you can see what's going on visually as you do this.

慈悲佛祖 2024-10-11 10:57:53

git reset 命令的存在是为了更改 HEAD 指向的内容。

对于您的情况,您可以这样做:

git checkout master              # switch to the master branch
git reset --hard clean_start     # point HEAD to the clean_start branch
git push -f origin master:master # force push the new HEAD to server

The git reset command exists to change what HEAD points to.

In your case, you can do this:

git checkout master              # switch to the master branch
git reset --hard clean_start     # point HEAD to the clean_start branch
git push -f origin master:master # force push the new HEAD to server
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文