1
3

More than 3 years have passed since last update.

Git初心者(自分)によるWSLでのGit導入からGitHubの使用まで

Last updated at Posted at 2020-04-15

はじめに

自分用に適当に書くので参考にならなかったらすみません :clap:

前提

  • 実行環境はWSL
  • GitHubのアカウントは作成済み
  • 投稿主(初心者)の備忘録

目次(やること)

  1. GitをWSLにインストール>>
  2. 初期設定する>>
  3. リモートの作成>>
  4. Gitにpush>>
  5. Gitからpull>>
  6. 変更のあったファイルを確認>>
  7. 変更のあったファイルの内容を確認>>
  8. addされているファイルを確認>>
  9. 変更履歴を確認>>

手順

$は入力しなくていいです。

1. GitをWSLにインストール

wsl上で以下のコマンドを実行

bash
$ sudo apt install git

しっかりインストールされていれば、

bash
$ git --version
git version 2.17.1

となるはず。(バージョンはそれぞれ)

2. 初期設定する

ユーザ名とメールアドレスを登録

bash
$ git config --global user.name "任意のユーザ名"
$ git config --global user.email "任意のメールアドレス"

うまく設定できていれば、

bash
$ git config user.name
<設定したユーザ名>
$ git config user.email
<設定したメールアドレス>

となるはず。

3. リモートの作成

1. GitHubにログイン

2. リポジトリを作成

右上の+ボタンからNew repositoryを選択
New_repository
リポジトリ名を決めて作成
create_repository

3. 完成

complete

4. Gitにpush

まずは、Gitを用いるディレクトリを作成(mkdir)
すでにあるならそこに移動(cd)
今回のディレクトリはgit_test(この中に入る)
中身は空のテキストファイルempty.txt

git_test
$ ls
empty.txt

1. Gitで管理を始める

git_test
$ git init

2. push先のリモートを指定

SSH
先ほど作成したリポジトリのSSH(自分のもの)をコピーして以下を実行

git_test
$ git remote add origin <コピーした指定先のSSH>

3. pushする

addしてcommitしてpushする。
commit時のメッセージは適切に書く。

git_test
$ git add empty.txt 
$ git commit -m 'empty.txtを作成しました。'
~色々出ます~
$ git push origin master
~色々出ます~

もし、カレントディレクトリ内のすべてをaddしたいときは、
ワイルドカードを用いて、git add *とするか
カレントディレクトリを表す.を用いて
git add .としてください。

4. GitHubを確認

うまくいていれば先ほどのリポジトリに追加されている。
success

5. Gitからpull

仮に友達のF君がgit_test2というディレクトリに、
先ほど私がpushしたものをpullすることを考える。

git_test2
$ git init
~色々出ます~
$ git remote add origin https://github.com/yosse95ai/git_test.git
$ git pull origin master
~色々出ます~
$ ls
empty.txt

うまくいったのでempty.txtが追加された。

6. 変更のあったファイルを確認

私は空のテキストファイルempty.txtの内容を変更し、
空のindex.htmlファイルを作成しました。

empty.txt
こんにちはQiita

変更されたファイルが何か確認します。

git_test
$ ls
empty.txt  index.html
$ git status                                                        
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)  

        modified:   empty.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        index.html

no changes added to commit (use "git add" and/or "git commit -a")

modified: empty.txt, index.htmlの部分は赤色で表示されました。

7. 変更のあったファイルの内容を確認

git_test
$ git diff
diff --git a/empty.txt b/empty.txt
index e69de29..0d9d510 100644
--- a/empty.txt
+++ b/empty.txt
@@ -0,0 +1 @@
+こんにちはQiita
\ No newline at end of file

@@ -0,0 +1 @@の部分は青っぽい色
+こんにちはQiitaの部分は緑色で表示されました。

8. addされているファイルを確認

まず、empty.txtのみaddします。

git_test
$ git add empty.txt 
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   empty.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        index.html

modified: empty.txtの部分は緑色で表示され、
index.htmlの部分は赤色で表示されました。
この後、私はすべての変更をaddしてcommitしてpushしました。

9. 変更履歴を確認

F君は再びpullしたみたいです。

1. ファイルの変更履歴を確認

まずは、どのファイルが変更されたか確認します。

git_test2
$ git log
commit 84864e87c8ddd18dcd5c590fba4ed4a97d538fe3 (HEAD -> master, origin/master)
Author: yosse95ai <Qiita@qiita.ac.jp>
Date:   Wed Apr 15 03:09:18 2020 +0900

    index.htmlを追加し、empty.txtに内容を追加

commit a067e9cc4bcfb1c08c65cbb6cd33358c0410b24a
Author: yosse95ai <Qiita@qiita.ac.jp>
Date:   Wed Apr 15 02:12:33 2020 +0900

    empty.txtを作成しました。

2. ファイルの内容の変更履歴を確認

次に、どのファイルの内容がどのように変更されたか確認します。

git_test2
$ git log -p
commit 84864e87c8ddd18dcd5c590fba4ed4a97d538fe3 (HEAD -> master, origin/master)
Author: yosse95ai <Qiita@qiita.ac.jp>
Date:   Wed Apr 15 03:09:18 2020 +0900

    index.htmlを追加し、empty.txtに内容を追加

diff --git a/empty.txt b/empty.txt
index e69de29..0d9d510 100644
--- a/empty.txt
+++ b/empty.txt
@@ -0,0 +1 @@
+こんにちはQiita
\ No newline at end of file
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..e69de29

commit a067e9cc4bcfb1c08c65cbb6cd33358c0410b24a
Author: yosse95ai <Qiita@qiita.ac.jp>
Date:   Wed Apr 15 02:12:33 2020 +0900

    empty.txtを作成しました。

diff --git a/empty.txt b/empty.txt
new file mode 100644
index 0000000..e69de29

変更した内容が確認できました。

さいごに

参考にならなくても責めないでください。。。
個人的には参考(備忘録)になりました。
頭の中の情報が更新され次第項目を追加していきます。

参考

1
3
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
1
3