0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ubuntu16.04にGitサーバを立ててドキュメントをGit管理する方法

Last updated at Posted at 2020-01-24

目的

  • レガシーな環境でもGitによるバージョン管理を行いたくてUbuntuPCにGitのサーバを立てた時の手順をまとめる

今回使用したもの

  • Linux(Ubuntu16.04)のPC2台
  • Linuxマシンにリモートからコマンドを送ることのできるターミナルエミュレータ(tertermなど)が入ったWindowsマシン

皆さんが実施する上で最低限で必要なもの

  • Linux(Ubuntu)PC: 2台
  • Linuxマシンが用意できないけどGitを勉強したい方はこちら

実施条件

  • LinuxPCはOSインストールが完了していること。
  • LinuxPCの管理者権限がある(sudoコマンドが実行できる)こと。

実施方法概要

  1. 準備
  2. リモートリポジトリの作成
  3. SSH鍵作成と登録
  4. clone

実施方法詳細

  1. 準備

    1. 2台あるLinuxPCからGitサーバとして使用するPCとクライアントPCを決める。

    2. git-userユーザの作成

      1. GitサーバPCのsudoコマンドが実行できるユーザで下記コマンドを実行してgit-userというユーザを作成する。

        $ sudo adduser git-user
        >Enter new UNIX password: 任意のgit-userのパスワード入力後Enter
        >Retype new UNIX password: 先に入力した任意のgit-userのパスワード入力後Enter
        >Full Name[]: 空欄でEnter
        >Room Number[]: 空欄でEnter
        >Work Phone[]: 空欄でEnter
        >Home Phone[]: 空欄でEnter
        >Other[]: 空欄でEnter
        >Is the information correct? [Y/n] Yを入力してEnter
        
    3. GitサーバPCのsudoコマンドが実行できるユーザで下記コマンドを実行してgit-userユーザでもsudoコマンドが実行できるようにする。

      $ sudo gpasswd -a git-user sudo
      >Adding user git-user to group sudo
      
    4. GitサーバPCのgit-userユーザで下記コマンドを実行してGitをインストールする。

      $ sudo apt update
      $ sudo apt install git
      $ git --version
      >git version 2.X.X
      
    5. GitサーバPCのgit-userユーザで下記コマンドを実行して認証情報を自動保存するように設定を変更する。

      $ git config --global credential.helper cache
      
    6. GitサーバPCのgit-userユーザで下記コマンドを実行して日本語ファイルでも文字化けしないようにする。

      $ git config --global core.quotepath false
      
    7. GitサーバPCのgit-userユーザで下記コマンドを実行して

    8. GitサーバPCのgit-userユーザで下記コマンドを実行してsshをインストールする。

      $ sudo apt install ssh
      
    9. GitサーバPCのIPアドレス、git-userユーザのパスワードを確認する。

    10. クライアントPCの任意のユーザで下記コマンドを実行してGitをインストールする。

      $ sudo apt update
      $ sudo apt install git
      $ git --version
      >git version 2.X.X
      
    11. クライアントPCの任意のユーザで下記コマンドを実行して認証情報を自動保存するように設定を変更する。

      $ git config --global credential.helper cache
      
    12. クライアントPCの特定のユーザでで下記コマンドを実行して日本語ファイルでも文字化けしないようにする。

      $ git config --global core.quotepath false
      
    13. クライアントPCの特定のユーザでで下記コマンドを実行してユーザとメールアドレスを設定する。

      $ git config --global user.email "任意のメールアドレス"
      $ git config --global user.name "任意のユーザ名"
      
    14. クライアントPCの特定のユーザで下記コマンドを実行してsshをインストールする。

      $ sudo apt install ssh
      
  2. リモートリポジトリの作成

    1. GitサーバPCのgit-userユーザで下記コマンドを実行してリモートリポジトリを格納しておくディレクトリを作成する。

      $ mkdir ~/remote_repository
      
    2. GitサーバPCのgit-userユーザで下記コマンドを実行してtestという名前のリモートリポジトリを作成する。(外部からのpushを許容するため、共有リポジトリとして使用するために--bareをつけて実行する。)

      $ mkdir -p ~/remote_repository/test
      $ cd ~/remote_repository/test
      $ git init --bare
      >Initalized empty Git repository in /home/git-user/remoterepository/test/.git
      
  3. SSH鍵作成と登録

    1. GitサーバPCのgit-userユーザで下記コマンドを実行してリモート役PCにローカル役PCのSSH鍵を登録しておくファイルを作成する。

      $ mkdir ~/.ssh
      $ touch ~/.ssh/authorized_keys
      
    2. クライアントPCの特定のユーザでで下記コマンドを実行してSSH鍵を発行する。(質問は初回鍵発行なら全て空欄でEnterを押下する。下記の問いは初回時)

      $ ssh-keygen -t rsa
      >Enter file in which to save the key (/home/ユーザ名/.ssh/id_rsa): 空欄でEnter
      >Created directory '/home/ユーザ名/.ssh'. 空欄でEnter
      >Enter passphrase (empty for no passphrase): 空欄でEnter
      

    >Enter same passphrase again: 空欄でEnter
```

1. クライアントPCの特定のユーザで下記コマンドを実行してSSH公開鍵を表示、コピーする。

    ```terminal
    $ cat ~/.ssh/id_rsa.pub
    >ここに出力された内容を語尾のユーザ名@PC名まで含めて全てコピーする
    ```

1. 先にコピーしたクライアントPCのSSH公開鍵をGitサーバPCのauthorized_keysに記載する。
    1. GitサーバPCのgit-userで下記コマンドを実行する。

        ```terminal
        $ echo "先にコピーしたクライアントPCのSSH公開鍵" >> ~/.ssh/authorized_keys
        ```
  1. clone

    1. クライアントPCの特定のユーザで下記コマンドを実行してcloneのテストを行う。

      $ cd
      $ git clone ssh://git-use@GitサーバPCのIPアドレス/home/git-user/remote_repository/test
      >Are you sure you want to continue connecting (yes/no)? yesと入力しEnter
      >warning: You appear to have clone an empty repository.
      
  2. commitとpush

    1. クライアントPCの特定のユーザで下記コマンドを実行してreadme.mdファイルをcommitとpushしてみる。

      $ cd ~/test
      $ git commit --allow-empty -m "First commit"
      $ git push origin master
      

cloneをちょっとだけ楽にしよう

  • cloneを行う時にGitサーバPCのユーザ情報とIPアドレスを打ち込むのは面倒である。

  • SSHのconfigファイルに予めGitサーバPCの情報を記入しておくことでその手間が省ける。

  • 下記にクライアントPCのSSHのconfigファイルに情報を記載する方法をまとめる

    1. クライアントPCの特定のユーザで下記コマンドを実行しSSHのconfigファイルを修正する。

      $ vi ~/.ssh/config
      
    2. 下記の内容を開いたconfigファイルに追記する。(host名がこの設定を呼び出す際の識別子になる、お好みの名前を設定して良い。今回は認識しやすいようにGitサーバPCのユーザ名にした)

      host git-user
          user git-user
          hostname GitサーバPCIPアドレス
          identityfile ~/.ssh/id_rsa
      
      データ名 入力データ
      host ホスト名(任意のものでOK)
      user 接続先PCのユーザ名
      hostname 接続先PCのIPアドレス
      indentityfile SSH接続に使用するSSH鍵までのフルパス
  • SSHのconfigファイルにGitサーバPCの情報が記載されている場合、クライアントPCでcloneするコマンドは下記のようになる。(host名がgit-userの場合)

    $ git clone ssh://git-user/home/git-user/remote_repository/test/.git
    
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?