How to sort out 3 versions if I can't I hook up new server to remote repo despite correct ssh keys?
Asked by David Newey:

Our server admin set up a new server for our staging using an image from our Production server. I made the necessary config changes to get it working on the new server. However, the git instance was also carried over (It is an older version of a dev branch in the repo. ) and when I checked 'git status' I got that this code was 2 commits ahead of the remote (BitBucket) repo and there were close to 30 files listed as having changes.

I had an issue with needing to use 'sudo' for my git commands which I finally solved by globally changing permissions to 775 in the .git directory except for HEAD, index and a file called ORIG_HEAD. I combed through the 30 files listed as having changes and removed those that appeared to be older versions of what's in the repo and committed anything that looked like it matched the repo. This left a branch that was clean except for being now 4 commits ahead of origin.

Prior to this I had already sorted out with the Bit Bucket team the proper ssh key set-up for read/write permissions - using keys placed in my Personal Settings ssh key store. However when I tried to 'fetch --all' this branch from the repo I was given the message:

```
Fetching origin
Unauthorized
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
error: Could not fetch origin
```

I then tried to checkout master and ran into files that would be over-written if I did the switch. Handled that and finally switched to master. I attempted to pull master from the repo and was finally able to do that after handling some other files that would be over-written with the fetch. So, once I had that I committed the fetch and again checked out my dev branch and merged master into dev, and again tried to sync with the dev branch in the remote repo, getting the same error as above. I ran the command:

```
ssh -Tv mailto:git@bitbucket.org
```

getting the following read out:

```
OpenSSH_8.0p1, OpenSSL 1.1.1g FIPS? 21 Apr 2020

debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: configuration requests final Match pass
debug1: re-parsing configuration
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: Connecting to bitbucket.org [104.192.141.1] port 22.
debug1: Connection established.
debug1: identity file /home/centos/.ssh/id_rsa type 0
debug1: identity file /home/centos/.ssh/id_rsa-cert type -1
debug1: identity file /home/centos/.ssh/id_dsa type -1
debug1: identity file /home/centos/.ssh/id_dsa-cert type -1
debug1: identity file /home/centos/.ssh/id_ecdsa type -1
debug1: identity file /home/centos/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/centos/.ssh/id_ed25519 type -1
debug1: identity file /home/centos/.ssh/id_ed25519-cert type -1
debug1: identity file /home/centos/.ssh/id_xmss type -1
debug1: identity file /home/centos/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.0
debug1: Remote protocol version 2.0, remote software version conker_d56c8e5d87 5fffdb1ce7e9
debug1: no match: conker_d56c8e5d87 5fffdb1ce7e9
debug1: Authenticating to bitbucket.org:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256@libssh.org need=64 dh_need=64
debug1: kex: curve25519-sha256@libssh.org need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:-------------------------------------------
debug1: Host 'bitbucket.org' is known and matches the RSA host key.
debug1: Found key in /home/centos/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: centos@ip-10-2-1-239.us-east-2.compute.internal RSA SHA256:------------------------------------------- agent
debug1: Will attempt key: centos@ip-10-2-1-239.us-east-2.compute.internal RSA SHA256:------------------------------------------- agent
debug1: Will attempt key: /home/centos/.ssh/id_rsa RSA SHA256:------------------------------------------
debug1: Will attempt key: /home/centos/.ssh/id_dsa
debug1: Will attempt key: /home/centos/.ssh/id_ecdsa
debug1: Will attempt key: /home/centos/.ssh/id_ed25519
debug1: Will attempt key: /home/centos/.ssh/id_xmss
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: centos@______________ RSA SHA256:------------------------------------------ agent
debug1: Server accepts key: centos@______________ RSA SHA256:------------------------------------------- agent
debug1: Authentication succeeded (publickey).
Authenticated to bitbucket.org ([104.192.141.1]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
authenticated via ssh key.
?
You can use git to connect to Bitbucket. Shell access is disabled
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3036, received 1912 bytes, in 0.0 seconds
Bytes per second: sent 116475.2, received 73353.3
debug1: Exit status 0
```

This appears to be a successful connection, yet trying to connect to the dev branch on the remote repo still gives me:

```
Fetching origin
Unauthorized
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
error: Could not fetch origin
```

Where I had at least a working staging site before all of this I now have only 500 errors on my site pages with no place to go git wise - at least I don't know where to go. One other thing that should be known is that the code originally installed on the server was from the dev branch about 3 commits back. The code in the master branch on the remote repo is one commit behind the code in the dev branch because prior to trying to sort out the staging server I had done a fresh push from my dev instance on my machine to the remote dev branch. The dev server code works fine and is in sync with the dev branch on the remote repo. The production server code works fine, but is not in sync with the remote repo. We previously had been pushing to production directly from my dev machine and are trying to get in a standard release procedure through git.


Answers:
author: Theodora Boudale
body: Hi David,

I would like to ask for some information, so we can better troubleshoot the issues you're having.

**1.** The issues that you are experiencing now with git commands, do they all occur on the same new server with the same clone of the repo?

You mention in your message different machines, so I just want to make sure.

If issues occur on different clones / different machines, it's important to troubleshoot one at a time. In case the issues occur with different clones / different machines, please select one clone and provide info for this, so we can troubleshoot this first.

**2.** What is the output of the command

```
git remote -v
```

in that clone of the repo now? Feel free to replace the workspace-id and repo name with dummy values, but keep the URL format so we can check if it's correct.

**3.** You mentioned that one of the commands that are giving errors is the fetch command. Could you please execute this command as follows:

```
GIT_SSH_COMMAND="ssh -vvv" git fetch --all
```

and attach the full output here? Please feel free to remove any private info like workspace-id and repo names from the output prior to sharing it here.

**4.** You also mentioned that you get an error when trying to connect to dev branch on the remote repo.  
Could you please let us know the exact command (or commands) you are using, that is giving you this error?

Kind regards,  
Theodora

comments: author: David Newey
body: Hi Theodora,

Thank you for your response. Gladly, I was able to get the whole thing sorted out with the following:

1. Set up a new ssh key and placed it in my personal settings.

2. I had renamed my connection so I renamed the connection:

git remote add origin \<url\>

3. git fetch origin

4. git reset --hard origin/\<my branch\>

5. My local repo now could see the remote repo and the code worked on the server. With a couple tweaks, everything was fixed and running.


author: Theodora Boudale
body: Hi David,

Thank you for the update, I'm glad to hear that you were able to sort this out.

Please feel free to reach out if you ever need anything else.

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud

git

laravel