Remote branch = A branch that exists on the remote server (GitHub, GitLab, etc.).
Key concept:
Analogy: It is like having a local copy of the library book.
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 whatResult: Your local feature-login branch follows origin/feature-login.
Retrieve updates without merging:
git fetch origin # Download the changes
git status # See if your branch is behind
git log --oneline origin/main # See the new remote commitsRetrieve AND merge:
git pull origin main # = git fetch + git merge
# Or for your tracked branch:
git pull # SimplerPush your changes:
git push # If the branch is tracked
git push origin feature-login # ExplicitSee all branches (local + remote):
git branch -a # All branches
git branch -r # Remotes only
git remote show origin # Remote detailsDelete a remote branch:
git push origin --delete feature-old # Delete on the server
git branch -d feature-old # Delete locally
git fetch --prune # Clean up referencesCreate a local branch from a remote:
git checkout -b local-name origin/remote-name
# Or more simply:
git checkout remote-name # Automatically creates the tracking| Command | Action |
|---|---|
git fetch | Retrieve updates without merging |
git pull | Retrieve + merge |
git push -u origin <branch> | Push + create tracking |
git branch -vv | See the tracking status |
git remote show origin | Detailed remote info |
git push origin --delete <branch> | Delete a remote branch |
git fetch --prune | Clean up dead references |
Remote branches = Team coordination. Master this = smooth work!