HOWTO · Git

How to Create and Use Branches on Git

This tutorial introduces the Git branch. We will see how Git branches can help you organize your project.

On this page

To create a branch from your current commit, run git branch feature/login. This creates the branch but leaves you on the current branch. To create it and switch to it in one step, use git checkout -b feature/login. Git branches are lightweight pointers to commits, so either command starts the new branch at the current branch’s commit unless you specify another start point.

Create and inspect branches with git branch

Run git branch with no arguments to list local branches. Git places an asterisk before the branch currently checked out. The following example creates a temporary repository, adds one commit, creates a topic branch, and lists the local branches. The asterisk marks the branch checked out now. The shell removes the temporary repository when it exits.

set -eu
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
git -C "$tmp" init -q -b main
git -C "$tmp" config user.name Example
git -C "$tmp" config user.email example@example.com
printf 'starter\n' > "$tmp/README.md"
git -C "$tmp" add README.md
git -C "$tmp" commit -qm "Add starter file"
git -C "$tmp" branch feature/login
git -C "$tmp" branch
  feature/login
* main

Create a branch without switching by running git branch feature/login. To rename the current branch, use git branch -m new-name; to rename a different local branch, use git branch -m old-name new-name. The -m option renames a branch; it does not move a branch between locations.

For a merged local branch, git branch -d feature/login asks Git to delete it and refuses when Git considers it unmerged. Use git branch -D feature/login only when you intend to remove the local branch reference even though its commits may not be merged elsewhere. Deleting a branch this way does not erase commits that remain reachable through another reference, but commits reachable only from that branch can eventually be removed by Git’s cleanup.

These commands affect local branches. To delete a branch from the remote named origin, run git push origin --delete feature/login. That does not delete your local branch; remove it separately if needed.

Create and switch to a branch with git checkout -b

Use git checkout existing-branch to switch to a branch that already exists. Use git checkout -b feature/login to create a new branch at the current commit and switch to it immediately. If the name already exists, Git reports that it cannot create the branch; choose another name or switch to the existing branch.

Uncommitted edits stay in the working tree when you switch branches if they can be carried safely. Git does not commit those edits or save them exclusively onto the new branch. They remain uncommitted and can appear on the branch you switch to. If switching would overwrite a change, Git stops and asks you to resolve the conflict first. Commit the work or use git stash before switching when you need to keep it separate.

After switching, run git status to confirm the current branch and see which files remain modified or untracked. For example, after git checkout -b feature/login, git status should identify feature/login; any compatible edits still appear as working-tree changes until you commit, stash, or discard them.