Working with Remote Branches in Git

Managing Remote Branches in Git

Inspecting Remote Repositories

Before working with remote branches, it's essential to understand how to inspect your remote connections.

List Remote Connections

To display all configured remote repository names:

$ git remote
origin

To show both the fetch and push URLs for each remote:

$ git remote -v
origin  https://github.com/yourusername/repository.git (fetch)
origin  https://github.com/yourusername/repository.git (push)

Detailed Remote Information

For comprehensive information about a specific remote:

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/yourusername/repository.git
  Push URL: https://github.com/yourusername/repository.git
  HEAD branch: main
  Remote branch:
    main tracked
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (fast-forwardable)

List Remote References

To explicitly view all remote references:

$ git ls-remote origin
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0        HEAD
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0        refs/heads/main

Managing Remote Connections

Add a Remote Repository

To add a new remote repository connection:

$ git remote add collaborator https://github.com/colleaguename/project.git

You can now use the short name "collaborator" instead of the full URL:

$ git fetch collaborator

Rename a Remote

To rename an existing remote repository:

$ git remote rename collaborator teammate

This will also update all remote branch references. For example, what was previosuly collaborator/main will now be teammate/main.

Remove a Remote

To delete a remmote repository connection:

$ git remote remove teammate

Working with Remote Branches

Fetch Remote Changes

To retrieve updates from a remote repository without merging:

$ git fetch upstream
$ git fetch

This command donwloads all data from the specified remote but doesn't modify your working files. After fetching, you'll have references to all remote branches that you can merge or inspect.

Pull Remote Changes

To fetch and merge changes from a remote branch:

$ git pull upstream main:main

To merge with your current branch:

$ git pull upstream main

Prune Stale References

To remove outdated remote-tracking references:

$ git fetch -p
$ git fetch --prune

Push Local Branches

To push your current branch to its remote counterpart:

$ git push upstream main

To push a local branch to a differently named remote branch:

$ git push upstream feature-branch:production-feature

To set a default remote for future pushes:

$ git push -u upstream main

Create Remote Branches

Method 1: Local Branch First

$ git checkout -b new-feature
$ git push upstream new-feature
$ git pull upstream new-feature

Method 2: Direct Remote Push

$ git push upstream main:remote-feature

This creates a remote branch without a local counterpart.

View Remote Branches

To list all remote branches:

$ git branch -r
  upstream/main
  upstream/new-feature
  upstream/experimental

Delete Remote Branches

To remove a remote branch:

$ git push upstream -d new-feature
$ git push upstream --delete experimental

After deleting remote branches, prune your local references:

$ git fetch -p
$ git fetch --prune

Reset Remote Branches

To revert a remote branch to a previous commit:

$ git reflog

Find the desired commit ID and reset your local branch:

$ git reset --hard 5f8a3b2c

Force push to update the remote branch:

$ git push -f upstream main

Use force push with caution as it overwrites the remote history.

Tags: git-remote branch-management version-control Collaboration distributed-version-control

Posted on Mon, 21 Sep 2026 16:03:06 +0000 by ukspudnie