Skip to content

Branch ​

  • A branch is an important concept in Git that splits the flow of code management.
  • The word itself means "a tree branch," and Git's commit history really does look like a tree that splits into many branches from its root.

Why Use Branches ​

  • Branches are used to create and manage different versions, such as a free version and a paid version, within a single project.
  • Instead of creating two separate projects, you can split the workflow with branches inside one codebase.

Characteristics of Branches ​

  • Work done on each branch is independent of the others.
  • For example, even if you modify a file and commit on the premium branch, it doesn't affect the master branch.
  • You can switch between branches and manage each version separately.

Branch Commands ​

Check the Current Branch ​

bash
git status

→ The branch currently in use is shown, like On branch master.

List Branches ​

bash
git branch

→ The branch you're currently on is marked with *.

Create a Branch ​

bash
git branch branchname

→ Example:

bash
git branch premium

(Example: creating a branch for work on the paid version)

Delete a Branch ​

bash
git branch -d test
  • To delete a branch, use git branch -d branchname.
  • Here, d is short for delete.

Switch Branches ​

bash
git checkout branchname
  • To move to an existing branch, use git checkout branchname.

→ Example:

bash
git checkout premium
(moves to the premium branch)

Create a Branch and Switch to It Right Away ​

bash
git checkout -b test
  • To create a branch and move to it right away, use git checkout -b branchname.

Add Files and Commit ​

bash
git add .
git commit -m "commit message"

Summary ​

  • Branch = a tree branch that splits the flow of code management. Work on each branch is independent
  • git branch name creates / git branch -d name deletes
  • git checkout name switches / git checkout -b name creates and switches right away

Reference: personal Notion study notes (Git study)