Oasist Blog

This blog mainly features Natural Languages, Programming and Life Hacks.

Git Installation on WSL

f:id:oasist:20200613213929p:plain
WSL on Windows

目次

1. Environment

2. Install Package

  • Update Apt package manager
$ sudo apt update
  • Upgrade Apt package manager
$ sudo apt upgrade

3. Install Git

* Only if Git is not install yet after you run sudo apt update and sudo apt upgrade.

3-1. Default Package

* You will install an older version than the lastest one in this way.

  • Install Git.
$ sudo apt install git
  • Check the version of Git installed.
$ git --version

3-2. Install from a Source

* You will install the latest version of Git althogh this way takes more time Apt does not manage it.

  • Install Git.
$ sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
  • Check the version of Git installed.
$ git --version

4. Git Settings

  • Assign username and email.
$ git config --global user.name "[username]"
$ git config --global user.email "[email]"

* Omit --global to assign username and email in each project.

  • Check the configuration.
$ git config --list
user.name=[username]
user.email=[email]

5. SSH Settings

  • Generate a SSH key.
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/[username_ubuntu]/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/[username_ubuntu]/.ssh/id_rsa.
Your public key has been saved in /home/[username_ubuntu]/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hxbYsu6UHql6qA4tIIA7BnbVktsubFbEV6ewBQw+hmM [username_ubuntu]@DESKTOP-QDJ9F6V
The key's randomart image is:
+---[RSA 2048]----+
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
|      foobar     |
|     hogehoge    |
+----[SHA256]-----+
  • Create config file and take down hostname, domainm, username and path to the secret key.
vim ~/.ssh/config
Host github
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Or

Host gitlab
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa

I faced some troubles here.

  1. Ubuntu refers to ~/.ssh/id_rsa while Windows detects /mnt/c/Users/[username_windows]/.ssh/id_rsa, so I had to manage both because I wanted to handle Git commands in IDE on the host OS, Windows.
  2. When I put a symbolic file of ~/.ssh/ on Ubuntu at Windows, Windows could not read it because of permission, so I failed to use Git in IDE.
  3. When I put a symbolic file of /mnt/c/Users/[username_windows]/.ssh/id_rsa on Windows to Ubuntu, Ubuntu warned that permission was too loose.

After all, I placed the same ~/.ssh on both Ubuntu and Windows, but I doubt that this is cool.
Somebody, please tell me a better solution.

6. GitHub / GitLab Settings

Register the string in the public key to GitHub or GitLab.

$ cat ~/.ssh/id_rsa.pub
ssh-rsa HOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobarHOGEHOGEFOOBARhogehogefoobar [username_ubuntu]@DESKTOP-XXXXXXX

6-1. GitHub

  • Click Setting

f:id:oasist:20200613214019p:plain
Setting をクリック

  • Click SSH and GPG keys

f:id:oasist:20200613214115p:plain
SSH and GPG keys をクリック

  • Click New SSH key

f:id:oasist:20200613214138p:plain
New SSH key をクリック

  • Label an arbitrary name, paste the string of the public key to register.

f:id:oasist:20200613214138p:plainf:id:oasist:20200613214212p:plain
鍵を登録

6-2. GitLab

  • Click Setting

f:id:oasist:20200613214341p:plain
Setting をクリック

  • Click SSH and GPG keys

f:id:oasist:20200613214406p:plain
SSH keys をクリック

  • Paste the string of the public key to register.

f:id:oasist:20200613214543p:plainf:id:oasist:20200613214432p:plain
鍵を登録

6-3. Check Connection

  • Check the connetion with GitHub / GitLab.
$ ssh github
PTY allocation request failed on channel 0
Hi oasis-forever! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

7. Atom

IDE, such as Atom, allows us to use Git in GUI.


  • Stage: $ git add [filename]
  • Discard Changes: $ git stash (individual)
  • Stage All: $ git add -A

f:id:oasist:20200613214707p:plain
Atom上Git操作①


  • Discard All Changes: $ git stash (all)
  • Undo Last Discard: $ git stash pop

f:id:oasist:20200613214752p:plain
Atom上Git操作②


  • Click File: $ git diff
  • Stage Selection: $ git add [filename] (partly)
  • Discard Selection: $ git stash (partly)

f:id:oasist:20200613214852p:plain
Atom上Git操作③


  • Input a message and click Commit to [branch name]: git commit -m "[message]"

f:id:oasist:20200613215019p:plain
Atom上Git操作④


  • Push: git push origin [branch name]

f:id:oasist:20200613215048p:plain
Atom上Git操作⑤


  • Fetch: git fetch origin [branch name]
  • Pull: git pull origin [branch name]
  • Push: git push origin [branch name]
  • Push Force: git push origin [branch name] -f

f:id:oasist:20200613215113p:plain
Atom上Git操作⑥

8. Reference