Master Git in 30 Days

Day 25: Using GitHub Remote Repository - Concepts

Building on the basics from Day 24, this article explores important concepts and workflows when working with GitHub remote repositories.

Understanding Remote Tracking Branches

Remote tracking branches are references to the state of remote branches. They follow the naming pattern <remote>/<branch>:

# List all branches including remote tracking branches
git branch -a

# Example output:
# * main
#   feature
#   remotes/origin/main
#   remotes/origin/feature

Fetch vs Pull

Fetch

Downloads objects and refs from another repository but doesn’t merge:

git fetch origin

This updates your remote tracking branches but doesn’t change your working directory.

Pull

Fetch + Merge in one command:

git pull origin main
# Equivalent to:
git fetch origin
git merge origin/main

Or with rebase:

git pull --rebase origin main

Upstream Branches

Set up upstream tracking to simplify push/pull:

# Set upstream when pushing
git push -u origin feature-branch

# Now you can just use:
git push
git pull

Check upstream configuration:

git branch -vv

Tracking Remote Branches

Create Local Branch from Remote

# Option 1: Automatic tracking
git checkout feature-branch
# If feature-branch exists on remote, Git auto-creates and tracks it

# Option 2: Explicit tracking
git checkout -b feature-branch origin/feature-branch

# Option 3: Using track flag
git checkout --track origin/feature-branch

Untrack a Remote Branch

git branch --unset-upstream

Push with Different Branch Names

Push local branch to remote with different name:

git push origin local-branch:remote-branch

Deleting Remote Branches

Delete a remote branch:

# Method 1
git push origin --delete branch-name

# Method 2
git push origin :branch-name

Working with Multiple Remotes

You can have multiple remotes (e.g., origin and upstream):

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

# Fetch from upstream
git fetch upstream

# Merge upstream changes
git merge upstream/main

# Push to your origin
git push origin main

Understanding git push Behaviors

Different push modes:

# Push current branch to matching branch on remote
git config push.default simple  # Default in Git 2.0+

# Push all matching branches
git config push.default matching

# Push current branch to branch with same name
git config push.default current

# No push without explicit ref spec
git config push.default nothing

Force Push and Its Dangers

When to Force Push

  • You’ve rebased your feature branch
  • You’re the only one working on the branch
  • You need to fix a mistake in pushed commits

How to Force Push

# Standard force push (dangerous!)
git push -f origin feature-branch

# Safer: force push with lease
git push --force-with-lease origin feature-branch

--force-with-lease only succeeds if remote hasn’t changed since you last fetched.

Force Push Best Practices

  1. Never force push to main/master
  2. Communicate before force pushing shared branches
  3. Use –force-with-lease instead of -f
  4. Create backup branch before force pushing
  5. Only force push your own feature branches

Handling Diverged Branches

When local and remote have diverged:

# Check the situation
git status

# Option 1: Merge remote changes
git pull origin main

# Option 2: Rebase on remote changes
git pull --rebase origin main

# Option 3: Force push (if appropriate)
git push --force-with-lease origin feature-branch

Cleaning Up Remote Tracking References

Remove stale remote-tracking branches:

# See stale branches
git remote prune origin --dry-run

# Remove stale branches
git remote prune origin

# Or during fetch
git fetch --prune

Bare vs Non-Bare Repositories

Non-Bare (Working Repository)

  • Has working directory
  • Used for development
  • What you clone normally

Bare Repository

  • No working directory
  • Only Git data
  • Used on servers
# Create bare repository
git clone --bare project project.git

# Or init as bare
git init --bare project.git

Git Workflow Patterns

Centralized Workflow

Everyone works on main branch:

git pull origin main
# Make changes
git add .
git commit -m "Changes"
git push origin main

Feature Branch Workflow

Each feature gets its own branch:

git checkout -b feature-x
# Work on feature
git push origin feature-x
# Create pull request
# After merge, delete branch
git branch -d feature-x
git push origin --delete feature-x

Gitflow Workflow

Uses multiple long-lived branches:

  • main: Production-ready code
  • develop: Integration branch
  • feature/: Feature development
  • release/: Release preparation
  • hotfix/: Emergency fixes

Pull Request Workflow

  1. Fork the repository (for open source)
  2. Clone your fork
  3. Create feature branch
    git checkout -b feature-name
    
  4. Make changes and commit
  5. Push to your fork
    git push origin feature-name
    
  6. Create pull request on GitHub
  7. Discuss and review code
  8. Make requested changes
  9. Merge when approved

Keeping Fork Updated

# Add upstream remote (once)
git remote add upstream https://github.com/original/repo.git

# Fetch upstream changes
git fetch upstream

# Merge upstream/main into your main
git checkout main
git merge upstream/main

# Push to your fork
git push origin main

Resolving Conflicts in Pull Requests

# Update your branch with target branch
git fetch origin
git checkout feature-branch
git merge origin/main

# Resolve conflicts
# ... edit files ...
git add .
git commit -m "Resolve conflicts"

# Push updated branch
git push origin feature-branch

Today’s Summary

Understanding remote repository concepts is crucial for effective collaboration. Whether working on a team or contributing to open source, knowing how to manage remotes, track branches, and handle conflicts will make you a more effective Git user.

Let me reorganize the Git commands and parameters learned today:

  • git fetch origin
  • git pull origin main
  • git pull –rebase
  • git push -u origin branch
  • git push origin –delete branch
  • git push –force-with-lease
  • git branch -vv
  • git branch –unset-upstream
  • git remote prune origin
  • git fetch –prune
  • git clone –bare