A Detailed Guide to Git Remote Repository Management

Remote repositories are the foundation of team collaboration. Through remote repositories, team members can share code, synchronize changes, and collaborate on development. Git supports multiple remote repositories, allowing for flexible configuration of different collaboration models.

1. Remote Repository Basics

(1) What Is a Remote Repository?

A remote repository is a version control repository hosted on a web server, used for:

100%
graph TB
    subgraph Local
        A[DeveloperALocal Warehouse]
        B[DeveloperBLocal Warehouse]
        C[DeveloperCLocal Warehouse]
    end
    
    subgraph Remote
        D[Remote Repository<br/>GitHub/GitLab]
    end
    
    A <-->|push/pull| D
    B <-->|push/pull| D
    C <-->|push/pull| D
    
    style D fill:#d4edda

(2) Types of Remote Repositories

Common remote repository hosting platforms:

(3) Remote Repository Name

By default, Git uses two names:


2. Viewing a Remote Repository

(1) View the remote repository name

▶ Example: List remote repositories

BASH
# View the name of the remote repository
git remote

# Output:
# origin

# View All Remote Repositories(IncludingURL)
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

(2) View remote repository details

▶ Example: View Details

BASH
# VieworiginDetails
git remote show origin

# Output:
# * remote origin
#   Fetch URL: https://github.com/user/repo.git
#   Push  URL: https://github.com/user/repo.git
#   HEAD branch: main
#   Remote branches:
#     main     tracked
#     develop  tracked
#     feature  new (next fetch will store in remotes/origin)
#   Local branches configured for 'git pull':
#     main     merges with remote main
#     develop  merges with remote develop
#   Local refs configured for 'git push':
#     main     pushes to main     (up to date)
#     develop  pushes to develop  (local out of date)

(3) Viewing Remote Branches

▶ Example: List remote branches

BASH
# View Remote Branches
git branch -r

# Output:
#   origin/HEAD -> origin/main
#   origin/main
#   origin/develop
#   origin/feature

# View All Branches(Local+Remote)
git branch -a

# Output:
# * main
#   develop
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
#   remotes/origin/develop
#   remotes/origin/feature

3. Add a Remote Repository

(1) Add a new remote repository

▶ Example: Adding a Remote Repository

BASH
# Add a Remote Repository
git remote add origin https://github.com/user/repo.git

# Add a second remote repository
git remote add upstream https://github.com/original/repo.git

# Add a named remote repository
git remote add backup git@backup-server.com:repo.git

# Verification: Added successfully
git remote -v

# Output:
# origin    https://github.com/user/repo.git (fetch)
# origin    https://github.com/user/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)
# backup    git@backup-server.com:repo.git (fetch)
# backup    git@backup-server.com:repo.git (push)

(2) Using Different fetch and push URLs

▶ Example: Configuring Different URLs

BASH
# Add a Remote Repository: different URLs for fetch and push
git remote add origin https://github.com/user/repo.git
git remote set-url --push origin git@github.com:user/repo.git

# View Configuration
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  git@github.com:user/repo.git (push)

# Common config: fetch via HTTPS, push via SSH

(3) Automatically add "origin" when cloning

▶ Example: Cloning a Repository

BASH
# Automatically add when cloning a repositoryorigin
git clone https://github.com/user/repo.git

# View Remote Repositories
git remote -v

# Output:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

# Specify a different name when cloning
git clone -o myremote https://github.com/user/repo.git

git remote -v

# Output:
# myremote  https://github.com/user/repo.git (fetch)
# myremote  https://github.com/user/repo.git (push)

4. Modifying the Remote Repository

(1) Renaming a Remote Repository

▶ Example: Renaming

BASH
# Rename a Remote Repository
git remote rename origin github

# Verification
git remote -v

# Output:
# github  https://github.com/user/repo.git (fetch)
# github  https://github.com/user/repo.git (push)

(2) Modify the remote repository URL

▶ Example: Modifying a URL

BASH
# Modify a Remote RepositoryURL
git remote set-url origin https://github.com/newuser/newrepo.git

# Editfetch URL
git remote set-url --delete origin https://github.com/user/repo.git
git remote set-url --add origin https://github.com/newuser/newrepo.git

# Editpush URL
git remote set-url --push origin git@github.com:user/repo.git

# Verify Changes
git remote -v

(3) Adding and Deleting URLs

▶ Example: Managing Multiple URLs

BASH
# Addfetch URL
git remote set-url --add origin https://backup.com/repo.git

# Addpush URL
git remote set-url --add --push origin https://backup.com/repo.git

# DeleteURL
git remote set-url --delete origin https://backup.com/repo.git

# View AllURL
git remote -v

5. Deleting a Remote Repository

(1) Remove a remote repository reference

▶ Example: Deleting a remote repository

BASH
# Delete a remote repository reference(Does not affect the remote repository itself)
git remote remove upstream

# Or userm
git remote rm backup

# Confirm Deletion
git remote

# Output:
# origin

(2) Clean up expired remote branch references

▶ Example: Cleaning up a remote branch

BASH
# Delete a remote branch(Branches on a remote repository)
git push origin --delete feature

# Clean up references to remote branches that have been deleted locally
git fetch -p

# Or
git remote prune origin

# View the branches that need to be cleaned up
git remote prune origin --dry-run

# Output:
# * origin/feature would be pruned

6. Advanced Operations on Remote Repositories

(1) Fork Workflow Configuration

▶ Example: Configuring a Fork Workflow

BASH
# 1. ForkFrom the original repository to your account

# 2. Clone YourselfFork
git clone https://github.com/yourname/repo.git

# 3. Add the original repository asupstream
git remote add upstream https://github.com/original/repo.git

# 4. View Remote Repositories
git remote -v

# Output:
# origin    https://github.com/yourname/repo.git (fetch)
# origin    https://github.com/yourname/repo.git (push)
# upstream  https://github.com/original/repo.git (fetch)
# upstream  https://github.com/original/repo.git (push)

# 5. Synchronize with upstream updates
git fetch upstream
git merge upstream/main

# 6. Push to your ownFork
git push origin main

(2) Workflow for Multiple Remote Repositories

100%
graph TB
    A[Local Warehouse] -->|push| B[GitHub<br/>origin]
    A -->|push| C[GitLab<br/>gitlab]
    A -->|push| D[Backup Server<br/>backup]
    
    B -->|pull| A
    C -->|pull| A
    
    style A fill:#fff3cd
    style B fill:#d4edda
    style C fill:#c3e6cb
    style D fill:#cce5ff

▶ Example: Configuring Multiple Remote Repositories

BASH
# Add Multiple Remote Repositories
git remote add origin https://github.com/user/repo.git
git remote add gitlab https://gitlab.com/user/repo.git
git remote add backup git@backup-server.com:repo.git

# Push to all remote repositories
git push origin main
git push gitlab main
git push backup main

# Or addremote.pushDefaultLayout
git config remote.pushDefault origin

# AddpushurlAutomatically Push to Multiple Repositories
git remote set-url --add --push origin https://github.com/user/repo.git
git remote set-url --add --push origin https://gitlab.com/user/repo.git

# Nowpush originIt will be pushed to both repositories at the same time
git push origin main

(3) View updates to the remote repository

▶ Example: Viewing Remote Updates

BASH
# Get Remote Updates(Do not merge)
git fetch origin

# View the latest status of a remote branch
git branch -r

# View RemotemainBranch Commits
git log origin/main

# Compare the differences between local and remote
git diff main origin/main

# View details of a remote branch
git show origin/feature

7. Configuring Remote Repositories

(1) Configure remote repository settings

▶ Example: Configuration Parameters

BASH
# View Remote Repository Configuration
git config --local --list | grep remote

# Output:
# remote.origin.url=https://github.com/user/repo.git
# remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

# LayoutfetchStrategy
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

# Layoutpush URL
git config remote.origin.pushurl git@github.com:user/repo.git

# Configure the default push branch
git config branch.main.remote origin
git config branch.main.merge refs/heads/main

(2) SSH vs. HTTPS

▶ Example: Switching Protocols

BASH
# UsageHTTPS
git remote set-url origin https://github.com/user/repo.git

# UsageSSH(Recommendations)
git remote set-url origin git@github.com:user/repo.git

# UsageGitAgreement
git remote set-url origin git://github.com/user/repo.git

# HTTPSYou must enter your password each time,Or use credential storage
git config --global credential.helper store

# SSHA key needs to be configured
ssh-keygen -t ed25519 -C "your_email@example.com"
# Add the public key toGitHub/GitLab

❓ FAQ

Q What is the difference between "origin" and "upstream"?
A "origin" is the repository you forked, and "upstream" is the original repository. In the fork workflow, you pull updates from "upstream," push them to "origin," and then create a pull request.
Q How do I push to multiple remote repositories at the same time?
A Use git remote set-url --add --push to add multiple push URLs, so that a single push will be sent to all configured URLs. Alternatively, you can manually push to each remote repository.
Q Does deleting a remote repository reference affect the remote repository?
A No. git remote remove This only deletes the local reference configuration; it does not affect the remote repository itself. The remote repository still exists; it’s just no longer tracked locally.
Q Which is better, HTTPS or SSH?
A SSH is more convenient; once you've set up a key, you don't need to enter a password every time. HTTPS is simpler—it doesn't require setting up a key—but you need to enter a password or use a credential store. We recommend using SSH.
Q How do I view the details of a remote repository?
A Use git remote show <name> to view detailed information, including the URL, tracked branches, configured pull/push rules, and more.

📖 Summary


📝 Exercises

  1. Basic Exercise: Create a local repository, add a GitHub remote repository, view the remote repository information, modify the remote repository URL, and practice basic remote repository management operations.

  2. Advanced Exercise: Simulate a fork workflow: Add two remote repositories, origin and upstream; pull updates from upstream; push them to origin; and understand the collaboration model of the fork workflow.

  3. Challenge: Configure automatic pushes to multiple remote repositories: Set up multiple push URLs for a single remote repository so that a single push is sent simultaneously to GitHub, GitLab, and a backup server.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏