Git Branches: Create, Switch, Rename and Delete Branches

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.

Advertisement

๐Ÿ“– 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:

TaskModernOlder
Create and switchgit switch -c namegit checkout -b name
Switchgit switch namegit checkout name
Create without switchinggit branch namegit branch name
Branch from a specific commit or branchgit switch -c hotfix maingit 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:

PrefixUseExample
feature/New functionality or testsfeature/checkout-api-tests
bugfix/ or fix/Bug fixesbugfix/flaky-login-wait
hotfix/Urgent production fixeshotfix/payment-timeout
chore/Maintenance, dependencies, configchore/upgrade-playwright
docs/Documentationdocs/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

  1. In git-practice, create feature/notes with git switch -c, make two commits, and view the graph with git log --oneline --graph --all.
  2. Switch back to main and confirm the new commits are not there.
  3. Push the branch to GitHub with -u and find it in the branch dropdown on GitHub.
  4. Rename it to feature/study-notes locally and on GitHub.
  5. Create a throwaway branch, commit something, delete it with -D, then recover it with git reflog.
  6. 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

๐ŸŽฌ Learn Git and GitHub step by step: watch the series on YouTube!

Subscribe to SqaToolsByDY • Practice on sqatools.in

1 thought on “Git Branches: Create, Switch, Rename and Delete Branches”

Leave a Comment