Git概要2-Gitサーバ


他の人と協力して開発作業(例えば会社で)を行う場合は、共有するGit倉庫が必要であり、開発エンジニアたちがアクセスでき、この共有倉庫からデータをプッシュしたり引き出したりすることができ、この倉庫を「Gitサーバ」と呼ぶ.

Gitサーバの作成


Gitサーバの作成は比較的簡単で、テスト環境では2台のCentOS 7を使用し、IPはそれぞれ192.168.107.128(テストサーバ)、192.168.107.129(テストクライアント)である.まずテストサーバ上で操作する:1、新しいユーザgitという新しく追加されたユーザを追加することは、クライアントとサーバ側の通信をテストするために使用される(通信はSSHプロトコルを使用する)、すなわちクライアントがpullやpushを行う際にgitユーザの権限で操作される.
[root@localhost ~]# useradd git
[root@localhost ~]# passwd git

ユーザーの作成が完了したらpasswdを使用してパスワードを設定してください.2、Git Repositoryを作成新しく作成したユーザーは/homeディレクトリの下で相応のディレクトリを生成し、/home/git/ディレクトリの下で操作する:
[root@localhost ~]# cd /home/git/
[root@localhost git]# mkdir repositories
[root@localhost git]# cd repositories/
[root@localhost repositories]# git --bare init
  Git   /home/git/repositories/
[root@localhost repositories]# cd ..
[root@localhost git]# chown -R git:git repositories/

git-bare init:サービスを提供できるが、このサーバ上で操作しない空のGit倉庫を作成します.chown-R git:git repositories/:Git倉庫の所有者をユーザーgitとして設定します.3、authorized_の設定keys authorized_keysは、テストクライアントのssh-keyを格納するためのプロファイルです.このファイルは、テストサーバの/home/git/ディレクトリの下に保存し、自分で作成する必要があります.」sshディレクトリ:
[root@localhost git]# mkdir .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# vi authorized_keys

テストクライアントでssh-keygenを実行し、Enterを実行します.デフォルトは「/root/.ssh/」ディレクトリに保存され、id_rsa.pubファイルのkey情報をauthorized_にコピーします.keysファイルでいいです.さて、ここまでサーバ側の作業は完了しても、基本的なGitサーバが使用できるようになりました.

クライアントアクション


Gitサーバの作成が完了したら、テストクライアントに切り替えてテストを行います.
[root@localhost ~]# mkdir work
[root@localhost ~]# cd work/
[root@localhost work]# git clone [email protected]:/home/git/repositories
[root@localhost work]# ls
repositories
[root@localhost work]# cd repositories/
[root@localhost repositories]# ls
[root@localhost repositories]# 

以上のように、テストクライアントでワークディレクトリを作成し、そのディレクトリの下でclone操作を行い、操作に成功し、空のバージョンライブラリを取得しました(Git倉庫にはファイルがありません.もちろん空です).
[root@localhost repositories]# echo "hello,world" > hi
[root@localhost repositories]# ls
hi
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "add file hi"
[root@localhost repositories]# git push origin master

「hi」という名前のファイルを作成し、「hello,world」と入力し、ローカルで追加およびコミットし、最後にリモートのGitサーバウェアハウスmasterブランチにプッシュします.このとき、新しいクライアントcloneがこのGit倉庫に入ったら、cloneは空の倉庫に行かず、倉庫には「hi」のファイルが現れ、内容は「hello,world」です.

バージョンの競合の解決


バージョンの競合は、2つのクライアントが同じサーバcloneからクライアントAがhiファイルを変更したなど、倉庫でよく発生する問題です.「AAAAAAAA」を1行追加し、Gitサーバに提出してプッシュした.クライアントBも「hi」ファイルを修正し、「BBBBBB」を1行追加し、Gitサーバにも提出してプッシュしたが、このときクライアントBのプッシュは成功せず、GitサーバはクライアントBに「GitサーバのバージョンがクライアントBのバージョンと衝突している」ことを注意する.クライアントA操作:
[root@localhost repositories]# echo "AAAAAA" >> hi
[root@localhost repositories]# cat hi 
hello,world
AAAAAA
[root@localhost repositories]# 
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "add 'AAAAAA'"
[master 84beedf] add 'AAAAAA'
 1 file changed, 1 insertion(+), 2 deletions(-)
[root@localhost repositories]# 
[root@localhost repositories]# git push origin master

クライアントB操作:
[root@localhost repositories]# echo "BBBBBB" >> hi
[root@localhost repositories]# cat hi
hello,world
BBBBBB
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "add 'BBBBBB'"
[master 1070263] add 'BBBBBB'
 1 file changed, 1 insertion(+), 2 deletions(-)
[root@localhost repositories]# git push origin master

この場合、GitサーバのバージョンをクライアントBに引き寄せ、競合を解決してからGitサーバに送信する必要があります.
[root@localhost repositories]# git pull
[root@localhost repositories]# cat hi
hello,world
<<<<<<< HEAD
BBBBBB
=======
AAAAAA
>>>>>>> 84beedf9597880a33219182b34146831539915e1
[root@localhost repositories]# vi hi
[root@localhost repositories]# cat hi
hello,world
BBBBBB
AAAAAA
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "resolve conflict"
[master f632248] resolve conflict
[root@localhost repositories]# git push origin master

これで、競合が解決し、最新の変更がGitサーバにプッシュされます.