使 git master HEAD 指向分支的当前 HEAD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4271938/
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
Make git master HEAD point to current HEAD of branch
提问by Maxem
I have to admit that I haven't played with gits advanced features but on my current project I had to.
我不得不承认我没有使用过 gits 高级功能,但在我当前的项目中我不得不这样做。
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.
哈希是在当前主节点之前大约 20 次提交的正确 SHA1 哈希,并且有效。我现在对该分支进行了一些更改,现在我想将远程存储库的当前主分支(具有其他人所做的更改)更改为我的本地分支。
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.
换句话说,我想将 master 20 提交的头部移回,然后将我的新干净分支合并到其中。
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?
这正是我必须做的吗?使用 revert HEAD~20 等,或者是否有一个命令可以使头部移动?
采纳答案by cdhowie
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:
请注意,如果其他人克隆了存储库,则来自他们的推送可能会撤消此操作。如果要合并本地 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
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.
这将创建一个合并提交,将两个分支(您的 master 和 origin 的 master)作为其父级,但该树将与您当前的分支提示相同。换句话说,它将创建一个符号合并,其中没有发生实际的代码合并。
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.
如果你不介意在 repo 中工作的其他人会被打断,你可以使用第一种方法。但是如果你和其他已经有这些提交的人一起工作,第二种方法将更加万无一失,并且会保留历史。
回答by Adam Dymitruk
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
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.
您可以专门将 master 指向您想要的位置:
git update-ref refs/heads/master clean_start
(如果您正在跟踪新更改
clean_start
并希望 master 指向那里)请注意,master 指向的任何内容(大约 20 次提交)都将“丢失”。
由于这个原因,您将需要强制推送 master :
git push origin master -f
如果您想将本地 master 留在原处,并将 clean_start 的位置推送到远程 master,只需执行以下操作:
git push origin clean_start:master -f
希望这可以帮助。
附注。
gitk --all &
首先运行,以便您在执行此操作时可以直观地看到发生了什么。
回答by jtdubs
The git resetcommand exists to change what HEAD points to.
在git的复位命令存在改变什么HEAD点。
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