远程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
gituser’s authorized keys via your regular account:sudo cat ~/.ssh/id_ed25519.pub >> /home/git/.ssh/authorized_keys - Set strict permissions for the
gituser’s.sshfolder: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):
- SSH into the VPS:
ssh your_regular_user@<IP ADRESS> - Initialize a new bare repo:
mkdir -p ~/new-project.git && cd ~/new-project.git git init --bare - (Optional) Transfer ownership to the
gituser:sudo chown -R git:git ~/new-project.git - 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 - Push all branches and tags to the new remote:
git push new-origin --all git push new-origin --tags - (Optional) Set the new remote as default:
git remote rename origin old-origin git remote rename new-origin origin
内容的提问来源于stack exchange,提问作者Jonas S.
相关产品推荐
相关产品推荐

