LoginSignup
1
1

More than 3 years have passed since last update.

(備忘録)gitコマンド(GitHub編)

Last updated at Posted at 2020-04-29

1. はじめに

gitコマンドについての自分への備忘録としてまとめてみました。ご参考になれば幸いです。

参考:Gitチュートリアル
参考:Git公式(英語)

2. git基本用語

  • リポジトリ・・・変更履歴を記録する場所
  • commit・・ローカルリポジトリ(個人リポジトリ)にファイル変更を記録
  • push・・・リモートリポジトリ(共有リポジトリ)に変更を反映
  • pull・・・リモートリポジトリ(共有リポジトリ)の情報を取得
  • プルリクエスト・・・変更の取り込みをリポジトリオーナーにリクエストする

3. gitのインストール

MacOSXの場合

// インストール済みのGitのバージョンを確認
$ git version

Windows10の場合

// Gitのインストール
https://git-scm.com/downloads

// スタートメニューからGit Bushを起動

// gitバージョンの確認
$ git version

4. GitHubの登録

// Sign up(アカウント登録、ブラウザでの操作)
https://github.com

// GitHubユーザ名の登録(Git Bashでの操作)
$ git config --global user.name "xxxxxxxx"

// GitHubメールアドレスの登録(Git Bashでの操作)
$ git config --global user.email xxxxxxxx@xxxx.xxx

// リモートリポジトリの新規作成(ブラウザでの操作)
+アイコン → New repository

// Repository name
practice

// Public

// Create repository

5. GitHubにsshで接続する

参考:GitHub に SSH で接続する
参考:SSH Keyを作成してGitHubなどに接続してみる
参考:GithubのSSH通信設定
参考:ssh-addできなかったときへの対処

6. Linux基本コマンド

// ディレクトリの移動
$ cd 移動先ディレクトリ名

// ディレクトリ内の表示
$ ls -la

// ディレクトリの作成
$ mkdir 新規ディレクトリ名

// ファイルの削除
$ rm 削除ファイル名

// ファイルのコピー
$ cp -pr コピー元ファイル名 コピー先ファイル名

// ファイルの移動
$ mv 移動対象ファイル名 移動先ディレクトリパス

// ファイル内容の表示
$ cat 対象テキストファイル名

7. viエディター基本コマンド

// コマンドモードに移行
Esc

// カーソル位置から挿入
i

// コマンドモードに移行
Esc

// 保存&終了
:wq!

8. git基本コマンド操作

// 作業ディレクトリを作成
$ mkdir Git

// ディレクトリを移動
$ cd Git/

// ローカルリポジトリの作成
$ git init ./

// ディレクトリ内の表示
$ ls -la

// 練習用ファイルの新規作成
$ echo '# HelloGit' > README.md

// ディレクトリ内の表示
$ ls -la

// 練習用ファイルを変更
$ echo '# HelloGit2' >> README.md

// ファイル内容の表示
$ cat README.md

// ステージングエリアに変更を反映
$ git add README.md

// ローカルリポジトリに変更を反映
$ git commit

===
first commit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   new file:   README.md
#
===

// …or push an existing repository from the command line のコマンド実行
// ローカルリポジトリをリモートリポジトリに登録
$ git remote add origin git@github.com:zono-0/practice.git

// ローカルリポジトリの内容をリモートリポジトリに反映
$ git push -u origin master

// ブラウザでGitHubの当該リポジトリを参照し反映を確認する

// gitステータスの確認
$ git status

// 練習用ファイルを変更
$ echo '# HelloGit3' >> README.md

// ファイル内容の表示
$ cat README.md

// ステージングエリアに変更を反映
$ git add .

// ローカルリポジトリの変更確認&コミットメッセージを追記後に反映
$ git commit -v

===
ファイル変更の反映練習

ただの練習なので深い意味はない
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up to date with 'origin/master'.
#
# Changes to be committed:
#   modified:   README.md
#
# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
diff --git a/README.md b/README.md
index 573bbdf..19c9a8c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
 # HelloGit
 # HelloGit2
+# HelloGit3
===

// gitログを確認
$ git log

// ローカルリポジトリの内容をリモートリポジトリに反映
$ git push origin master

8. コミットメッセージの記述ポイント

1行目:変更内容の概要
2行目:空行
3行目:変更内容の詳細

9. git logコマンド例

$ git log

// 1行表示
$ git log --oneline

// 変更差分表示
$ git log -p README.md
q

// 最新3つのログ表示
$ git log -n 3

10. git diffコマンド例

// ステージングエリアとの変更差分
$ git diff

// ステージングエリアとコミット差分
$ git diff HEAD

11. gitの管理対象から外す方法

// .gitignoreファイルに管理対象を記載する

12. おわりに

参考になれば幸いです。

2020/04/29 NISHIZONO Takahiro

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