0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:Git初学者のためのmainブランチトラブル回避術を整理してみた

Posted at

はじめに

Git 操作の中でもよく行う「ブランチの切り替え」について、自分用の備忘録としてまとめておきます。

書こうと思ったきっかけ

受講している IT スクールのハッカソンの開発の一環で、ブランチの管理や切り替えを何度も行う必要がありました。過去に main ブランチへの切り替え時に詰まった経験があったため、忘れないようにまとめておきます。

内容

main ブランチに切り替える基本のコマンド

git checkout main

ローカルに main ブランチがない場合(リモートから取得して切り替え)

git fetch origin
git checkout -b main origin/main

main ブランチを最新の状態に更新

git pull origin main

現在のブランチを確認

git branch

作業中の変更がある場合の対処

方法①:変更を一時的に退避(stash)する

git stash
git pull origin develop  # または main
# 必要なら変更を戻す
git stash pop

方法②:変更をコミットしてから pull

git add app/main.py
git commit -m "WIP: ローカル変更を一時コミット"
git pull origin develop  # または main

方法③:変更を破棄(注意)

git checkout -- app/main.py
git pull origin develop  # または main

まとめ

ブランチの切り替えは日常的に行う操作ですが、作業中の変更があるとエラーになることがあります。

変更を残すのか捨てるのかを意識しながら、適切な方法で切り替えるようにしていきたいです...!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?