[Git] 動作を試す 実行例56：remote・pushでローカルリポジトリ間同士でpush送信(要設定変更)

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

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

※前回記事のリポジトリ状態からの続きになっています。
| [前回記事へ](https://qiita.com/dl_from_scratch/items/3ea224b9a1f576278d86) | [目次へ：Git関連記事のまとめページ](https://qiita.com/dl_from_scratch/items/597bc28a4787a7901d21) | [次回記事へ](https://qiita.com/dl_from_scratch/items/1a3300da8886c17d5405) |
|:-:|:-:|:-:|

# 実行例

```
※別のローカルリポジトリへpush送信
git push -f origin-L2 origin/master:master
↓
結果: 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 597 bytes | 597.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To D:/test-space/local-repo2
 ! [remote rejected] origin/master -> master (branch is currently checked out)
error: failed to push some refs to 'D:/test-space/local-repo2'

※別のローカルリポジトリへpush送信
git push -f origin-L2 origin/master:refs/heads/master
↓
結果:
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 597 bytes | 597.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To D:/test-space/local-repo2
 ! [remote rejected] origin/master -> master (branch is currently checked out)
error: failed to push some refs to 'D:/test-space/local-repo2'

(※既定の設定では、別のローカルリポジトリ(非ベアリポジトリ)へpush送信は出来ない模様、エラー)

-----

※別のローカルリポジトリへpush送信
git push -f origin-L2 master
↓
結果: 
error: src refspec master does not match any
error: failed to push some refs to 'D:/test-space/local-repo2'

(※ローカルブランチmasterが存在しないのでエラー)

git checkout master
↓
結果: 
hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
hint: you can do so by fully qualifying the name with the --track option:
hint:
hint:     git checkout --track origin/<name>
hint:
hint: If you'd like to always have checkouts of an ambiguous <name> prefer
hint: one remote, e.g. the 'origin' remote, consider setting
hint: checkout.defaultRemote=origin in your config.
fatal: 'master' matched multiple (2) remote tracking branches

(※origin、origin-L2の2つのリモートリポジトリ名がある場合のcheckout)
git checkout -b master origin/master
↓
結果: 
Switched to a new branch 'master'
branch 'master' set up to track 'origin/master'.

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

※別のローカルリポジトリへpush送信
git push -f origin-L2 master
↓
結果: 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 597 bytes | 597.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To D:/test-space/local-repo2
 ! [remote rejected] origin/master -> master (branch is currently checked out)
error: failed to push some refs to 'D:/test-space/local-repo2'

(※既定の設定では、別のローカルリポジトリ(非ベアリポジトリ)へpush送信は出来ない模様)

-----

リモートリポジトリへの git push に失敗した場合 (remote rejected)
https://nilfigo.hatenablog.com/entry/2013/08/09/145435
↓
引用: 
「bare」ではないので、可能ならば「bare」にする。
$ git config --bool core.bare true

git config --add receive.denyCurrentBranch ignoreはどう危険なのか
https://nishiohirokazu.hatenadiary.org/entry/20120416/1334548800
↓
引用: 
「ある人が行った実装を、別の人が無意識に削除してコミットする」という事態を引き起こす。
push時に「bareでない(ワークツリーを持っている)リポジトリの、今チェックアウトされているブランチをpushすることはできない。なぜならインデックスとワークツリーが不整合になるからだ」と言われてエラーになっている。

(※既定の設定では、別のローカルリポジトリ(非ベアリポジトリ)へpush送信は出来ない模様、エラー)

-----

(※設定を変更して、強制的にpush送信すると)

cd /test-space/local-repo2

git config --get-all receive.denyCurrentBranch
↓
結果: 
(なし)

git config --add receive.denyCurrentBranch ignore

git config --get-all receive.denyCurrentBranch
↓
結果: 
ignore

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

cd /test-space/local-repo3

※別のローカルリポジトリへpush送信
git push -f origin-L2 master
↓
結果: 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 597 bytes | 597.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
To D:/test-space/local-repo2
   c69a305..1247fa9  master -> master

(※設定を変更して、強制的にpush送信、成功)

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

cd /test-space/local-repo2

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

-----

(※別のローカルリポジトリのブランチ位置を強制的に過去側へ戻す)

cd /test-space/local-repo3

※別のローカルリポジトリへpush送信
git push -f origin-L2 c69a305:master
↓
結果:
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To D:/test-space/local-repo2
 + 1247fa9...c69a305 c69a305 -> master (forced update)

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

cd /test-space/local-repo2

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

(※masterの位置が「message R1」の所まで戻されておりOK)

-----

(※設定を戻す)

cd /test-space/local-repo2

git config --get-all receive.denyCurrentBranch
↓
結果: 
ignore

git config --local --unset-all receive.denyCurrentBranch

git config --get-all receive.denyCurrentBranch
↓
結果: 
(なし)

-----

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

```

# 環境

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