「Gitコマンド、いつも調べてしまう」と悩んでいませんか?
現役WEBエンジニアとして、毎日Gitを使っています。
この記事では、本当によく使うGitコマンドを解説します。
目次
基本コマンド
初期設定
# ユーザー名設定
git config --global user.name "Your Name"
# メールアドレス設定
git config --global user.email "your@email.com"
# 設定確認
git config --list
リポジトリ作成・クローン
# 新規作成
git init
# クローン
git clone https://github.com/user/repo.git
# 特定ブランチをクローン
git clone -b branch-name https://github.com/user/repo.git
日常的に使うコマンド
状態確認
# 状態確認
git status
# 差分確認
git diff
# ステージング済みの差分
git diff --staged
# ログ確認
git log --oneline
ステージング・コミット
# 全ファイルをステージング
git add .
# 特定ファイルをステージング
git add filename
# コミット
git commit -m "コミットメッセージ"
# ステージングとコミットを同時に
git commit -am "コミットメッセージ"
プッシュ・プル
# プッシュ
git push origin main
# プル
git pull origin main
# フェッチ(マージなし)
git fetch origin
ブランチ操作
ブランチの基本
# ブランチ一覧
git branch
# リモートブランチも含む
git branch -a
# ブランチ作成
git branch feature/new-feature
# ブランチ切り替え
git checkout feature/new-feature
# 作成と切り替えを同時に
git checkout -b feature/new-feature
マージ・リベース
# マージ
git merge feature/new-feature
# リベース
git rebase main
# マージコミットなしでマージ
git merge --squash feature/new-feature
取り消し系コマンド
間違えた時の対処
# ステージング取り消し
git reset HEAD filename
# 直前のコミット修正
git commit --amend
# コミット取り消し(変更は残す)
git reset --soft HEAD^
# コミット取り消し(変更も消す)
git reset --hard HEAD^
# 特定ファイルの変更を取り消し
git checkout -- filename
スタッシュ
一時退避
# スタッシュ
git stash
# メッセージ付きスタッシュ
git stash save "作業中の内容"
# スタッシュ一覧
git stash list
# スタッシュを戻す
git stash pop
# 特定のスタッシュを戻す
git stash apply stash@{0}
よく使うエイリアス設定
# おすすめエイリアス
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph"
まとめ
Gitコマンドのポイント:
- 基本コマンドを覚える
- ブランチ操作を理解
- 取り消し方法を知っておく
- エイリアスで効率化
