Managing branches¶
Creating a new branch¶
In order to create a new branch at your current location, you can use the git branch command. You can also create a branch at a different location. Refer to the examples below:
# Create the branch 'development' at current location (HEAD)
git branch development
# Create the branch 'bugfix' at commit with hash 'ab245'
git branch bugfix ab245
When subsequently calling git branch without an argument, Git shows all your branches, along with the active one:
Switching to a different branch¶
In order to start writing commits into the new development branch, we first need to switch to it. In Git this is called 'checking out' and is accomplished using the git switch command. You can check out commits, as well as branches. But to keep things simple, we will stick to branches for now.
# Checkout (i.e. switch to) the 'development' branch
git switch development
# Or using `checkout`
git checkout branch
git switch or git checkout?
A lot of online resources will point you towards the git checkout command. This still works fine in most cases. There is, however, a lot going on under the hood that makes the two commands perform differently from each other.
Stick to git switch, as it is the recommended approach in newer Git versions, unless you need specific options that only git checkout provides.
Example¶
Below is a simple visualisation to demonstrate this process:
git init- A new repository is made.
mainis the default branch.
- A new repository is made.
- Two commits are made on the
mainbranch.- Therefore, twice, a file has been added/modified; staged with
git addand then committed withgit commit.
- Therefore, twice, a file has been added/modified; staged with
- A new branch called
developmentis made withgit branch development. - The
developmentbranch is switched to withgit switch development. - Three commits are made on the
development branch.- Therefore, three times, a file has been added/modified; staged with
git addand then committed withgit commit.
- Therefore, three times, a file has been added/modified; staged with
gitGraph
commit id: "main_1"
commit id: "main_2"
branch development
checkout development
commit id: "dev_1"
commit id: "dev_2"
commit id: "dev_3"
Tip
To create a new branch, and immediately check it out, you can use the following shorthand: git checkout -b <new_branch_name>:
To forcibly move a branch around, you can use: git branch -f <branch_name> <new_location>: