Branches and Parallel Development
Learn how Git branches work, create feature branches, switch safely, track remote branches, and structure parallel development.
How Git Branches Work
A Git branch is essentially a movable reference pointing to a commit. Creating a branch is cheap because Git does not duplicate the entire project. As new commits are created, the branch reference advances.
The special reference HEAD identifies the currently checked-out position.
git branch feature/login
git switch feature/login
git switch -c feature/profilegit branch creates a branch without switching to it. git switch changes the current branch, and git switch -c creates and checks out a new branch.- Branches are lightweight references to commits.
- Feature branches isolate work from the main line.
git switch -cis a convenient modern command for creating and switching.
What does a Git branch primarily represent?
git switch and git checkout
git checkout historically handled both branch switching and file restoration, which can make commands ambiguous. Modern Git provides git switch for branch operations and git restore for file restoration.
checkout remains valid and is widely encountered in existing projects, but switch and restore communicate intent more clearly.
git switch main
git switch -c feature/search
git branch --show-currentgit switchis focused on branch changes.git restoreis focused on restoring files.git checkoutis still valid but has multiple historical responsibilities.
Which modern command is specifically designed for switching branches?
Tracking Branches
A local branch can track a remote branch. This relationship allows commands such as git pull and git push to infer the upstream branch. You can configure it explicitly with git push -u origin feature/login.
Tracking information is metadata about branch relationships; it does not copy commits by itself.
git switch -c feature/login
git push -u origin feature/login
git branch -vv-u option establishes the remote branch as the upstream for the local branch. git branch -vv displays tracking relationships.- Upstream configuration connects a local branch with a remote-tracking branch.
git push -ucommonly establishes the relationship during the first push.
What does `git push -u origin feature/login` commonly configure?
Listing, Renaming, and Deleting Branches
Git provides commands for managing branch references. Local branches can be listed with git branch, renamed with git branch -m, and deleted with git branch -d. Remote branches can be removed using git push origin --delete branch-name.
Deleting a branch does not automatically delete commits if those commits are still reachable through another reference.
git branch
git branch -a
git branch -m old-name new-name
git branch -d feature/login
git push origin --delete feature/login- Use
-dfor safe deletion of merged branches. - Use
-Donly when intentionally forcing deletion. - Remote branch deletion is a push operation.
Which command safely deletes a merged local branch?