Branches let you work on features and fixes without breaking stable code. In this lesson you will create, switch, push, rename and delete branches, understand HEAD, and adopt naming conventions used by professional teams.
π What You’ll Learn: what branches and HEAD are, listing branches, creating and switching with git switch and git checkout, working and committing on a branch, pushing branches, renaming and deleting branches locally and on GitHub, and branch naming conventions.
πΏ What Is a Branch?
A branch is an independent line of work. You create one to build a feature, fix a bug or try an idea without touching the stable code on main. When the work is ready, you merge it back.
Technically a branch is just a lightweight, movable pointer to a commit. Creating one is instant, even in huge repositories, which is why Git users branch all the time.
C4 ββ C5 β feature/login-tests
/
C1 ββ C2 ββ C3 β main
β
HEAD points to the branch you are on
HEAD is Git’s marker for “where you are now”, usually the current branch.
π List Branches
git branch # local branches; * marks the current one
git branch -a # local and remote-tracking branches
git branch -v # with the latest commit of each
git branch -vv # plus upstream and ahead/behind information
feature/login-tests
* main
β Create and Switch Branches
Modern Git (2.23 and newer) has git switch, made just for branches:
git switch -c feature/login-tests # create a branch and switch to it
git switch main # switch to an existing branch
git switch - # switch back to the previous branch
The older, still common equivalents use git checkout:
| Task | Modern | Older |
|---|---|---|
| Create and switch | git switch -c name | git checkout -b name |
| Switch | git switch name | git checkout name |
| Create without switching | git branch name | git branch name |
| Branch from a specific commit or branch | git switch -c hotfix main | git checkout -b hotfix main |
π‘ Tip: Commit (or stash) your changes before switching branches. Git refuses to switch if your uncommitted changes would be overwritten.
π§ͺ Work on a Branch
git switch -c feature/login-tests
# edit tests/login.spec.ts
git add tests/login.spec.ts
git commit -m "Add login tests for locked-out users"
git log --oneline --graph --all
* 7e2f1a9 (HEAD -> feature/login-tests) Add login tests for locked-out users
* 8b1d4e2 (origin/main, main) Add learning goals and initial test plan
* 3f2a91c Add README with project title
main has not moved; your new commit exists only on the feature branch.
βοΈ Push a Branch to GitHub
git push -u origin feature/login-tests
GitHub then shows a Compare & pull request button. Opening a pull request is covered in Pull Requests Step by Step.
Work on a teammate’s branch:
git fetch
git switch feature/api-tests # creates a local branch tracking origin/feature/api-tests
βοΈ Rename a Branch
git branch -m new-name # rename the current branch
git branch -m old-name new-name # rename another branch
Rename a branch that is already on GitHub:
git branch -m feature/login feature/login-tests
git push origin -u feature/login-tests # push the new name
git push origin --delete feature/login # delete the old name on GitHub
ποΈ Delete a Branch
git branch -d feature/login-tests # delete if already merged (safe)
git branch -D experiment # force delete, even if not merged
git push origin --delete feature/login-tests # delete the branch on GitHub
git fetch --prune # clean up remote-tracking branches deleted on GitHub
β οΈ Common mistake: git branch -D deletes unmerged work. If you deleted the wrong branch, find its last commit with git reflog and recreate it with git branch name <hash>.
π·οΈ Branch Naming Conventions
Clear names make a repository easy to navigate. A common convention:
| Prefix | Use | Example |
|---|---|---|
feature/ | New functionality or tests | feature/checkout-api-tests |
bugfix/ or fix/ | Bug fixes | bugfix/flaky-login-wait |
hotfix/ | Urgent production fixes | hotfix/payment-timeout |
chore/ | Maintenance, dependencies, config | chore/upgrade-playwright |
docs/ | Documentation | docs/readme-setup |
release/ | Release preparation (Git Flow) | release/2.4.0 |
Use lowercase, hyphens instead of spaces, and add a ticket number if your team uses one: feature/QA-123-login-tests. Strategies such as Git Flow and trunk-based development are in branching strategies.
π Assignment
- In
git-practice, createfeature/noteswithgit switch -c, make two commits, and view the graph withgit log --oneline --graph --all. - Switch back to
mainand confirm the new commits are not there. - Push the branch to GitHub with
-uand find it in the branch dropdown on GitHub. - Rename it to
feature/study-noteslocally and on GitHub. - Create a throwaway branch, commit something, delete it with
-D, then recover it withgit reflog. - Delete the remote branch and clean up with
git fetch --prune.
β FAQ
π€ What is a branch in Git?
A movable pointer to a commit that lets you develop independently from other work. New commits on a branch do not affect other branches until you merge.
π€ How do I create a new branch in Git?
git switch -c branch-name (or git checkout -b branch-name) creates it and switches to it.
π€ What is the difference between git switch and git checkout?
git switch is a newer command only for changing branches. git checkout does that and also restores files, which made it confusing; both still work.
π€ How do I delete a branch locally and on GitHub?
git branch -d name locally (or -D to force) and git push origin --delete name on GitHub.
π€ How do I rename a Git branch?
git branch -m new-name for the current branch; for a pushed branch, push the new name and delete the old one on GitHub.
π― Next Steps
- π git merge: Fast-Forward and Three-Way Merges
- π§― Resolve Merge Conflicts Step by Step
- π Git and GitHub Tutorial: full course
π¬ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in