You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

远程Git仓库公钥认证失败,如何获取仓库并重建?

Solution: Copy Git Repository via Regular SSH & Recreate Remote

1. Copy the Remote Git Repository to Your Local Machine

Since you can SSH with your regular user, use scp or rsync to transfer the entire Git repository directory from the VPS. First, locate the repo path on the VPS—common locations include:

  • Bare repos: /home/git/repositories/your-project.git
  • Non-bare (working directory) repos: /var/www/your-rails-app/.git

Using SCP:

scp -r your_regular_user@<IP ADRESS>:/path/to/remote/repo.git ~/local/destination/folder/

Using Rsync (ideal for large repos):

rsync -avz your_regular_user@<IP ADRESS>:/path/to/remote/repo.git ~/local/destination/folder/

2. Validate the Local Copy

Once transferred, confirm the copy is a valid Git repo:

cd ~/local/destination/folder/repo.git
git log --oneline  # Verifies commit history exists

If it’s a bare repo, create a working copy by cloning it locally:

git clone ~/local/destination/folder/repo.git your-local-working-copy

3. Recreate the Remote Repository on VPS (If Needed)

To restore remote push/pull functionality, choose one of these options:

Option A: Fix the git User’s SSH Access

Even if you tried common steps, double-check these quick fixes:

  • Copy your local public key to the git user’s authorized keys via your regular account:
    sudo cat ~/.ssh/id_ed25519.pub >> /home/git/.ssh/authorized_keys
    
  • Set strict permissions for the git user’s .ssh folder:
    sudo chown -R git:git /home/git/.ssh
    sudo chmod 700 /home/git/.ssh
    sudo chmod 600 /home/git/.ssh/authorized_keys
    
  • Restart the SSH service: sudo service ssh restart

Option B: Create a New Bare Repository

If fixing the git user fails, create a new repo using your regular user (or transfer ownership to git later):

  1. SSH into the VPS: ssh your_regular_user@<IP ADRESS>
  2. Initialize a new bare repo:
    mkdir -p ~/new-project.git && cd ~/new-project.git
    git init --bare
    
  3. (Optional) Transfer ownership to the git user:
    sudo chown -R git:git ~/new-project.git
    
  4. Add the new remote to your local working copy:
    git remote add new-origin your_regular_user@<IP ADRESS>:~/new-project.git
    # Use git@ if you fixed access: git remote add new-origin git@<IP ADRESS>:~/new-project.git
    
  5. Push all branches and tags to the new remote:
    git push new-origin --all
    git push new-origin --tags
    
  6. (Optional) Set the new remote as default:
    git remote rename origin old-origin
    git remote rename new-origin origin
    

内容的提问来源于stack exchange,提问作者Jonas S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 20:27:00