Create a new branch
A new branch (in the following example named V1.1
) is created with
git branch V1.1
This command does not change into the current branch. In order to work on the new branch, the branch must be checked out:
git checkout V1.1
git branch -b | git switch -c
A shortcut for both, creating a new branch and checking it out in one go, is to use the -b
flag on git checkout
:
$ git checkout -b tq84
Switched to a new branch 'tq84'
Alternatively, a new branch can also be created and made the current one with
$ git switch -c new-branch
Switched to a new branch 'new-branch'
Creating a branch does not change the state of the repository
Note that creating a branch and/or changing into it does not change the state of a repository. Before creating a new branch,
git status
reports a
clean working tree with
nothing to commit.
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
After creating a new branch (and switching into it), git status
still reports the same state (but does not say that the branch is up to date with the origin:
$ git checkout -b xyz
Switched to a new branch 'xyz'
$ git status
On branch xyz
nothing to commit, working tree clean
Show remote tracking branches
git branch -r
shows remote tracking branches:
$ git branch -r
origin/main
origin/V1.1
origin/fixes
andy/main
pter/tests
The returned list consists of the name («alias»)
remote repository separated by a slash (
/
) from the name of the branch in that remote repository.
Delete a branch
Delete a branch with merged changes (must be in another branch?)
git branch -d V1.1
Delete a branch with unmerged changes (must be in another branch)
git branch -D V1.1
Delete branch on remote origin
If the deleted branch already exists on the remote origin, it can be deleted with one of the following variants
git push origin :V1.1
git push origin -d V1.1
git push origin -D V1.1
Note that the
:V1.1
syntax refers to a
refspec.