Contents
gitのtagにバージョンを入れて管理し、masterを切り戻したくなった時に古いtagからmasterの切り戻しができないか検証してみました。
これをやりたかった背景としては、テキスト形式のconfigのバージョン管理をしたかったため。
やりたいこと
- gitのtagでバージョン管理をしたい
- master(リモートブランチ含む)の切り戻しを古いtagから実施したい
やってみたこと
- tagにバージョンを入れて管理する
- 切り戻したいtagのブランチを作成
- ローカルのmasterを一旦削除
- tagのブランチをmasterへリネーム
- リモートのmasterへ強制上書き
- リモートのmaster(GitLabやGitHub)はデフォルトでプロテクトがかかっているので解除する必要があります
Gitホスティングサービス
運用のイメージ
初期同期
通常運用
tagからの切り戻し
検証手順
事前準備
- GitLabに
repo
というリポジトリを作成しておく
初期同期
(1) リポジトリを作成します。
1 2 3 4 |
[root@localhost ~]# mkdir repo [root@localhost ~]# cd repo/ [root@localhost repo]# git init |
(2) remoteの設定をします。
1 2 |
[root@localhost repo]# git remote add https://user:pass@gitlab.com/username/repo.git |
(3) テスト用のconfigファイルを作成します。
1 2 3 4 |
[root@localhost repo]# echo 'param1' > config [root@localhost repo]# cat config param1 |
(4) commitしてtagを付けてgitlabにpushします。
1 2 3 4 5 6 |
[root@localhost repo]# git add . [root@localhost repo]# git commit -m '[add]config' [root@localhost repo]# git tag -a v1.0 -m 'version 1.0' [root@localhost repo]# git push gitlab --all [root@localhost repo]# git push gitlab --tags |
GitLab設定
(1) Settings
の Repository
をクリックします。
(2) Protected Branches
の Expand
をクリックします。
(3) master
の Unprotect
をクリックしてプロテクトを解除します。
通常運用
(1) リポジトリをクローンします。
1 2 |
[root@localhost ~]# git clone https://gitlab.com/username/repo.git |
(2) 設定ファイルを修正します。
1 2 3 4 5 6 |
[root@localhost ~]# cd repo/ [root@localhost repo]# echo param2 >> config [root@localhost repo]# cat config param1 param2 |
(3) 必要に応じてremoteの設定をします。
(4) commitしてtagを付けてgitlabにpushします。(ここでは検証なのでmasterにマージします)
1 2 3 4 5 6 |
[root@localhost repo]# git add . [root@localhost repo]# git commit -m '[update]config' [root@localhost repo]# git tag -a v1.1 -m 'version 1.1' [root@localhost repo]# git push gitlab --all [root@localhost repo]# git push gitlab --tag |
tagからmasterの切り戻し
(1) リポジトリをクローンします。
1 2 |
[root@localhost ~]# git clone https://gitlab.com/username/repo.git |
(2) masterブランチをtagのv1.0へ切り戻します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@localhost ~]# cd repo/ [root@localhost repo]# git tag v1.0 v1.1 [root@localhost repo]# git checkout -b v1.0 v1.0 Switched to a new branch 'v1.0' [root@localhost repo]# git branch master * v1.0 [root@localhost repo]# git branch -D master Deleted branch master (was 48ab8fa). [root@localhost repo]# git branch -m v1.0 master [root@localhost repo]# git branch * master |
(3) 必要に応じてremoteの設定をします。
(4) リモートのmasterを上書きします。
1 2 |
[root@localhost repo]# git push gitlab --all -f |
ひとまず、これでやりたいことはできた٩( ‘ω’ )و