Lesson 2 — Git Branches¶
1. Learning objectives¶
By the end of this lesson, you should understand:
- What a branch represents.
- Why branches are important.
- The difference between local and remote branches.
- How to create, switch, rename, and delete branches.
- How to publish a branch to GitHub.
- How to inspect available branches.
- How to follow a feature-branch workflow.
2. What is a Git branch?¶
A branch is a movable reference to a commit.
It allows developers to work on changes independently without immediately modifying the main development branch.
For example:
The feature branch can contain new work while main remains unchanged.
Branches are useful for:
- New features.
- Bug fixes.
- Documentation.
- Refactoring.
- Experiments.
3. Understand the main branch¶
The main branch is commonly the primary branch of a repository.
It should contain stable, reviewed code.
You should generally avoid developing every feature directly on main.
Instead:
- Start from an updated
main. - Create a separate branch.
- Implement the change.
- Test the change.
- Open a pull request.
- Review and merge it.
4. Inspect branches¶
List local branches:
The current branch is marked with *.
List remote-tracking branches:
List all branches:
Show only the current branch:
5. Create a branch¶
Create a branch without switching to it:
Create and immediately switch to a branch:
The second command is generally the preferred modern workflow.
6. Switch branches¶
Switch to another existing branch:
Before switching, check whether you have uncommitted changes:
Git may prevent a switch if local changes would be overwritten.
7. Branch naming conventions¶
Use descriptive names that communicate the purpose of the branch.
Recommended prefixes:
| Prefix | Purpose | Example |
|---|---|---|
feature/ |
New functionality | feature/user-authentication |
fix/ |
Bug fixes | fix/login-validation |
docs/ |
Documentation | docs/api-guide |
refactor/ |
Code restructuring | refactor/auth-services |
test/ |
Tests | test/user-api |
chore/ |
Maintenance | chore/update-dependencies |
hotfix/ |
Urgent production fixes | hotfix/payment-error |
Use lowercase names and hyphens between words.
Avoid names such as:
Prefer:
8. Rename a branch¶
Rename the current branch:
Rename a different local branch:
If the old branch was already published, you may also need to update its remote branch.
For example:
Be careful when deleting a remote branch shared with other developers.
9. Publish a branch to GitHub¶
Create a branch:
Make a change and commit it:
Publish the branch:
After the upstream is configured, use:
10. Understand local and remote branches¶
A local branch exists in your local repository.
A remote-tracking branch represents the state of a branch on a remote repository as last observed by Git.
Example:
origin is the conventional name of the default remote.
Inspect remote URLs:
Download information about remote branches:
git fetch updates remote-tracking references without merging the changes into your current branch.
11. Delete branches¶
Delete a local branch that has been merged:
Force-delete a local branch:
The uppercase -D can delete a branch even if Git considers its changes unmerged. Use it carefully.
Delete a remote branch:
Deleting a local branch does not automatically delete its remote counterpart.
12. A practical feature-branch workflow¶
Start from main:
Create a feature branch:
Create or edit a file.
Inspect changes:
Stage and commit:
Push:
You can now create a pull request on GitHub.
13. Important commands reference¶
| Command | Purpose |
|---|---|
git branch |
List local branches |
git branch -r |
List remote-tracking branches |
git branch -a |
List all branches |
git branch --show-current |
Show current branch |
git branch NAME |
Create a branch |
git switch NAME |
Switch branches |
git switch -c NAME |
Create and switch |
git branch -m NAME |
Rename current branch |
git branch -d NAME |
Delete merged local branch |
git branch -D NAME |
Force-delete local branch |
git fetch origin |
Fetch remote updates |
git push -u origin NAME |
Publish and track a branch |
git push origin --delete NAME |
Delete remote branch |
14. Practical exercise¶
Create a documentation branch:
Create:
Write a short guide explaining:
- What a branch is.
- Why branches are useful.
- How to create a branch.
- How to switch branches.
- How to delete branches.
Commit:
Push:
Open GitHub and create a pull request to merge the documentation into main.
After the pull request is merged, synchronize your local repository:
Delete the local branch:
If GitHub has not automatically deleted the remote branch, delete it:
15. Completion checklist¶
- [ ] Understand what branches are.
- [ ] List local branches.
- [ ] List remote branches.
- [ ] Create a branch.
- [ ] Switch branches.
- [ ] Rename a branch.
- [ ] Publish a branch.
- [ ] Delete a local branch.
- [ ] Delete a remote branch.
- [ ] Complete a feature-branch workflow.
16. What comes next?¶
Lesson 3 covers commits, commit messages, Conventional Commits, commit history, inspecting previous changes, and safely correcting commits.