LoginSignup
0
0

[Git] 動作を試す 実行例49:pushでリモートリポジトリのブランチ位置を強制的に移動

Posted at

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

git push を強制する -f (–force)と –force-with-lease
https://www-creators.com/archives/5168
↓
引用: 
リモートの履歴が指定したローカルブランチより進んでいれば、履歴同士の競合により、エラーで push は失敗してしまいます。
それでも、無理やりリモートの履歴を上書きするには、git push は「-f」オプションで強制できます。
git push -f origin master
ブランチ更新日付が新しい時だけ強制する「--force-with-lease」

リモートリポジトリの巻き戻しが拒否される
https://www.nakamuri.info/mw/index.php/%E3%83%AA%E3%83%A2%E3%83%BC%E3%83%88%E3%83%AA%E3%83%9D%E3%82%B8%E3%83%88%E3%83%AA%E3%81%AE%E5%B7%BB%E3%81%8D%E6%88%BB%E3%81%97%E3%81%8C%E6%8B%92%E5%90%A6%E3%81%95%E3%82%8C%E3%82%8B
↓
引用: 
-f(--force) を付けても push が拒否される場合があります。
リモートリポジトリをgit init --bare --sharedというように --shared オプション付きで作った場合、
none-fast-forward な push が断固拒否される設定になっているからです。
denyNonFastforwards がその設定です。 true なら none-fast-forward な push は断固拒否されます。
リモートリポジトリに対して git コマンドで git config receive.denyNonFastforwards false

-----

(※試行用にlocal-repo3を作成)

cd /test-space
mkdir local-repo3
cd /test-space/local-repo3

git init
git remote add origin "D:/test-space/remote-repo1.git"

git log
↓
結果: 
fatal: your current branch 'master' does not have any commits yet

-----

(※始めに必要なコミット履歴を取得)

git fetch origin master branch-R1
↓
結果: 
remote: Enumerating objects: 27, done.
remote: Counting objects: 100% (27/27), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 27 (delta 5), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (27/27), 2.03 KiB | 2.00 KiB/s, done.
From D:/test-space/remote-repo1
 * branch            master     -> FETCH_HEAD
 * branch            branch-R1  -> FETCH_HEAD
 * [new branch]      master     -> origin/master
 * [new branch]      branch-R1  -> origin/branch-R1

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 message R3
* c69a305 message R1
* 94996a0 message4
| * e009b2d (origin/branch-R1) message R2
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

-----

(※リモートリポジトリのブランチ位置を強制的に移動させる→エラーが発生)

※リモートリポジトリのブランチ位置を強制的に過去側へ移動
git push -f origin 00cad71:branch-R1
↓
結果: 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: error: denying non-fast-forward refs/heads/branch-R1 (you should pull first)
To D:/test-space/remote-repo1.git
 ! [remote rejected] 00cad71 -> branch-R1 (non-fast-forward)
error: failed to push some refs to 'D:/test-space/remote-repo1.git'

(※「non-fast-forward」エラーが発生する)

※-fを付けてもpushが拒否される場合、リモートリポジトリをgit init --bare --shared
 (--shared オプション付き)で作った場合、none-fast-forwardなpushが断固拒否される
 設定になっている。

-----

cd /test-space/remote-repo1.git
type config
↓
結果: 
[core]
        repositoryformatversion = 0
        filemode = false
        bare = true
        symlinks = false
        ignorecase = true
        sharedrepository = 1
[receive]
        denyNonFastforwards = true

(※リモートリポジトリの設定はdenyNonFastforwards = trueになっている)

cd /test-space/local-repo3

-----

(※リモートリポジトリのブランチ位置を強制的に移動させる
 方法1: リモートリポジトリのブランチを削除して、任意の位置へ作成し直す)

※リモートリポジトリのブランチを削除
git push --delete origin branch-R1
↓
結果: 
To D:/test-space/remote-repo1.git
 - [deleted]         branch-R1

※任意の位置へ同名のローカルブランチを作成
git branch branch-R1 00cad71
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 message R3
* c69a305 message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 (branch-R1) message2
* 4ace194 message1

※ブランチをリモートリポジトリへpush送信
git push origin branch-R1
↓
結果: 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
 * [new branch]      branch-R1 -> branch-R1

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 message R3
* c69a305 message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 (origin/branch-R1, branch-R1) message2
* 4ace194 message1

(※リモートリポジトリのブランチ位置が強制的に過去側に移動されている)

-----

cd /test-space/remote-repo1.git
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (HEAD -> master) message R4
* fab68d8 (branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 (branch-R1) message2
* 4ace194 message1

(※リモートリポジトリのブランチ位置が強制的に過去側に移動されている)

cd /test-space/local-repo3

-----

(※元の位置に戻す)

※リモートリポジトリのブランチ位置を強制的に最新側へ移動
git push origin e009b2d:branch-R1
↓
結果: 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
   00cad71..e009b2d  e009b2d -> branch-R1

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 message R3
* c69a305 message R1
* 94996a0 message4
| * e009b2d (origin/branch-R1) message R2
|/
* 20bbab7 message3
* 00cad71 (branch-R1) message2
* 4ace194 message1

git branch -D branch-R1

-----

(※リモートリポジトリのブランチ位置を強制的に移動させる
 方法2: リモートリポジトリの設定denyNonFastforwardsをfalseへ変更する)

cd /test-space/remote-repo1.git

※リモートリポジトリの設定denyNonFastforwardsをfalseへ変更
git config receive.denyNonFastforwards false

type config
↓
結果: 
[core]
        repositoryformatversion = 0
        filemode = false
        bare = true
        symlinks = false
        ignorecase = true
        sharedrepository = 1
[receive]
        denyNonFastforwards = false

cd /test-space/local-repo3

※リモートリポジトリのブランチ位置を強制的に過去側へ移動
git push -f origin 00cad71:branch-R1
↓
結果: 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
 + e009b2d...00cad71 00cad71 -> branch-R1 (forced update)

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 message R3
* c69a305 message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 (origin/branch-R1) message2
* 4ace194 message1

(※リモートリポジトリのブランチ位置が強制的に過去側に移動されている)

-----

cd /test-space/remote-repo1.git
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (HEAD -> master) message R4
* fab68d8 (branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 (branch-R1) message2
* 4ace194 message1

(※リモートリポジトリのブランチ位置が強制的に過去側に移動されている)

cd /test-space/local-repo3

-----

(※元に戻す)

git push origin e009b2d:branch-R1
↓
結果: 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
   00cad71..e009b2d  e009b2d -> branch-R1

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 message R3
* c69a305 message R1
* 94996a0 message4
| * e009b2d (origin/branch-R1) message R2
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

-----

(※local-repo3を削除)
cd /test-space
rd /s /q "/test-space/local-repo3"

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

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