0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Git コマンド完全チートシート(GitFlow 前提)

Last updated at Posted at 2025-03-09

GitFlow の基本ブランチ

  • master/main: 本番環境のコード
  • develop: 開発中のコード
  • feature/xxx: 新機能開発用
  • release/xxx: リリース準備用
  • hotfix/xxx: 緊急バグ修正用

初期設定

基本設定

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

# エディタの設定
git config --global core.editor "vim"

# 改行コードの設定
git config --global core.autocrlf input  # Mac/Linux
git config --global core.autocrlf true   # Windows

# すべての新規リポジトリで「main」をデフォルト
git config --global init.defaultBranch main

既存リポジトリのクローン

git clone <repository-url>
cd <repository-directory>

新規リポジトリの作成

1. ローカルでプロジェクトフォルダを作成(ターミナル)

mkdir project-name
cd project-name

2. リモートリポジトリを作成(GitHub)

  1. GitHubにログインします
  2. 右上の「+」アイコンをクリックし、「New repository」を選択
  3. リポジトリ名(例:project-name)を入力
  4. 説明文(オプション)を入力
  5. リポジトリを公開するか非公開にするか選択
  6. 「Initialize this repository with a README」のチェックを外したままにする
  7. 「Create repository」をクリック

3. ローカルリポジトリの初期化とGitHubへの接続(ターミナル)

echo "# project-name" >> README.md
git init
git add README.md
git commit -m "最初のコミット"
git branch -M main
git remote add origin https://github.com/{username}/project-name.git
git push -u origin main

{username} は自分のGitHubユーザー名に置き換えてください。

パターン別操作手順

1. 新規機能開発

開発開始時

# developブランチが最新であることを確認
git checkout develop
git pull origin develop

# featureブランチを作成して切り替え
git checkout -b feature/new-function-name

# 最初のコミット
git add .
git commit -m "feat: 新機能の実装を開始"

開発作業中

# 変更の確認
git status
git diff

# 変更のステージング
git add <file-name>  # 特定のファイル
git add .            # すべての変更

# コミット
git commit -m "feat: 〇〇機能を実装"

# リモートへのプッシュ(初回)
git push -u origin feature/new-function-name

# リモートへのプッシュ(2回目以降)
git push

開発ブランチの更新(develop の変更を取り込む)

# 現在の作業を保存
git stash

# developブランチの最新化
git checkout develop
git pull origin develop

# featureブランチに戻って統合
git checkout feature/new-function-name
git merge develop

# 保存した作業を復元
git stash pop

# コンフリクトがあれば解決

機能完成後のマージ

# ローカルの変更をコミットしプッシュ
git add .
git commit -m "feat: 新機能の実装完了"
git push

# Pull Requestを作成(GitHub/GitLabなどのUIから)
# レビュー後、developブランチにマージ

2. 既存機能の改修

改修開始時

# developブランチが最新であることを確認
git checkout develop
git pull origin develop

# featureブランチを作成して切り替え
git checkout -b feature/improve-existing-function

# 最初のコミット
git add .
git commit -m "fix: 既存機能の改修を開始"

改修作業中

# 変更した内容を確認
git status
git diff

# 変更をステージング
git add <file-name>

# コミット(プレフィックスに注意)
git commit -m "fix: 〇〇の問題を修正"
git commit -m "refactor: 〇〇のコードをリファクタリング"
git commit -m "perf: 〇〇の処理を最適化"

# リモートへプッシュ
git push -u origin feature/improve-existing-function

改修完了後

# 最終的な変更をコミット
git add .
git commit -m "fix: 既存機能の改修完了"
git push

# Pull Requestを作成(GitHub/GitLabなどのUIから)
# レビュー後、developブランチにマージ

3. リリース準備

リリースブランチの作成

# developブランチが最新であることを確認
git checkout develop
git pull origin develop

# リリースブランチを作成
git checkout -b release/v1.0.0
git push -u origin release/v1.0.0

リリース前の修正

# リリース前のバグ修正
git add <file-name>
git commit -m "fix: リリース前の〇〇を修正"
git push

リリースの完了

# masterにマージ
git checkout master
git pull origin master
git merge --no-ff release/v1.0.0
git tag -a v1.0.0 -m "バージョン1.0.0をリリース"
git push origin master
git push origin v1.0.0  # タグをプッシュ

# developにも反映
git checkout develop
git pull origin develop
git merge --no-ff release/v1.0.0
git push origin develop

# リリースブランチの削除
git branch -d release/v1.0.0
git push origin --delete release/v1.0.0

4. 緊急バグ修正(Hotfix)

Hotfixブランチの作成

# masterブランチから分岐
git checkout master
git pull origin master
git checkout -b hotfix/critical-bug-fix

修正作業

# バグ修正
git add <file-name>
git commit -m "fix: 重大なバグを修正"
git push -u origin hotfix/critical-bug-fix

Hotfixの完了

# masterにマージ
git checkout master
git pull origin master
git merge --no-ff hotfix/critical-bug-fix
git tag -a v1.0.1 -m "バグ修正バージョン1.0.1"
git push origin master
git push origin v1.0.1  # タグをプッシュ

# developにも反映
git checkout develop
git pull origin develop
git merge --no-ff hotfix/critical-bug-fix
git push origin develop

# hotfixブランチの削除
git branch -d hotfix/critical-bug-fix
git push origin --delete hotfix/critical-bug-fix

便利なコマンド集

ブランチ操作

# ブランチ一覧表示
git branch          # ローカルブランチのみ
git branch -a       # リモートブランチも含む
git branch -v       # 最新のコミットも表示

# ブランチの切り替え
git checkout <branch-name>
git switch <branch-name>  # Git 2.23以降

# ブランチの作成と切り替え
git checkout -b <new-branch-name>
git switch -c <new-branch-name>  # Git 2.23以降

# ブランチの削除
git branch -d <branch-name>      # マージ済みのみ削除可能
git branch -D <branch-name>      # 強制削除

# リモートブランチの削除
git push origin --delete <branch-name>

変更の確認と取り消し

# 変更状態の確認
git status
git status -s       # 短縮表示

# 差分の確認
git diff            # ワークツリーとステージングの差分
git diff --staged   # ステージングとリポジトリの差分

# 変更の取り消し
git checkout -- <file-name>   # ファイルの変更を取り消し(Git 2.23未満)
git restore <file-name>       # ファイルの変更を取り消し(Git 2.23以降)

# ステージングの取り消し
git reset HEAD <file-name>    # ステージングを取り消し(Git 2.23未満)
git restore --staged <file-name>  # ステージングを取り消し(Git 2.23以降)

コミット操作

# コミット
git commit -m "コミットメッセージ"
git commit -a -m "メッセージ"  # ステージングなしでコミット(追跡ファイルのみ)

# コミット履歴の確認
git log
git log --oneline           # 1行表示
git log --graph --oneline   # グラフ付き簡易表示
git log -p                  # 変更内容も表示
git log -n 5               # 最新5件のみ表示

# 直前のコミットの修正
git commit --amend -m "新しいコミットメッセージ"

# コミットの打ち消し(新たなコミットを作成)
git revert <commit-hash>

# コミットの取り消し(履歴を書き換え)
git reset --soft HEAD^    # コミットを取り消し、変更はステージングに残す
git reset --mixed HEAD^   # コミットを取り消し、変更はワークツリーに残す
git reset --hard HEAD^    # コミットを取り消し、変更も削除する(危険)

マージとリベース

# ブランチのマージ
git merge <branch-name>
git merge --no-ff <branch-name>  # 常にマージコミットを作成

# マージの中止
git merge --abort

# リベース
git rebase <branch-name>

# インタラクティブリベース
git rebase -i HEAD~3  # 直近3コミットを編集

リモートリポジトリ操作

# リモートの確認
git remote -v

# リモートの追加
git remote add <name> <url>

# リモートからフェッチ
git fetch <remote>
git fetch --all

# リモートからプル
git pull <remote> <branch>
git pull --rebase <remote> <branch>  # リベースでプル

# リモートへプッシュ
git push <remote> <branch>
git push -u <remote> <branch>  # ブランチの追跡設定も行う
git push --force-with-lease    # 強制プッシュ(安全)

一時保存(Stash)

# 作業の一時保存
git stash
git stash save "作業中のメモ"

# 保存リストの確認
git stash list

# 保存した作業の復元
git stash apply          # 最新の保存を復元(保存は残る)
git stash apply stash@{n}  # 特定の保存を復元
git stash pop            # 最新の保存を復元して削除

# 保存の削除
git stash drop stash@{n}
git stash clear          # すべての保存を削除

タグ操作

# タグの作成
git tag v1.0.0
git tag -a v1.0.0 -m "バージョン1.0.0"

# タグの表示
git tag
git show v1.0.0

# タグのプッシュ
git push origin v1.0.0
git push origin --tags   # すべてのタグをプッシュ

# タグの削除
git tag -d v1.0.0
git push origin --delete v1.0.0  # リモートのタグを削除

競合解決

# 競合ファイルの確認
git status

# マージツールで競合解決
git mergetool

# 競合の解決後
git add <resolved-files>
git commit

コミットメッセージ規約(Conventional Commits)

<type>(<scope>): <subject>

<body>

<footer>

主なタイプ

  • feat: 新機能
  • fix: バグ修正
  • docs: ドキュメントのみの変更
  • style: コードの意味に影響を与えない変更(空白、フォーマット、セミコロンの欠落など)
  • refactor: バグの修正や機能の追加ではないコード変更
  • perf: パフォーマンスを向上させるコード変更
  • test: 不足しているテストの追加や既存のテストの修正
  • chore: ビルドプロセスや補助ツールの変更

feat(auth): ユーザー認証機能を追加

- ログイン画面の実装
- JWT認証の実装
- セッション管理の実装

Closes #123
0
1
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?