简体   繁体   English

本地存储库无法从远程存储库中拉取分支

[英]Local repository is not able to pull branch from remote repository

I have been using GitLab successfully with another onsite developer with small projects in C and MATLAB.我一直在与另一位在 C 和 MATLAB 中进行小项目的现场开发人员一起成功地使用 GitLab。

Working from home using a VPN I pushed a large LabVIEW program into a fresh project so I can work alongside another off site developer.使用 VPN 在家工作 我将一个大型 LabVIEW 程序推送到一个新项目中,这样我就可以与另一位异地开发人员一起工作。 The developer cloned the project and began work on a branch.开发人员克隆了该项目并开始在一个分支上工作。

The issue is I have not been able to pull/fetch this branch.问题是我无法拉/取这个分支。 GitLab is showing the branches and commits that the developer has made. GitLab 正在显示开发人员所做的分支和提交。

I have not been long using git and so I may just be being stupid.我使用 git 的时间不长,所以我可能只是愚蠢。

Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git 推送:“fatal 'origin' 似乎不是 git 存储库 - 致命无法从远程存储库读取。”

I have looked at this in particular but have not made it work, below are some of the things I have tried.我特别看过这个,但没有让它起作用,下面是我尝试过的一些东西。

$ git remote remove somelongcode
fatal: No such remote: 'somelongcode'

$ git remote add origin
usage: git remote add [<options>] <name> <url>

    -f, --fetch           fetch the remote branches
    --tags                import all tags and associated objects when fetching
                          or do not fetch any tag at all (--no-tags)
    -t, --track <branch>  branch(es) to track
    -m, --master <branch>
                          master branch
    --mirror[=(push|fetch)]
                          set up remote as a mirror to push to or fetch from

$ git remote master --prune
error: Unknown subcommand: master
usage: git remote [-v | --verbose]
   or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
   or: git remote rename <old> <new>
   or: git remote remove <name>
   or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
   or: git remote [-v | --verbose] show [-n] <name>
   or: git remote prune [-n | --dry-run] <name>
   or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
   or: git remote set-branches [--add] <name> <branch>...
   or: git remote get-url [--push] [--all] <name>
   or: git remote set-url [--push] <name> <newurl> [<oldurl>]
   or: git remote set-url --add <name> <newurl>
   or: git remote set-url --delete <name> <url>

    -v, --verbose         be verbose; must be placed before a subcommand
________________________
$ git fetch origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
________________________
$ git reset --hard origin/master
fatal: ambiguous argument 'origin/master': unknown revision or path not `in the working tree.`
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I can see the branch in remote.我可以看到远程的分支。 This is the most encouraging at least I can see the remote even if I cannot sync with my local repository这是最令人鼓舞的,至少即使我无法与本地存储库同步,我也可以看到远程

$ git ls-remote
From git@gitlab.com:group/project.git
somelongcode        HEAD
somelongcode        refs/heads/apploc
somelongcode        refs/heads/master

As you can see locally I cannot see the branch正如你在本地看到的,我看不到分支


$ git pull
From gitlab.com:group/project
 * branch            master     -> FETCH_HEAD
Already up to date.

$ git branch
* master

$ git remote show origin
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

The structure of any git repo is the same wherever you go, you can check your local branches under the folder .git/refs/heads or by using the command git branch .无论你走到哪里,任何 git repo 的结构都是一样的,你可以在文件夹 .git/refs/heads 下或使用命令git branch来检查你的本地git branch

As for what's in the remote declared in your repo, you can see that in .git/refs/remotes/ or by using the command git branch -a and check the red colored lines.至于在您的 repo 中声明的遥控器中的内容,您可以在 .git/refs/remotes/ 中看到,或者使用命令git branch -a并检查红色线条。

Now to the subject at hand, any git repo needs to know the url and the name of whatever remote you're going to use, and you can use git remote to manage that.现在谈到手头的主题,任何 git repo 都需要知道您将要使用的任何遥控器的 url 和名称,您可以使用git remote来管理它。 So if you want to list them use git remote , to list Up and Down stream of each one use git remote -v , and to add a new remote use git remote add <remote_name> <remote_url> .因此,如果您想列出它们,请使用git remote ,列出每个使用git remote -v上行和下行流,并添加新的远程使用git remote add <remote_name> <remote_url>

The remote url is composed as follows if you are using ssh git@<server_address>:<name_space>/<repo_name>.git so it's composed of:如果您使用的是 ssh git@<server_address>:<name_space>/<repo_name>.git ,则远程 url 的组成如下:

  • server address which if you're using gitlab is gitlab.com ,服务器地址,如果您使用的是gitlab.comgitlab.com
  • name space which is group as you mentioned, and lastly,正如您提到的那样,名称空间group ,最后,
  • repo name which in our case is project but you still need the .git extension at the end of the url.回购名称,在我们的例子中是project但您仍然需要在 url 末尾添加.git扩展名。

You can also copy the url from the gitlab interface by clicking on the clone button which will show you the url with ssh and another with https.您还可以通过单击克隆按钮从 gitlab 界面复制 url,该按钮将显示带有 ssh 的 url 和另一个带有 https 的 url。

Now if you configured your remote correctly you will need to refresh your local repository with what's in the remote by using the command git fetch <remote_name> and that will download all the branches from the remote to your .git/refs/remotes/ folder, Then if you have a branch that's there and doesn't have any mirror branch in local (.git/refs/heads) you can create it yourself to be able to add other commits on top of it by using the command git checkout -b <branch_name> <remote_name>/<branch_name> .现在,如果您正确配置了远程,则需要使用命令git fetch <remote_name>使用远程中的内容刷新本地存储库,这会将远程的所有分支下载到您的 .git/refs/remotes/ 文件夹,然后,如果您有一个分支并且在本地 (.git/refs/heads) 中没有任何镜像分支,您可以自己创建它,以便能够使用命令git checkout -b <branch_name> <remote_name>/<branch_name>在其上添加其他提交git checkout -b <branch_name> <remote_name>/<branch_name>

Try the below试试下面的

$ git remote add origin remote/repository/URL
# Sets the new remote

$ git remote -v
# Verifies the new remote URL

Then然后

git pull origin branch_name

Hi @Yannoff and anyone else who looked at this.嗨@Yannoff 和其他看过这个的人。 Thanks for you help.谢谢你的帮助。 I think I tried nearly everything with Bash.我想我用 Bash 尝试了几乎所有的东西。 I cloned the project on another machine and it all worked as expected.我在另一台机器上克隆了这个项目,一切都按预期工作。 I then cloned it again on my machine and again everything worked fine.然后我在我的机器上再次克隆它,一切正常。 It was my original local repository that was causing the issues.导致问题的是我原来的本地存储库。 I wonder whether working from home over VPN had an effect?我想知道通过 VPN 在家工作是否有影响? Everything works now but not my original local repo.现在一切正常,但不是我原来的本地回购。

However I am now clearer on how it all works with the help of the web links you gave me so it has not all been in vein!但是,在您给我的网络链接的帮助下,我现在更清楚这一切是如何工作的,因此并非全部都在脉中!

*** I was not setting up the remote origin correctly see Yannoff's advice and the accepted answer for reasoning.*** *** 我没有正确设置远程源,请参阅 Yannoff 的建议和公认的推理答案。***

Very good website - https://www.atlassian.com/git/tutorials非常好的网站 - https://www.atlassian.com/git/tutorials

To synchronize your local repository branch list with the remote one, use git fetch要将本地存储库分支列表与远程存储库分支列表同步,请使用git fetch

So your command should be something like:所以你的命令应该是这样的:

git fetch somelongcode

Then you should have the up-to-date branch list :)那么你应该有最新的分支列表:)

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

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