git branch
The command to create, delete or list branches.
Creating a new branch
Note: A newly created branch won’t be switched to automatically.
From the current branch
By only providing a name, git branch
will use your current branch as the starting point when creating a new branch.
$ git branch <name>
From a specific starting point
If you want the new branch to use an existing branch or tag as its starting point you can pass it in as a second argument.
$ git branch <name> <branch|tag>
Example
You’re currently on the bugfix
branch, however you want to create a new feature
branch using master
as the starting point.
Rather than running the following:
$ git checkout master
$ git branch feature
You can simply run the following instead:
$ git branch feature master
Deleting an existing branch
Note: Using these commands will only delete the branch in your local copy of the repository. Learn how to delete a remote Git branch.
Safely using the -d
flag
The branch will only be deleted if either:
- The branch has been fully merged with its corresponding remote branch.
- In HEAD if no remote branch has been set.
$ git branch -d <branch>
Unsafely using the -D
flag
$ git branch -D <branch>