Git Branching Guide

This page explains local branches, remote branches, feature branching, merge flow, and how to tell whether you are working locally or against a remote such as GitHub.

Quick Summary

Use this first if you want the short version.

Local branch

A branch like main or feature/login in your own clone on disk.

Remote branch

A branch stored on GitHub, GitLab, or another server, usually shown locally as origin/main.

Feature branching

Create one branch per change, commit there, then merge that branch back.

Safe beginner flow

Update main, create branch, commit locally, push branch, merge branch, pull on other machines.

1. The Basic Idea

A branch is a named line of work. It lets you change code without immediately changing another branch.

In most projects, the stable branch is main or master. A feature branch is where you do one specific task, for example feature/navbar or bugfix/login-error.

2. Check Where You Are

Before you create, merge, or push branches, check your current state.

git status
git branch --show-current
git branch -vv
git remote -v
git status

Shows your current branch and whether you have uncommitted local changes.

git branch --show-current

Shows only the current local branch name.

git branch -vv

Shows whether your local branch tracks a remote branch such as origin/main.

git remote -v

Shows which remote server URL your repository is linked to.

Before switching branches or merging, keep the working tree clean when possible. That means commit your changes or stash them deliberately.

3. Branch Types

These names are conventions. Git itself does not enforce them, but teams use them for clarity.

main or master

The primary branch. Usually the branch you deploy from or protect the most.

feature/...

New work. Example: feature/left-sidebar. This is the normal branch type for one task.

bugfix/...

Fixes a defect without mixing in unrelated work.

hotfix/...

Urgent production fix, usually merged back quickly.

release/...

Optional staging branch used by some teams before a release.

experiment/...

Temporary branch for testing ideas you may or may not keep.

Feature branching means one branch for one piece of work. That keeps changes isolated and makes merging easier.

4. Create a Branch

Start from the base branch you want to branch from, usually main.

git switch main
git pull origin main
git switch -c feature/my-change

This means: move to main, update it from the remote, then create a new local feature branch from that point.

Older tutorials use git checkout -b feature/my-change. It still works. git switch -c is clearer.

5. Work and Commit Locally

Git work starts locally. When you edit files in your project folder, those changes are local until you commit and push them.

git status
git diff
git add .
git commit -m "Add fixed left sidebar navigation"

A commit is still local. It exists only in your clone until you push it to a remote.

6. Local vs Remote

This is one of the most important Git distinctions:

Thing What it means
main Your local branch in the repository on your machine.
origin/main Your local record of what Git last knows about the remote main branch.
Remote server The actual repository on GitHub, GitLab, or another hosted service.

You do not work directly inside origin/main. You work on a local branch such as main or feature/my-change.

Simple rule: if you edited files in your folder, you are working locally. If you want the server to receive those changes, you must push.

How to know if you are local or synced with remote

git status
git branch -vv
git fetch origin
git status

After git fetch origin, Git updates its knowledge of the server. Then git status and git branch -vv become more reliable for ahead/behind information.

7. Push a Branch

After committing locally, push the branch to the remote server.

git push -u origin feature/my-change

The first push uses -u to connect your local branch to its remote branch. After that, while you stay on the same branch, you can usually use:

git push
git push does not push every branch. It pushes the current local branch to the remote branch linked to it.

How to check what branch will be pushed

git branch -vv

If you see [origin/feature/my-change], your local branch is linked to that remote branch.

8. Merge a Feature Branch

Merging means bringing the full content of one branch into another branch.

Feature branching flow

  1. Start from main.
  2. Create feature/....
  3. Commit all work on that feature branch.
  4. Return to main.
  5. Merge the feature branch into main.
git switch main
git pull origin main
git merge feature/my-change
git push origin main

Read the merge command literally: "I am on main, and I want to bring feature/my-change into this branch."

Merge works best when the feature branch contains one coherent task. That is the main reason feature branching is useful.

9. Merge Conflicts

A conflict happens when Git cannot decide which version of a changed section to keep.

git status

You may see markers like this:

<<<<<<< HEAD
code from current branch
=======
code from merged branch
>>>>>>> feature/my-change

Resolve the file, then continue:

git add path/to/file
git commit

To cancel the merge:

git merge --abort

10. Fetch vs Pull

Command What it does
git fetch Downloads remote updates into remote-tracking branches like origin/main, but does not merge them into your current branch.
git pull Fetches first, then merges or rebases those remote updates into your current local branch.
git fetch origin
git log --oneline origin/main
git merge origin/main

That is the manual version. The shortcut is:

git pull origin main
Use fetch when you want to inspect remote changes first. Use pull when you are ready to bring them into your current branch.

11. Remote-Tracking Branches

A name like origin/main is not the server itself. It is your local snapshot of what Git last fetched from the server.

git branch -r
git branch -a
git log --oneline origin/main

If someone else pushes to GitHub, your machine does not know immediately. You must run git fetch or git pull to update your local knowledge.

12. If Codex Works on a Remote

In normal Git usage, work should still land in a branch somewhere. The question is where the commit is created and where you later pull it from.

Case A: Codex edits your local repository

This is the simple case. Codex changes files in your local clone. You review, commit, and push from your machine.

Case B: Codex commits to a remote branch

In that case the remote branch moves first, and your local clone becomes behind that remote branch until you fetch or pull.

git fetch origin
git switch your-branch
git pull origin your-branch

If the branch does not exist locally yet:

git fetch origin
git switch -c your-branch --track origin/your-branch
So if Codex works on the remote first, the mechanism is: remote branch changes, then your local clone fetches that branch, then your local branch pulls or tracks it.

What happens in practice

  1. Remote branch receives new commits.
  2. Your local clone still has old state.
  3. git fetch updates origin/....
  4. git pull or git merge origin/... brings those commits into your local branch.

13. Full Example You Can Run

This example assumes the project already has a remote named origin and a stable branch named main.

# Check current state
git status
git branch -vv

# Update local main from the server
git switch main
git pull origin main

# Create a feature branch
git switch -c feature/left-panel

# Work on files, then review
git status
git diff

# Commit locally
git add .
git commit -m "Add fixed left panel navigation to Git lesson"

# Push the feature branch to the remote
git push -u origin feature/left-panel

# Later, merge it back into main
git switch main
git pull origin main
git merge feature/left-panel
git push origin main

If the remote was changed first by someone else

# Update your knowledge of the remote
git fetch origin

# See whether your local branch is behind
git branch -vv

# Bring remote changes into local main
git switch main
git pull origin main

14. Delete a Branch

Deleting a branch locally and deleting a branch remotely are two different operations.

Action Command What it affects
Delete local branch git branch -d feature/my-change Removes only your local branch.
Force delete local branch git branch -D feature/my-change Removes only your local branch, even if it was not merged.
Delete remote branch git push origin --delete feature/my-change Removes the branch from the remote server.

Delete only the local branch

git branch -d feature/my-change

Use -d when the branch was already merged. Git will stop you if the branch still contains unmerged work.

Force delete only the local branch

git branch -D feature/my-change

Use -D only when you are sure you do not need the unmerged work anymore.

Delete only the remote branch

git push origin --delete feature/my-change

This deletes the branch on GitHub or another remote, but it does not automatically remove your local branch.

If you deleted the branch remotely

Your local repository may still show old remote-tracking information until you fetch and prune.

git fetch --prune origin
git branch -r

If you still have the local branch and want to remove it too:

git branch -d feature/my-change

If you deleted the branch locally

The remote branch still exists until you delete it from the server.

git push origin --delete feature/my-change
Deleting a branch in one place does not delete it everywhere. Local branch deletion and remote branch deletion are separate steps.

15. Cheat Sheet

Command Meaning
git status Show current branch and working tree state.
git branch --show-current Show current local branch.
git branch -vv Show branch tracking and ahead/behind status.
git remote -v Show configured remotes.
git switch -c feature/x Create and switch to a new local branch.
git push -u origin feature/x Create or update the remote branch and set upstream tracking.
git fetch origin Download remote updates without merging them.
git pull origin main Fetch and merge remote main into local main.
git merge feature/x Bring the whole feature branch into the current branch.
git branch -r List remote-tracking branches.
git branch -d feature/x Delete the local branch if it was already merged.
git branch -D feature/x Force delete the local branch even if it was not merged.
git push origin --delete feature/x Delete the branch on the remote server.
git fetch --prune origin Refresh remote-tracking branches after a remote branch was deleted.