Git Remote Branches

1 min

Table of contents

  1. What Is a Remote Branch?
  2. Branch Tracking
  3. Synchronization
  4. Managing Remote Branches
  5. Essential Commands

1. What Is a Remote Branch?

Remote branch = A branch that exists on the remote server (GitHub, GitLab, etc.).

Key concept:

  • Local branch: on your PC only
  • Remote branch: on the shared server
  • Remote-tracking branch: local copy of the remote branch

Analogy: It is like having a local copy of the library book.

Back to table of contents

2. Branch Tracking

bash
mkdir demo-remote && cd demo-remote
git init && echo "print('main')" > app.py
git add . && git commit -m "Initial commit"

# Add remote (simulates GitHub)
git remote add origin https://github.com/user/repo.git

# Create a branch that tracks a remote
git checkout -b feature-login
echo "print('login')" > login.py
git add . && git commit -m "Add login"

# Push AND create the tracking
git push -u origin feature-login

# Check the tracking
git branch -vv    # See which branches track what

Result: Your local feature-login branch follows origin/feature-login.

Back to table of contents

3. Synchronization

Retrieve updates without merging:

bash
git fetch origin                 # Download the changes
git status                       # See if your branch is behind
git log --oneline origin/main    # See the new remote commits

Retrieve AND merge:

bash
git pull origin main             # = git fetch + git merge
# Or for your tracked branch:
git pull                         # Simpler

Push your changes:

bash
git push                         # If the branch is tracked
git push origin feature-login    # Explicit

Back to table of contents

4. Managing Remote Branches

See all branches (local + remote):

bash
git branch -a                    # All branches
git branch -r                    # Remotes only
git remote show origin           # Remote details

Delete a remote branch:

bash
git push origin --delete feature-old    # Delete on the server
git branch -d feature-old               # Delete locally
git fetch --prune                       # Clean up references

Create a local branch from a remote:

bash
git checkout -b local-name origin/remote-name
# Or more simply:
git checkout remote-name         # Automatically creates the tracking

Back to table of contents

5. Essential Commands

CommandAction
git fetchRetrieve updates without merging
git pullRetrieve + merge
git push -u origin <branch>Push + create tracking
git branch -vvSee the tracking status
git remote show originDetailed remote info
git push origin --delete <branch>Delete a remote branch
git fetch --pruneClean up dead references

Remote branches = Team coordination. Master this = smooth work!

Back to table of contents