Git CLI

Branch

# ローカルブランチのみ表示
git branch

# すべてのブランチ
git branch -a

# ブランチ名変更
git branch -m [old_branch] [new_branch]
git branch -m master main

# ブランチ削除
git branch -d <branch_name>

# ブランチ強制削除
git branch -D <branch_name>

Clone

git clone [repo_url]

Config

# 初期ブランチ名を main に設定
git config --global init.defaultBranch main

# git ユーザー情報を設定
git config --global user.email "Your email"
git config --global user.name "Your Name"

# リモート repo URL を取得
git config --get remote.origin.url

# 改行文字設定を確認
git config --global core.autocrlf

# Git の自動変換を無効化
git config core.autocrlf false

Checkout

# ブランチに切り替え
git checkout <branch_name>

# ブランチを作成して切り替え
git checkout -b <branch_name>

Log

git log --oneline

git log --oneline --graph

git log --oneline --graph --all

Merge

# [branch-name] を現在のブランチにマージ
git merge <branch_name>

# fast-forward なしでマージ
git merge --no-ff develop

Pull / Push

git pull

git pull origin main

# 初回プッシュ(upstream 設定付き)
git push -u [Remote_Host] [Local_branch_name]:[Remote branch name]

Reset

# 指定コミットに戻る
git reset [hash_number]

git reset [hash_number]^

git reset [hash_number]~5

# Soft reset(変更を保持)
git reset --soft HEAD~1

# 単一ファイルを Reset
git reset <filename>

Remote

# リモート repos URL を表示
git remote -v

# 新しいリモート URL を設定
git remote set-url origin [new-url]

# リモートを追加
git remote add [name] <url>
git remote add origin <url>

Status

git status

Stash

# キャッシュされた未コミットファイルを復元
git stash pop

Tag

# タグを作成
git tag v0.1.0

# タグをプッシュ
git push origin v0.1.0

# タグを削除
git tag -d <old-tag-name>

# 注釈付きタグを作成
git tag -a <new-tag-name> -m "Tag message"

ファイルの改行文字を確認

git ls-files --eol

Use Cases

リモート Git ブランチ名を変更

git branch -a

git push origin --delete <old-name>

git push origin -u <new-name>

単一(たんいつ) コマンド:

git push origin :<old-name> <new-name>

GitHub SSH を設定

SSH キーを作成(さくせい)

ssh-keygen -t ed25519 -C "your_email@example.com"

SSH キーを ssh-agent に追加(ついか)

eval "$(ssh-agent -s)"

接続(せつぞく) テスト:

ssh -T git@github.com

または:

exec ssh-agent zsh

SSH 秘密(ひみつ) キーを ssh-agent に追加(ついか)

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_github

すべての agent を表示(ひょうじ)

ssh-add -l

SSH Agent

eval `ssh-agent -s`

ssh-add ~/.ssh/id_ed25519

cat ~/.ssh/id_ed25519.pub

GitHub を known hosts に追加(ついか)

ssh-keyscan -H github.com >> ~/.ssh/known_hosts

Git Flow

init

git flow init

feature

# feature ブランチを作成
git flow feature start my-feature

git add .
git commit -m "Add new feature"

# develop にマージ
git flow feature finish my-feature

bugfix

git flow bugfix start fix-bug

git add .
git commit -m "Fix bug"

# develop にマージ
git flow bugfix finish fix-bug

merge

# Feature または Bugfix を main ブランチにマージ
git checkout main
git merge --no-ff develop
git push origin main

hotfix

git flow hotfix start fix-production-bug

git add .
git commit -m "Fix critical production bug"

git flow hotfix finish fix-production-bug

.gitignore

vi .gitignore
# ファイル名
# フォルダ名/
# *.log

:wq  # ファイルを保存して終了

git add .
git commit -m "Added .gitignore"

Tree

tree -d -I 'target|test'