简体   繁体   English

将本地Git分支推送到原点

[英]Push local Git branch to origin

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分支?

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

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 ... 不确定机器2上的回购状态,所以我会删除目录(或者创建一个新的目录),然后在Machine2上克隆回购和结帐测试......

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

All you have to do is the following: 您所要做的就是以下内容:

On Machine 1 , deploy the remote branch and link the local testing branch to it: 在计算机1上 ,部署远程分支并将本地测试分支链接到它:

git push -u origin testing

On Machine 2 , deploy a local branch that's linked to the remote testing branch: 在计算机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

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 local remote-tracking branch called origin/testing that keeps track of the last known status of the testing branch on the remote. 你必须明确你的意思,因为在你将testing推送到远程仓库之后,还会有一个名为origin/testing本地远程跟踪分支,它跟踪远程testing分支的最后已知状态。 。 You can find this remote-tracking branch under .git/refs/remotes/origin/ . 你可以在.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 local remote-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分支,则可以使用--remotes-r标志运行git branch -d命令:

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 也可以看看

My next question probably will be: how to delete the origin/testing branch? 我的下一个问题可能是:如何删除origin / testing分支?

To answer your next question: Come out of branch you intend to delete and run: 回答你的下一个问题:出来你打算删除并运行的分支:

git branch -d < branch_name > git branch -d <branch_name>

-d delete fully merged branch -d delete完全合并的分支

-D delete branch (even if not merged) -D delete分支(即使没有合并)

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

相关问题 将本地git提交历史记录到Origin分支 - Push local git commits history into origin branch Git:查找用于推送到原始位置的本地分支 - Git: find local branch used to push to origin 如何从本地分支“X”隐式地“git push”到“origin / Y” - how to implicitly `git push` from a local branch “X” to “origin/Y” git push origin new_local_branch推送所有文件 - git push origin new_local_branch pushes all files Git将上游推到原点而不创建本地分支 - Git push upstream to origin without creating local branch git push origin HEAD: remote_branch 和 git push origin local_branch:remote_branch 有什么区别? - what is the difference between git push origin HEAD : remote_branch and git push origin local_branch:remote_branch? 为什么“git push git push origin local-branch :development”会删除远程开发分支? - why "git push git push origin local-branch :development" will delete remote development branch? 一个git命令创建新的本地分支,推送到原点,跟踪原点 - One git command to create new local branch, push to origin, track origin $ git checkout 你的分支在 'origin/main' 之前 4 次提交。 (使用“git push”发布你的本地提交) - $ git checkout Your branch is ahead of 'origin/main' by 4 commits. (use "git push" to publish your local commits) git push 本地分支到服务器 - git push local branch to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM