$ git branch [-a] [-l "GLOB_PATTERN"]
shows all local branches in a repository.
also shows all removed branches.
restricts the branches to those that correspond to a specific pattern.
$ git branch --sort=-committerdate
sorts the branches according to the commit date.
You can also use
git config --global branch.sort -committerdateto make this setting your default setting.
$ git branch [BRANCH_NAME]
creates a new branch based on the current
$ git switch [-c] [BRANCH_NAME]
switches between branches.
creates a new branch.
In Git < 2.23,
git switchis not yet available. In this case you still need to use
$ git checkout [-b] [BRANCH_NAME]
changes the working directory to the specified branch.
creates the specified branch if it does not already exist.
$ git merge [FROM_BRANCH_NAME]
connects the given branch with the current branch you are currently in, for example:
$ git checkout main $ git merge hotfix Updating f42c576..3a0874c Fast forward setup.py | 1 - 1 files changed, 0 insertions(+), 1 deletions(-)
means that the new commit immediately followed the original commit and so the branch pointer only had to be continued.
In other cases the output can look like this:
$ git checkout main $ git merge '#42' Merge made by recursive. setup.py | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
is a merge strategy that is used when the merge is only to be done to
Occasionally, however, Git runs into issues with merging, such as:
$ git branch -d [BRANCH_NAME]
deletes the selected branch if it has already been transferred to another.
-dforcing the deletion.
So far, these examples have all shown local branches. However, the git branch command also works with remote branches. To work with remote branches, a remote repository must first be configured and added to the local repository configuration:
$ git remote add origin https://ce.cusy.io/veit/NEWREPO.git
Now the branch can also be added to the remote repository:
$ git push origin [BRANCH_NAME]
git branch -d you delete the branches locally only. To delete them on
the remote server as well, you can type the following:
$ git push origin --delete [BRANCH_NAME]
To remove remote branches locally, you can run
git fetch with the
-p option. You can also make this the default behaviour by
$ git config --global fetch.prune true
You can rename branches, for example with
$ git branch --move master main
This changes your local
master branch to
main. In order for others to
see the new branch, you must push it to the remote server. This will make the
main branch available on the remote server:
$ git push origin main
The current state of your repository may now look like this:
$ git branch -a * main remotes/origin/HEAD -> origin/master remotes/origin/main remotes/origin/master
masterbranch has disappeared because it has been replaced by the
mainbranch is also present on the remote computer.
masterbranch is also still present on the remote server. So presumably others will continue to use the the
masterbranch for their work until you make the following changes:
For all projects that depend on this project, the code and/or configuration must be updated.
The test-runner configuration files may need to be updated.
Build and release scripts need to be adjusted.
The settings on your repository server, such as the default branch of the repository, merge rules and others, need to be adjusted.
References to the old branch in the documentation need to be updated.
Any pull or merge requests that target the
masterbranch should be closed.
After you have done all these tasks and are sure that the
main branch works
the same as the
master branch, you can delete the
$ git push origin --delete master
Team members can delete their locally still existing references to the
master branch with
$ git fetch origin --prune