Git在离线状态下设置跟踪分支 [英] Git set tracking branch while offline

查看:141
本文介绍了Git在离线状态下设置跟踪分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绑定分支 原因/分支,以便后续从分支推送可以只是 git push ,据我了解。



我可以在离线状态下为全新的回购( origin / branch 尚不存在)设置此类跟踪吗?我希望将它从分支所有后续推送到 origin / branch ,而不必在我上线时指定。


<我将使用 xyz 作为分支的名称并保留 origin

/ code>作为远程名称以避免混淆。您已经提到了 online 方法来同时推送和设置上游。

  git push --set-upstream origin xyz 



在线提取,上游离线设置



首选的方法是在离线前获取分支,并使用 git的 - set-upstream-to 选项

  git branch --set-upstream-to origin / xyz分支设置上游而不推送。 

这导致将以下行添加到 .git / config

  [分支c $ c>这也是前面描述的在线方式的结果。

xyz]
remote = origin
merge = refs / heads / xyz



完全离线的方法



如果您不想首先获取远程分支,您可以编辑 .git / config git config 工具来执行

  git config branch.xyz.remote origin 
git config branch.xyz.merge refs /头部/ xyz

当您取回远程分支时,您应该达到与 - set-upstream-to ,远程分支已经获取。

另类离线方法

您可以通过将远程分支ref设置为特定提交来模拟取消提取,例如与你的本地分支一样。

  git update-ref refs / remotes / origin / xyz xyz 
git branch --set-upstream-to origin / xyz

直接参考更新的想法是从Decave's回答。小心使用并阅读 git-fetch(1) 关于快进/非快进提取的注释。


git push -u origin branch

binds branch with origin/branch so that subsequent pushes from branch can be just git push, as far as I understand.

Can I set up this kind of tracking for a brand new repo (origin/branch doesn't exist yet) while offline? I want to make it so all subsequent pushes from branch go to origin/branch without me having to specify that when I go online.

解决方案

I'll use xyz as the name of the branch and keep origin as the name of the remote to avoid confusion. You already mentioned the online method to push and set upstream at the same time..

git push --set-upstream origin xyz

Fetch online, set upstream offline

The preferred way would be to fetch the branch before going offline and using --set-upstream-to option of git branch to set upstream without pushing.

git branch --set-upstream-to origin/xyz

That results in adding the following lines to .git/config which is also the result of the online way described before.

[branch "xyz"]
    remote = origin
    merge = refs/heads/xyz

Fully offline approach

When you don't want to fetch the remote branch first, you can either edit .git/config by hand or use the git config facility to perform the same action.

git config branch.xyz.remote origin
git config branch.xyz.merge refs/heads/xyz

When you then fetch the remote branch, you should have reached the same result as with --set-upstream-to with remote branch already fetched.

Alternative offline approach

You can instead mimic the fetch offline by setting the remote branch ref to a specific commit, e.g. to the same commit as your local branch.

git update-ref refs/remotes/origin/xyz xyz
git branch --set-upstream-to origin/xyz

The idea of direct ref updating is borrowed from Decave's answer. Use with care and read git-fetch(1) notes about fast-forward/non-fast-forward fetches.

这篇关于Git在离线状态下设置跟踪分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆