基本操作
| コマンド | 説明 |
|---|
git init | 新しいリポジトリを初期化 |
git clone <url> | リポジトリをクローン |
git status | 作業ディレクトリの状態を確認 |
git add <file> | ファイルをステージング |
git add . | すべての変更をステージング |
git commit -m "message" | コミットを作成 |
git push | リモートにプッシュ |
git pull | リモートからプル |
ブランチ操作
| コマンド | 説明 |
|---|
git branch | ブランチ一覧を表示 |
git branch <name> | 新しいブランチを作成 |
git checkout <branch> | ブランチを切り替え |
git checkout -b <name> | 新しいブランチを作成して切り替え |
git merge <branch> | ブランチをマージ |
git branch -d <name> | ブランチを削除 |
履歴確認
| コマンド | 説明 |
|---|
git log | コミット履歴を表示 |
git log --oneline | 1行で履歴を表示 |
git log --graph | グラフ形式で表示 |
git diff | 変更差分を表示 |
git show <commit> | コミットの詳細を表示 |
取り消し操作
| コマンド | 説明 |
|---|
git reset HEAD <file> | ステージングを取り消し |
git checkout -- <file> | 作業ディレクトリの変更を取り消し |
git revert <commit> | コミットを取り消す(新しいコミットを作成) |
git reset --hard <commit> | 指定のコミットまで戻す(危険) |
リモート操作
| コマンド | 説明 |
|---|
git remote -v | リモート一覧を表示 |
git remote add <name> <url> | リモートを追加 |
git fetch | リモートの情報を取得 |
git push -u origin <branch> | 上流ブランチを設定してプッシュ |
便利なエイリアス設定
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --decorate
unstage = reset HEAD --
last = log -1 HEAD
amend = commit --amend --no-edit
トラブルシューティング
| 状況 | 解決方法 |
|---|
| コミットメッセージを間違えた | git commit --amend -m "新しいメッセージ" |
| 間違えてコミットした | git reset --soft HEAD~1 で戻してやり直し |
| マージコンフリクト発生 | ファイルを編集→git add→git commit |
| プッシュが拒否された | git pull --rebase してから再プッシュ |
| 特定のファイルだけ戻したい | git checkout <commit> -- <file> |
ブランチ運用のベストプラクティス
git checkout -b feature/user-auth
git add . && git commit -m "feat: ユーザー認証を実装"
git push -u origin feature/user-auth
git checkout main && git pull
git branch -d feature/user-auth
関連記事
← 一覧に戻る