Gitコマンドを集めてみました。

How to Use Git Branches: A Beginner’s Guide

Learn the essential Git branch commands for creating, switching, merging, and sharing branches safely.

Working with Git branches lets you develop features, fix bugs, or try ideas without touching your main branch.
When you’re done, merge your work back cleanly.

1) Check Your Current Branch

The * marks the branch you’re on.

git branch

2) Create a New Branch

Creates the branch but does not switch to it.

git branch new-feature

3) Switch Between Branches

Use classic checkout or modern switch:

git checkout new-feature
# or
git switch new-feature

4) Create & Switch in One Step

git checkout -b new-feature
# or
git switch -c new-feature

5) Merge Branches

  1. Go to the branch you want to merge into (usually main):
git checkout main
  1. Merge your feature branch:
git merge new-feature

6) Delete a Branch

After merging (or if you don’t need it):

git branch -d new-feature   # safe delete (refuses if unmerged)
git branch -D new-feature   # force delete

7) Work With Remote Branches

Share your branch via origin (e.g., GitHub/GitLab):

git push -u origin new-feature

List remote branches:

git fetch --all
git branch -r

Check out an existing remote branch (tracking will be set automatically in modern Git):

git checkout branch-name
# or
git switch branch-name

Example Workflow

# 1) Create and switch
git switch -c feature-login

# 2) Work & commit
git add .
git commit -m "Add login form"

# 3) Share to remote
git push -u origin feature-login

# 4) Open a Pull Request on your host (GitHub/GitLab/Bitbucket)

# 5) Merge and clean up
git checkout main
git merge feature-login
git branch -d feature-login
git push origin --delete feature-login   # optional: remove remote branch

Tip: Practice in a test repo before using on production projects.

Posted by

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です