Visual Studio CodeでGitを使えるようにセットアップしたところ、ものすごい快適になりました。
ここでは、WindowsPCからLinux(Ubuntu)サーバーのリモートリポジトリに接続する方法をご紹介します。
Gitダウンロード
以下から使用しているOSのインストーラーをダウンロード
Gitインストール
※ これ以降Windows10を例に説明します
1. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_01-min.png)
2. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_02-min.png)
3. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_03-min.png)
4. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_04-min.png)
5. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_05-min.png)
6. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_06-min.png)
7. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_07-min.png)
8. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_08-min.png)
9. 〔NEXT〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_09-min.png)
10. 〔Install〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_10-min.png)
11. インストールが終わるまで待ちます
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_11-min.png)
12. 〔Finish〕をクリック
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_git_install_12-min.png)
SSH公開鍵登録
公開鍵と秘密鍵を作成
Windowsのコマンドプロンプトから公開鍵と秘密鍵を作成します。3行目のコマンド入力後は何も入力せず全てキーボードの〈Enter〉を押してください。
C:\Users\color-code>mkdir .ssh
C:\Users\color-code>cd .ssh
C:\Users\color-code\.ssh>ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\color-code/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
※ C:\Users\[ユーザー名]/.ssh フォルダが既にある場合は、1行目のコマンドを実行すると「サブディレクトリまたはファイル .ssh は既に存在します。」と表示されますが、気にせず2行目以降のコマンドを実行してください。
公開鍵をサーバーに登録
※ 以下の設定だった場合の説明をしますので、適時読み替えてください。
サーバーのIPアドレス:192.168.11.3
接続ユーザー:git-user
gitリポジトリ―パス:/var/lib/git/git-repository.git
ここからはサーバー側での作業になります。先ほど作成した公開鍵(id_rsa.pub)をサーバーにコピーしておいてください。
$ cd /home/git-user/.ssh/
/home/git-user/.ssh$ sudo nano authorized_keys
ファイルが開いたら、ファイルの最終行に公開鍵の中身を追加します。
:
ssh-rsa AAAAAAAA.....
保存して閉じればサーバー側の作業は完了です。
初期登録
ここからはクライアントPC側の作業です。VIsual Studio Codeのターミナルから、以下の通りユーザー名とメールアドレスを登録します。username箇所と、xxx@xxx.com箇所を正しい内容に書き換えて実行してください。
$ git config --global user.name "username"
$ git config --global user.email xxx@xxx.com
動作確認
WordPress開発ディレクトリに作成したリポジトリをCloneし、データを配置
Visual Studio CodeのUIからクローンすると、クローン先のパスに、リポジトリ名のフォルダが作成されてしまいます。
クローン先のパス直下にクローンしたい場合は、Visual Studio Codeのターミナルから以下コマンドでクローンすれば可能です。
git clone ssh://git-user@192.168.11.3/var/lib/git/git-repository.git Z:\\clone_dest
※ “Z:\\clone_dest”箇所にクローン先パスを入力します。\はエスケープが必要なので\\で入力します。
リポジトリ名のフォルダができても良い場合は以下の操作でクローン出来ます。
〈Ctr〉+〈Shift〉+〈p〉を押し、① ”git”と入力し、② ”Git:Clone” を選択
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_clone_01-min.png)
リポジトリのURLを入力して<Enter>。
ssh://git-user@192.168.11.3/var/lib/git/git-repository.git
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_clone_02-min.png)
ダイアログが表示されるので、クローン先のパスを入力。
※ 以下エラーが出る場合は、Clone先の適切なパーミッション設定が必要です。
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_clone_error_01-min.png)
クローンが開始されれば成功です。
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_clone_03-min.png)
コミット
①の〔ソース管理〕をクリックし、②の〔+〕をクリックすると、〔ステージング済みの変更〕となり、コミット対象になります。
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_commit_01-min.png)
①にコメントを入力し、②をクリックすると、〔ステージング済みの変更〕がコミットされます。
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_commit_02-min.png)
プッシュ
以下の赤枠をクリックするとメニューが表示されプッシュをはじめ様々な操作ができます。
![](https://color-code.net/wp-content/uploads/2019/06/ec_1944_push-min.png)
まとめ
Visual Studio Code だけでGit操作が完結するというのはかなり便利です。
これまでTortoiseGitを使っていましたが、今では Visual Studio Code のGit機能しか使っていません。
Visual Studio Code を使っている方にはとてもおすすめです。