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?

同じ名前のブランチを差し替える

Posted at

概要

productionブランチを更新するのではなく、
他のブランチを今後productionとして扱う、
ということをしたい。

付随要件

本番環境は旧productionの最新→新productionの最新へと切り替え、常にproduction最新を保つ

方法概要

本番機以外で、
旧productionを他の名前にリネームしてそれをリモートに反映し、
新productionをつくってそれをリモートに反映する

本番機で、
旧productionをリネームし、
新productionをリモートから取得する

詳細

前提
ここではブランチ名を以下とします。
いずれも必ずしも残す必要のないものですが、
節目をわかりやすくするためにしばらく残す方針としました。

  • 新たにproductionとするブランチは production_20250315_open
  • リモートに残す旧productionは production_20250315_closed
  • 本番機のローカルリポジトリに残る旧productionは production_20250315_closed_localonly
    • これは切り替え後に不具合があった場合は切り戻し対象
本番機以外で実施

# ---
# 新productionの元となるブランチを取得・最新化
# ---
b=production_20250315_open
git fetch origin $b
git switch $b
git pull origin $b

# ---
# 旧productionを取得・最新化
# ---
b=production
git fetch origin $b
git switch $b
git pull origin $b

# ---
# 旧productionをリネーム
# ---
# ブランチ名設定
b1=production
b2=production_20250315_closed
# 切り替え
git switch $b1
# ローカルの$b1を$b2に変更
git branch -m $b1 $b2
# リモートに$b2をプッシュ
git push origin $b2
# リモートの$b1を削除
git push origin --delete $b1

# ---
# 新productionを作成
# ---
b1=production_20250315_open
b2=production
# 切り替え
git switch $b1
# ローカルの$b1から$b2をチェックアウト
git checkout -b $b2
# リモートに$b2をプッシュ
git push origin $b2
本番機で実施
# productionであることを念のため確認
git status
# productionをリネームする
git branch -m production production_20250315_closed_localonly
# ローカルにproductionブランチがないことを確認
git branch | grep production
# 新productionを取得する
git fetch origin production
git switch production
git pull origin production
# 念のためログを見て新productionであることを判断する
git log -n 1

おわったあと
ローカルリポジトリにproductionを持ってる人は一旦消す。

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?