Changing the Repository Source Address in Git on Linux
There are several methods to change the Git repository source address in a Linux system. Below are the detailed steps and explanations:
Method 1: Using the git remote command (Recommended)
1. Check Current Remote Repository Information
# Enter the project directory
cd /path/to/your/project
# Check current remote repository information
git remote -v
Example output:
origin https://old-repository.com/username/project.git (fetch)
origin https://old-repository.com/username/project.git (push)
2. Change the Remote Repository URL
Method A: Directly Modify the URL
# Change the URL of origin to the new address
git remote set-url origin https://new-repository.com/username/project.git
Method B: Remove and Then Add
# Remove the old origin
git remote remove origin
# Add the new origin
git remote add origin https://new-repository.com/username/project.git
3. Verify Changes
git remote -v
Method 2: Directly Modify the Configuration File
1. Edit the Git Configuration File
# Open the project's git configuration file
vim .git/config
2. Modify the URL Field
Find the <span>[remote "origin"]</span> section and modify the url field:
[remote "origin"]
url = https://new-repository.com/username/project.git
fetch = +refs/heads/*:refs/remotes/origin/*
Method 3: Change Using SSH Protocol
If switching from HTTPS to SSH protocol:
# Switch from HTTPS to SSH
git remote set-url origin [email protected]:username/project.git
Complete Example Script
#!/bin/bash
# Script to change Git repository source address
echo "=== Git Repository Source Address Change Tool ==="
# Enter the project directory
PROJECT_DIR="/path/to/your/project"
cd "$PROJECT_DIR" || exit 1
echo "Current remote repository information:"
git remote -v
echo -e "\nPlease enter the new repository address:"
read -r NEW_URL
# Confirm operation
echo "About to change to: $NEW_URL"
read -p "Confirm change? (y/N): " CONFIRM
if [[ "$CONFIRM" == "y" || "$CONFIRM" == "Y" ]]; then
# Execute change
git remote set-url origin "$NEW_URL"
echo "Change successful!"
echo -e "\nUpdated remote repository information:"
git remote -v
else
echo "Operation canceled"
fi
Examples of Different Protocol Formats
| Protocol Type | Example Format |
| HTTPS | <span>https://github.com/username/repo.git</span> |
| SSH | <span>[email protected]:username/repo.git</span> |
| Git | <span>git://github.com/username/repo.git</span> |
Common Issues and Solutions
1. Permission Issues
# If you encounter permission errors, check directory permissions
chmod -R 755 .git
2. Verify Connection
# Test SSH connection
ssh -T [email protected]
# Test new remote repository connection
git remote show origin
3. Push Test
# Test pushing to the new repository
git push -u origin main
Notes
- 1. Backup Important Data: Ensure local code is committed before changing the repository address
- 2. Branch Tracking: After changing, you may need to reset upstream branch tracking
- 3. Multiple Remote Repositories: You can add multiple remote repositories instead of replacing
git remote add new-origin https://new-repository.com/project.git - 4. Submodule Handling: If the project contains submodules, you need to update the remote addresses of the submodules separately