Skip to main content

CLI

ここで記載しているコマンドは基本的にカレントディレクトリに.gitが存在するものとします。

github CLI

githubのログイン(要 ブラウザによる認証)
gh auth login -w -p https 

git config

設定の一覧表示

git config -l
使用例
$ git config -l
safe.directory=*
credential.helper=store
alias.tree=log --graph --all --pretty=format:'%x09%C(auto) %h %Cgreen %ar %Creset%x09by"%C(cyan ul)%an%Creset" %x09%C(auto)%s %d'
init.defaultbranch=main
user.name=gmnk
user.email=maqt01476497@gmail.com

safe.directory

以下コマンドでgitの脆弱性対策をoffにしてます。(開発用以外はoffにしない方が望ましいです。)

git config --global safe.directory '*'

credential.helper

以下コマンドで初回時以降はユーザー名とパスワードの入力が省略されます(gitlab等)

git config --global credential.helper store

git log

ログを確認

コミットログを表示します。

git log [ログの出力数]
使用例(ログを3つ出力)
git log -3

ログをグラフ表示(推奨)

コミットログをグラフ表示します。
参考サイト

git log --graph --all --pretty=format:'%x09%C(auto) %h %Cgreen %ar %Creset%x09by"%C(cyan ul)%an%Creset" %x09%C(auto)%s %d'
info

長いコマンドである為、以下コマンドでgitエイリアスを追加することを推奨します。

Linux(エスケープシーケンスに'\'を使用)
git config --global alias.tree "log --graph --all --pretty=format:'%x09%C(auto) %h %Cgreen %ar %Creset%x09by"\"%C(cyan ul)%an%Creset\" %x09%C(auto)%s %d'"
WWindows(エスケープシーケンスに'ダブルクォーテーション'を使用)
git config --global alias.tree "log --graph --all --pretty=format:'%x09%C(auto) %h %Cgreen %ar %Creset%x09by""%C(cyan ul)%an%Creset"" %x09%C(auto)%s %d'"

gitエイリアス追加後は、以下コマンドでグラフ表示が可能です。

git tree

git clone

カレントディレクトリ(/home/goma/)にmainブランチをcloneする場合のコマンドは以下の通りです。
-bオプションはブランチ名を指定する際に使用します。
-bを省略時はHEADブランチになります。

git clone -b main https://github.com/gmnk616/devdoc.git

/home/goma/pv-devdocmainブランチをcloneする場合のコマンドは以下の通りです。
(ディレクトリ名は/home/goma/pv-devdocになります。)

git clone -b main https://github.com/gmnk616/devdoc.git /home/goma/setup-wslenv

git init

現在のディレクトリまたは指定したディレクトリに「.git」というリポジトリを構成するディレクトリを作成するコマンド

カレントディレクトリに.gitを作成

git init

指定したディレクトリに.gitを作成

git init /home/goma/testgit/

git add

以下コマンドで以下項目がステージング対象となります。
基本的に以下-Aオプションの使用を推奨します

  • 変更されたファイル
  • 削除されたファイル
  • 名前変更されたファイル
  • 新しく作られたファイル
git add -A

以下コマンドで以下項目がステージング対象となります。

  • 変更されたファイル
  • 削除されたファイル
git add -u

git status

git addしたファイル一覧の確認等に使用します。

git status

git commit

コミット

git addしたファイル一覧のコミットに使用します。
-mオプションはコメントです。

git commit -m "初回登録"

以下のように-mオプションを省略すると、コメント入力にデフォルトのエディタが起動します。
改行付のコメント入力時は以下を推奨

git commit

コメント入力画面(画面はnano)でハイライト表示箇所が追加したコメントです。
nanoの場合コメント入力後にCtrl + Xを入力して、
Save modified buffer?y(Y)を入力してEnterキーを選択するとコメントが入力されます。

TODO: 現在記載中です

直前コミットのコメントを修正

git commit --amend -m "コメント"
tip

前のコメントをpush済の場合は-fオプション付きで強制的にプッシュ
(極力push前に修正できるのが望ましいです。)

git push -f

git remote

リモートリポジトリ追加

git pushする前に実行する必要があるコマンドです。
カレントディレクトリの.gitリモートリポジトリ追加したいリポジトリが追加されます。
一度追加すれば以降はgit remote addしなくてもgit pushが可能になります

git remote add [追加するリモートリポジトリ名] [追加したいリポジトリ]

コマンドの具体例は以下になります。

git remote add origin https://github.com/gmnk616/devdoc.git

リモートリポジトリ詳細

git remote -v

git push

git push [作成したリポジトリ名] [ブランチ名]

コマンドの具体例は以下になります。

git push origin main

git fetch

git fetch

リモートに存在しないブランチをローカルから取り除く場合は、-p("prune"取り除く)を付けます。

git fetch -P

git branch

ブランチを作成(現在のカレントブランチに作成)

git branch [ブランチ名]

ローカル&リモートブランチ一覧を表示

git branch -a

現在のブランチを確認

git branch --contains

ブランチを削除

git branch -d [ブランチ名]

push, mergeされていないブランチを強制的に削除したい場合は、代わりに-Dオプションを使用します

git branch -D [ブランチ名]

作成したブランチ名を変更

作成したmasterブランチをmainに変更
git branch -m master main

git checkout

gitブランチ(カレントブランチ)を切り替えます

git checkout [ブランチ名]
使用例(ログを3つ出力)

以下masterブランチがカレントブランチの状態で、developブランチをカレントブランチにする例を示します

# 現在のカレントブランチを確認
$ git branch --contains
develop
* master

# カレントブランチをdevelopに切替
$ git checkout develop
Switched to branch 'develop'

# カレントブランチがdevelopに切り替わったことを確認
$ git branch --contains
* develop
master

git merge

fast forwardあり

git merge [ブランチ名]
developの内容をmainにマージ

TODO:記載中

fast forwardなし

git merge --no-ff [ブランチ名]
developの内容をmainにマージ

TODO:記載中

git tag

タグ一覧を表示

git tag

git stash

参考サイトを以下に示します。
【Git】stashコマンドのまとめと使い方 〜変更差分の一時退避〜
【git】git stashの使い方

git reflog(現在のHEADブランチを調べる)

git reflog [ログの出力数]
使用例
$ git reflog -3
4669055 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: checkout: moving from main to main
4669055 (HEAD -> main, origin/main, origin/HEAD) HEAD@{1}: branch: Reset to remotes/origin/main
4669055 (HEAD -> main, origin/main, origin/HEAD) HEAD@{2}: checkout: moving from main to main