将本地 Git 分支推送到原点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3109456/
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
Push local Git branch to origin
提问by takeshin
I'm working on two machines and origin repo on third one (origin is accessible from the other two, no direct connection between machine1 and machine2).
我正在两台机器上工作,第三台机器上的原始存储库(原始库可以从其他两台访问,机器 1 和机器 2 之间没有直接连接)。
# Machine 1
$ git branch
master
* testing
cms
# Machine 2
$ git branch
* master
Now, I want to push the testing
branch to the origin and have it on machine2 too, so the final effect would be:
现在,我想将testing
分支推送到原点并将其也放在 machine2 上,因此最终效果将是:
# Machine 2
$ git branch
* testing
master
I have tried:
我试过了:
# Machine 1
$ git push origin testing
# Machine 2
$ git pull origin testing # bunch of merge conflicts
$ git checkout -b testing origin/testing # errors too
Current state is:
目前的状态是:
# Machine 1
$ git branch -r
origin/HEAD -> origin/master
origin/master
origin/testing
How to do it?
怎么做?
My next question probably will be: how to delete the origin/testing
branch?
我的下一个问题可能是:如何删除origin/testing
分支?
采纳答案by abarr
You have successfully pushed the testing branch to origin so now you just need to get the branch to machine2.
您已成功将测试分支推送到原点,因此现在您只需要将分支发送到 machine2。
Not sure of the state of teh repo on machine2 so I would delete the directory (Or create a new one to use) and just clone the repo on Machine2 and checkout testing ...
不确定 machine2 上的 repo 的状态,所以我会删除目录(或创建一个新的目录),然后在 Machine2 上克隆 repo 并结帐测试......
So on Machine2 in an empty dir (new or clean):
因此,在空目录(新的或干净的)中的 Machine2 上:
$ git clone git@github/somewhere
$ git branch
* master
$ git checkout testing
# ...
# something about getting and switching
# ...
$ git branch
* testing
master
回答by Bruno
Instead of using git pull
(which fetches and merges), try git fetch
(which won't try to merge with your current branch on machine2, and cause conflicts):
而不是使用git pull
(获取和合并),尝试git fetch
(它不会尝试与 machine2 上的当前分支合并,并导致冲突):
# Machine 1
$ git push origin testing
# Machine 2
$ git fetch origin testing
$ git checkout -b testing origin/testing
回答by Loukan ElKadi
All you have to do is the following:
您所要做的就是以下几点:
On Machine 1, deploy the remote branch and link the local testing branch to it:
在 Machine 1 上,部署远程分支并将本地测试分支链接到它:
git push -u origin testing
On Machine 2, deploy a local branch that's linked to the remote testing branch:
在 Machine 2 上,部署一个链接到远程测试分支的本地分支:
git fetch origin && git checkout --track origin/testing
Now both local testing branches must be deployed and linked to the remote testing branch =)
现在必须部署两个本地测试分支并链接到远程测试分支 =)
Now for deleting a remote branch you can simply try this :
现在要删除远程分支,您可以简单地尝试以下操作:
git push origin --delete testing
or
或者
git push origin :testing
回答by Loukan ElKadi
The original poster states:
原始海报指出:
My next question probably will be: how to delete the
origin/testing
branch?
我的下一个问题可能是:如何删除
origin/testing
分支?
You have to be clear what you mean by that, because after you push testing
to the remote repo, there will also be a localremote-tracking branch called origin/testing
that keeps track of the last known status of the testing
branch on the remote. You can find this remote-tracking branch under .git/refs/remotes/origin/
.
你必须清楚你的意思,因为在你推testing
送到远程仓库之后,还会有一个本地远程跟踪分支被调用origin/testing
,它跟踪远程分支的最后一个已知状态testing
。您可以在 下找到这个远程跟踪分支.git/refs/remotes/origin/
。
Deleting both the remote testing
and local origin/testing
branches
删除远程testing
和本地origin/testing
分支
If you delete the branch on the remote with the following commands:
如果您使用以下命令删除远程上的分支:
git push origin :testing
# Or
git push origin --delete testing
then the localremote-tracking branch origin/testing
will also be deleted for you automatically.
那么本地远程跟踪分支origin/testing
也将自动为您删除。
Deleting just the local origin/testing
branch
只删除本地origin/testing
分支
If for some reason you just wanted to delete the local remote-tracking branch origin/testing
, but not the testing
branch on the remote, then you can run the git branch -d
command with the --remotes
or -r
flag:
如果出于某种原因您只想删除本地远程跟踪分支origin/testing
,而不是testing
远程上的分支,那么您可以git branch -d
使用--remotes
or-r
标志运行命令:
git branch --delete --remotes origin/testing
# Or shorter
git branch -dr origin/testing
If you get an error message that the branch can't be deleted because it hasn't been merged yet, then you can force the deletion by using the -D
flag instead of --delete
or -d
.
如果您收到一条错误消息,指出由于尚未合并分支而无法删除该分支,那么您可以使用-D
标志而不是--delete
或来强制删除-d
。
See Also
也可以看看
回答by sarjeetsingh
My next question probably will be: how to delete the origin/testing branch?
我的下一个问题可能是:如何删除原点/测试分支?
To answer your next question: Come out of branch you intend to delete and run:
回答您的下一个问题:退出您打算删除并运行的分支:
git branch -d < branch_name >
git branch -d < 分支名称 >
-d delete fully merged branch
-d 删除完全合并的分支
-D delete branch (even if not merged)
-D 删除分支(即使没有合并)