简体   繁体   中英

`ssh -vT git@github.com` works fine, and agent is in github ssh keys. Where is the issue?

I just bought a new machine, and I'm working on a new github repo. I clone it into my machine using

git clone git@github.com:<username>/<exact-repo-name>.git

and it clones fine:

Cloning into '<exact-repo-name>'...
remote: Counting objects: ###, done.
remote: Total ### (delta 0), reused 0 (delta 0), pack-reused ###
Receiving objects: 100% (###/###), ### KiB | 0 bytes/s, done.
Resolving deltas: 100% (###/###), done.
Checking connectivity... done.

And, I add all of the remote branches locally by doing: for remote in git branch -r; do git checkout -b $remote; done for remote in git branch -r; do git checkout -b $remote; done

Then, when I try to pull from any of the branches, using

git pull origin/<branch-name> 

I get the ever-so-common error:

origin/<branch-name> 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.

So, I go through all of the steps:

ssh -vT git@github.com
ssh-add -l
eval "$(ssh-agent -s)"

These all succeed, and I check the output of ssh-add -l and I see that it is in the list ssh keys on my github account. I go into Settings -> ssh keys, and I see the agent there.

Are there certain user permissions that are on top of those for ssh ?

I agree with @Felix that this most likely is not an SSH problem, though it might be masquerading as this. Instead, I think you have a problem with your path. You can try running git clone like this:

git clone https://username@github.com/username/exact-repo-name.git

Here the username is your GitHub username. Git will prompt you for a password so you don't have to enter it as plain text. Please read this SO article for more information.

It's probably not an ssh issue, as an ssh problem would normally result in a message like

fatal: The remote end hung up unexpectedly

Most likely it's a path problem: either you have an extra slash or space in the remote path (or if there's supposed to be a space, you're missing quotation marks in your command line), or some letter is the wrong case. Double-check the rest of the URL. It could also be exactly what it says, a permissions problem -- are you sure you're connecting as the right user for this repository?

Edit: from your added details, it looks like you're just getting the syntax of the pull command mixed up. Does it work if you do this?:

git checkout <branch-name>
git pull # Edit: don't do this without reading all of 'git help pull'

? Also, does git fetch --all work? ( git pull is just git fetch followed by git merge .)

Further edit: I can reproduce your error; it is a command syntax problem. Here are more examples:

"source" is a git repository in a local folder; I clone it into a folder called "dest":

$ git clone source dest
$ cd dest

Now I do the git branch command in your for loop:

$ git branch -r
  origin/HEAD -> origin/master
  origin/branch1
  origin/branch2
  origin/master

I would expect that first line to cause problems, since that would result in these commands being run in your for loop:

git checkout -b "origin/HEAD"
git checkout -b "->"
...

(Note that your example is missing backticks around `git branch -r` , so if what you posted is literally what you're running, you'll end up with branches called "git", "branch", and "-r", which would really not be what you want... if you had trouble putting backticks into inline code blocks, see https://meta.stackexchange.com/questions/55437/how-can-the-backtick-character-be-included-in-code )

Then if I try your pull command, I get the same error as you:

$ git pull origin/branch1
fatal: 'origin/branch1' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

This is because git pull takes the remote repository name (origin) and the remote branch name (branch1) as two separate parameters, separated by a space. So this works:

$ git pull origin branch1
From /tmp/source
 * branch            branch1    -> FETCH_HEAD
Already up-to-date.

But, that's probably not quite what you want, because it sounds like you wanted to create a bunch of local branches to track the same named branches on origin (eg you want a local branch named "branch1" that tracks "origin/branch1"). Your local branch creation commands didn't include setting up tracking, so what you've actually got is a local branch called "origin/branch1" and a branch on remote repository "origin" also called "branch1", ie after running your for loop, the output of git branch -a is:

$ git branch -a
  master
  origin/HEAD
  origin/branch1
* origin/branch2
  origin/master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch1
  remotes/origin/branch2
  remotes/origin/master

...So there are two branches called "origin/branch1", and the only thing that differentiates them is that in one case, the repository is your local one and the branch's literal full name is "origin/branch1", and in the other case, the repository is the remote one (named origin) and the branch's literal full name is "branch1", which is notated as "origin/branch1" -- this will probably be very confusing to work with (and in some commands, git will complain and say, "origin/branch1 is ambiguous" and you'll have problems).

Note also that each of the new branches your for loop created is actually just a copy of master (or whatever the default/selected branch was on origin), because you didn't specify a remote branch for them to start from. That is, in your clone command, your local repository was set up with one branch selected (probably master). You then told git "make new branch from where I am now" for each line in your for loop, so each of those new branches, despite having all the different names of the branches on remote, are references to the first branch selected with your clone. This is probably not what you wanted.

I think what you actually wanted was this command, run for each remote branch:

$ git checkout --track origin/branch1
Branch branch1 set up to track remote branch branch1 from origin.
Switched to a new branch 'branch1'

Actually, if you've just done a fresh clone, a git checkout branch1 [without the -b ] will have the same effect I think -- since there is no local branch called branch1, git will look for one in the repository you cloned from and set up tracking if it finds one.

...So I guess what this whole post boils down to is:

  • Leave out the -b in your initial checkout commands
  • Use a space instead of a slash in your pull command

:)

See https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches and the output of git help pull for more about remote tracking and pulling; some of it is a bit subtle (even more so than all the caveats I mentioned :) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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