简体   繁体   English

git:将远程设置为本地存储库本身

[英]git: setting a remote to the local repository itself

let's create a new git repository:让我们创建一个新的 git 存储库:

$ git init test
$ cd test

and then add a remote, pointing to ourselves然后添加一个遥控器,指向我们自己

$ git remote add origin .

note the dot, referring to the test directory.注意点,指的是测试目录。

this seems to function as expected.这似乎按预期运行。 you can make commits, set up branch tracking, and push and pull from yourself.您可以进行提交,设置分支跟踪,以及从自己那里推送和拉取。 the only strange thing i've run into is that if you commit, then fetch, your unpushed changes are already present if you go to push (it makes sense why this happens, but it's perhaps unexpected).我遇到的唯一奇怪的事情是,如果你提交,然后获取,如果你去推送,你未推送的更改已经存在(为什么会发生这种情况是有道理的,但这可能是出乎意料的)。

is this functionality stable / intended?这个功能稳定/预期吗? what are the consequences of using a remote like this?使用这样的遥控器有什么后果? will any git command usages error out on a repository like this?在这样的存储库中,任何 git 命令用法都会出错吗? are there any real use cases?有没有真正的用例?

I was thinking this might be good for getting git functionality minus the replication (as opposed to pushing to a bare repository on your same computer)我认为这可能有助于获得 git 功能减去复制(而不是推送到同一台计算机上的裸存储库)

this seems like a strange feature, and i'm surprised i haven't found any discussion on it anywhere.这似乎是一个奇怪的功能,我很惊讶我没有在任何地方找到任何关于它的讨论。

It's not a specific feature.这不是一个特定的功能。 It just falls naturally out of the fact that a remote is a short name for a URL where we'll find some other Git repository.很自然地,远程是一个 URL 的短名称,我们可以在其中找到其他一些 Git 存储库。 Normally .通常. would not be a URL—you'd have to spell out file:///path/to/git/repo for instance—but Git accepts certain extended URLs, including anything that is a valid path name.不会是 URL——例如你必须拼出file:///path/to/git/repo但 Git 接受某些扩展 URL,包括任何有效的路径名。 1 1

It does turn two things on their heads, though:不过,这确实让他们头疼的是两件事:

  • Normally origin is short for something longer .通常origin更长的东西的缩写 Here, origin is now "short" for .在这里, origin现在是. , which is shorter . ,这是更短的

  • Normally, the other Git repository is another Git repository.通常,另一个 Git 存储库是另一个Git 存储库。 Here, it's your own Git repository.在这里,它是您自己的 Git 存储库。 That's what makes some of things feel a little weird.这就是让一些事情感觉有点奇怪的原因。

There's no particular reason to bother with a name for the "other" Git repository though: just use git fetch . refspec不过,没有什么特别的理由需要为“其他”Git 存储库命名:只需使用git fetch . refspec git fetch . refspec or git push . refspec git fetch . refspecgit push . refspec git push . refspec . git push . refspec This leaves you without the convenience of setting an upstream for these two operations, but you can set the upstream of a (local) branch to be another (local) branch, which gets most of the convenience back.这让您无法方便地为这两个操作设置上游,但您可以将(本地)分支的上游设置为另一个(本地)分支,这将获得大部分便利。


1 As jthill notes in a comment , using a path name here changes some visible behaviors: in particular Git will, at clone time, make hard links to local-file-system objects if this is possible. 1正如jthill 在评论中指出的那样,在此处使用路径名会改变一些可见的行为:特别是 Git 会在克隆时建立到本地文件系统对象的硬链接(如果可能的话)。 Using file:// URLs disables this (as does --no-local ).使用file:// URL 会禁用此功能(与--no-local )。 So while the path occupies the syntactic location for a URL, it isn't really a URL.因此,尽管路径占据句法位置URL,这是不是一个真正的URL。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM