Git manual states远程分支无法修改
Remote branches are references (pointers) to the state of branches in your remote repositories. They’re local branches that you can’t move; they’re moved automatically for you whenever you do any network communication.
但我刚刚发现我实际上可以做到这一点。
这是实验:
cd rep1 && git init
echo "remote" > f1.txt
git add f1.txt
git commit -m "remote 1st commit"
git checkout -b new // switch branch to allow push to master from another repository
因此此提交的哈希值是 81d86e051a9c85347f3faab16e5b50d96093b75d
。
现在我想在另一个名为 rep2
的存储库中将分支作为远程分支:
cd rep2 && git init
git remote add origin "../rep1"
git fetch origin master
git branch -a //outputs remotes/origin/master
git checkout origin/master
现在我已经检查了远程分支,但 HEAD 处于分离状态。然后我更改 f1.txt
并提交更改:
echo "local change" > f1.txt && git add f1.txt
git commit -m "local commit 2"
HEAD 现在指向我的新提交的哈希 9ae81a259749351186f7d9f27269f1068fcafc80
,但 origin/master
仍然指向所做的提交 81d86e051a9c85347f3faab16e5b50d96093b75d
于 rep1
。
现在神奇的是:
git push origin HEAD:master
这会更新远程rep1
上的两个主分支指针:
$ git ls-remote origin
81d86e051a9c85347f3faab16e5b50d96093b75d HEAD
9ae81a259749351186f7d9f27269f1068fcafc80 refs/heads/master
和本地rep2
:
$ cat .git/refs/remotes/origin/master
9ae81a259749351186f7d9f27269f1068fcafc80
因此,我更新了远程分支,而没有创建本地分支。这里有趣的事实是 HEAD
在远程上没有改变,尽管我还不知道该怎么做。
那么为什么我要花 30 分钟来试验和打字呢?如果可能的话,我想知道这种方法可能会出现的问题。
PS。免责声明,我绝对不会使用这种方法,这只是好奇。最佳答案
they’re moved automatically for you whenever you do any network communication.
这正是您所做的,网络操作:
git push origin HEAD:master
这指示远程存储库 rep1
master
分支获取+ merge 当前 rep2
提交 HEAD。
git push
updates references inrefs/remotes
after updating them on the repository where commits where pushed, correct?
是的,本地 rep2
中的远程跟踪分支 remotes/origin/master
因此会更新,以记住远程 master
(在 rep1
中)现在引用新提交(因为推送是在 rep1
上作为快进 merge 完成的)。
关于git - 在没有本地的情况下修改远程分支 - 后果是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28324064/