How to Create a Branch in Git
A Git branch gives you a separate line of work for a feature, fix, or experiment without changing another branch. Follow these commands to create a branch, switch to it, and publish it to a remote repository.
Practice Git workflows →Create and switch to a branch in one command
The modern command git switch -c creates a branch and checks it out immediately:
git switch -c feature/profile-pageChoose a short name that describes the work. Use lowercase words separated by hyphens, and check your team’s naming convention if it has one. The new branch starts from the commit currently checked out.
Create a branch without switching to it
To create a branch but remain on the current one, use git branch:
git branch feature/profile-pageThen switch to it with git switch feature/profile-page. To see local branches and identify the current one, run git branch; the current branch has an asterisk beside its name.
The older git checkout -b command
You may see tutorials use git checkout -b to create and switch branches:
git checkout -b feature/profile-pageThis still works. Newer Git versions provide git switch specifically for changing branches, which can make its purpose clearer. Use whichever form matches your project’s documentation and Git version.
Push a new branch to a remote
A branch created locally is not automatically available on GitHub or another remote. Push it and set its upstream with:
git push -u origin feature/profile-pageThe -u option records the upstream branch, so later pushes and pulls can use the configured remote and branch without repeating both names. Replace origin if your remote uses a different name.
Before you create a branch
- Run
git statusto see whether you have uncommitted changes. New branches start from the current commit, and uncommitted changes may remain in your working tree. - If the branch should start from the latest
main, switch to it and update it according to your team’s workflow before creating the feature branch. - Keep the branch focused on one task, and commit related changes together as you make progress.
Git branch FAQ
What is the difference between git branch and git switch -c?
git branch name creates the branch but leaves you where you are. git switch -c name creates the branch and switches to it.
Does creating a Git branch copy all the project files?
No. A branch is a lightweight reference to a commit. Git updates your checked-out files when you switch branches; it does not duplicate the whole repository for each branch.