0
0

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入門】Gitでfeatureブランチを最新状態に保つ方法(他ブランチを汚さずにローカルで更新)

Posted at

🧩 概要

開発中のfeatureブランチが古くなり、
baseブランチ(例:develop)に多くの更新が入った場合、
最新の変更を取り込みたいけど、まだ本番にマージしたくないという状況があります。

この記事では、そんなときに安全にローカルでfeatureブランチを最新状態にする方法をまとめます。

🎯 状況例

・作業ブランチ:feature/progress-report
・ベースブランチ:develop

・状況:develop では多くの修正が進行中。
 でも feature/progress-report もその変更を取り込みたい。

 ⚠️ まだ未完成のタスクなので、productionにマージしたくない!

🛠 方法①: Merge(安全・履歴そのまま)

最も一般的で安全な方法。
develop の変更をローカルで feature/progress-report に取り込むだけです。

コマンド

qiita.rb
# 変更を保存
git status
git add -A && git commit -m "WIP: save before merge"

# 最新を取得
git fetch origin

# featureブランチへ移動
git checkout feature/progress-report

# baseブランチを取り込む
git merge origin/develop

# コンフリクト解決後
git add -A
git commit

# 結果を自分のブランチにpush(※baseブランチは変わらない!)
git push origin feature/progress-report

✅ ポイント

これで develop は一切変更されません。
pushするのは feature/progress-report だけなので安全。
コンフリクトが出たら、その場で修正してOK。

🧼 方法②: Rebase(履歴をきれいに保つ)

まだ他の人がそのブランチを使っていない場合はこちら。

コマンド

qiita.rb
git fetch origin
git checkout feature/progress-report
git rebase origin/develop

# コンフリクト解決
git add -A
git rebase --continue

## すでにpushしていた場合はforce push
git push --force-with-lease origin feature/progress-report

✅ ポイント

コミット履歴がきれいに並ぶ(ただしforce push注意)。
チームで共有していない個人作業ブランチ向け。

🧠 補足:SourceTreeでの操作

フェッチ (Fetch) をクリック

ブランチ feature/progress-report をチェックアウト

メニューから 「マージ」 を選択し、origin/develop を選ぶ

コンフリクトを解消 → コミット → プッシュ(※featureブランチにだけ!)

🎯 結論

方法 向いている場面 特徴
Merge チーム開発中・安全重視 baseブランチに影響なし、履歴が残る
Rebase 自分専用ブランチ・履歴整理したい 履歴きれい、force pushが必要

✍️ まとめ

ローカルで merge or rebase すれば、他ブランチに影響なし!
push先を間違えなければ、productionに反映されることはありません。
コンフリクトが発生したら焦らず解決 → add → commit。

💡 Pro Tip:

ブランチを安全に試すときは、feature/progress-report_merge-preview や feature/progress-report_sync-test などの一時ブランチを切るのもおすすめ。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?