git fetch with path instead of remote

时光总嘲笑我的痴心妄想 提交于 2020-01-14 10:13:52

问题


I understand the idea of running git fetch <remote>, because the remote branches are then available with git checkout <remote>/<branch>.

But how does it work if I just run

git fetch path/to/other/repo

How can I checkout the corresponding branches? Note that the operation runs silently (even with --verbose), and that no new branch is created.

edit: just to be clear: I perfectly understand how git works with remotes. I'm just curious about this alternate syntax git fetch path/to/remote, or git fetch <url>. How is it supposed to work? Why does it not create new branches? Why does it run silently even in verbose mode? What is the intended usage?


回答1:


Even if it's "local" in terms of file system, another repository is a remote repository.

If you want to use it, add it as a remote repository first (even if the url is local):

git remote add <name> <url>

Then, proceed with git fetch (and other commands) in the same way as with repository on remote machines.

EDIT: If you do a simple git fetch from a local path, it creates a new pseudo-branch called FETCH_HEAD. You can check it out in a new branch for example using:

git checkout -b new_branch FETCH_HEAD



回答2:


I think the best explanation (much better than the docs) is an answer to another question by Jakub Narębski.

Basically:

git fetch <path>

just fetches the HEAD branch of the remote, and stores it locally into FETCH_HEAD.

git fetch <path> <branch>

fetches the branch in the remote repo and stores it in FETCH_HEAD.

More advanced usages are described in Jakub Narębski's answer, but, as he states himself, the best way to fetch is to use named remotes.




回答3:


You have to explicitly pass a refspec to git fetch to get the remote branches. git help fetch should help.



来源:https://stackoverflow.com/questions/3829426/git-fetch-with-path-instead-of-remote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!