Skip to content

Branching — Module Recap

Module 03 is complete. You can now create, switch, merge, and delete branches — the full lifecycle of a feature branch. This is the workflow pattern you will use for the rest of your development career.

What branches are — A branch is a lightweight movable pointer to a commit. No code is copied. Creating a branch costs almost nothing. HEAD is a special pointer tracking your current position in the repository.

Creating and switching branchesgit switch -c <name> creates a branch and switches to it in one step. Switching branches updates your working tree to reflect that branch’s state. Git will not let you switch with uncommitted changes that would be overwritten.

Merging branches — Always merge into the branch you want to update. A fast-forward merge moves the pointer forward with no merge commit (linear history). A merge commit is created when both branches have diverged. Merge conflicts occur when the same region of a file was changed on both branches — resolve by editing the conflict markers, staging, and committing.

Branch naming conventions — Use a prefix/description pattern: feature/, fix/, docs/, refactor/, chore/. Lowercase, hyphenated, specific. GitHub Flow: main is always deployable, every change goes on a short-lived branch, merged via pull request.

Deleting merged branchesgit branch -d is safe (refuses to delete unmerged work). git branch -D is force delete. git branch --merged main lists what is safe to clean up. Delete the remote branch with git push origin --delete <name>.

CommandWhat it does
git branchList local branches (* marks current)
git branch <name>Create a branch at the current commit
git switch <name>Switch to an existing branch
git switch -c <name>Create and switch to a new branch
git merge <branch>Merge a branch into the current branch
git merge --abortAbort an in-progress merge and restore pre-merge state
git branch -d <name>Delete a merged branch (safe)
git branch -D <name>Force-delete a branch regardless of merge status
git branch --merged mainList branches fully merged into main
git branch --no-merged mainList branches with unmerged commits
git branch -aList local and remote branches
git push origin --delete <name>Delete a remote branch
git fetch --pruneRemove stale remote-tracking references

For solo or team development:

Terminal window
git switch -c feature/your-feature # 1. create and switch
# make changes, git add, git commit... # 2. develop
git switch main # 3. return to main
git merge feature/your-feature # 4. merge
git branch -d feature/your-feature # 5. delete the branch

This five-step cycle is the foundation of every workflow covered from here forward.

Module 04 covers the full range of undo operations in Git:

  • Discarding changes in your working tree that you have not staged
  • Unstaging files you added by mistake
  • Amending the last commit — fixing the message or adding forgotten files
  • git revert — creating a new commit that undoes a previous commit, safely, without rewriting history
  • git reset — rewinding the branch pointer to an earlier commit, understanding the three modes

Every developer needs these tools eventually. Knowing which one to reach for — and why — is the difference between a quick fix and a panic.