:bulb: This note describes how to create and switch branches in a remote repository cloned to a local environment with git clone.

[01] List All Branches

1-1. List branches in the cloned repository

1
2
3
4
5
git branch

# example
D:\githubblog\flask-app> git branch
* main

1-2. List all branches (including remote)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
git branch -a

# example
D:\githubblog\flask-app> git branch -a
PS D:\githubblog\flask-pybo> git branch -a
* main
  remotes/origin/2-04
  remotes/origin/2-05
  remotes/origin/2-06
  remotes/origin/2-07
  remotes/origin/2-08
  remotes/origin/2-09
  remotes/origin/2-10
  remotes/origin/3-01
  remotes/origin/3-02
  remotes/origin/3-03
  remotes/origin/3-04
  remotes/origin/3-05
  remotes/origin/3-06
  remotes/origin/3-07
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

:bulb: After cloning a remote repository, git branch alone does not show all branches. Use git branch -a instead.

[02] Create and Switch Branches

2-1. Basic method (two commands)

1
2
3
4
5
6
7
8
9
10
11
12
git branch ${branch_name}
git checkout ${branch_name}

# example
D:\githubblog\flask-app> git branch
* main
D:\githubblog\flask-app> git branch test-branch-01
D:\githubblog\flask-app> git checkout test-branch-01
Switched to branch 'test-branch-01'
PS D:\githubblog\flask-app> git branch
  main
* test-branch-01

2-2. Create and switch in a single command

1
2
3
4
5
6
7
8
9
10
11
12
git checkout -b ${branchname}

# example
D:\githubblog\flask-pybo> git branch
  main
* test-branch-01
D:\githubblog\flask-pybo> git checkout -b test-branch-02
Switched to a new branch 'test-branch-02'
D:\githubblog\flask-pybo> git branch
  main
  test-branch-01
* test-branch-02

[03] Merge Branch Work Back into main

1
2
3
4
5
6
7
# commit the work
git add *
git commit -m "comment"

git push
git checkout main
git merge ${branchname}