バージョン管理に必要な Git コマンド - ブランチ、コミット、マージ、コラボレーション。
Git を構成し、ユーザー情報を設定する
# 名前とメールアドレスを設定する
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 設定を確認する
git config --list
# デフォルトのブランチ名を設定する
git config --global init.defaultBranch main# デフォルトエディタを設定する
git config --global core.editor "code --wait"
# カラ出力を有効にする
git config --global color.ui auto
# 認証情報ヘルパーを設定する(Windows)
git config --global credential.helper wincredGit リポジトリを初期化またはクローンする
# 新しいリポジトリを初期化する
git init
# 特定のブランチ名で初期化する
git init -b main
# 既存のリポジトリをクローンする
git clone https://github.com/user/repo.git
# 特定のディレクトリにクローンする
git clone https://github.com/user/repo.git my-folder日常的な Git 使用のための一般的なコマンド
# リポジトリの状態を確認する
git status
# ファイルをステージングエリアに追加する
git add file.txt
git add . # すべてのファイルを追加
git add *.js # すべての JavaScript ファイルを追加
# 変更をコミットする
git commit -m "Commit message"
git commit -am "Message" # 追跡中のファイルを追加してコミットする
# コミット履歴を表示する
git log
git log --oneline # コンパクト表示
git log --graph # ブランチのグラフを表示するブランチの作成と管理
# ブランチを一覧表示する
git branch # ローカルブランチ
git branch -a # すべてのブランチ(ローカル+リモート)
# 新しいブランチを作成する
git branch feature-branch
# ブランチに切り替える
git checkout feature-branch
git switch feature-branch # 現代的な代替手段
# 新しいブランチを作成して切り替える
git checkout -b feature-branch
git switch -c feature-branch# ブランチを現在のブランチにマージする
git merge feature-branch
# ブランチを削除する
git branch -d feature-branch # 安全に削除
git branch -D feature-branch # 強制削除
# ブランチの名前を変更する
git branch -m old-name new-nameリモートリポジトリを操作する
# リモートリポジトリを一覧表示する
git remote -v
# リモートリポジトリを追加する
git remote add origin https://github.com/user/repo.git
# リモートから変更を取得する
git fetch origin
# リモートから変更をプルする
git pull origin main
# リモートへ変更をプッシュする
git push origin main
git push -u origin main # アップストリームを設定してプッシュする# リモートを削除する
git remote remove origin
# リモートの名前を変更する
git remote rename old-name new-name
# リモート URL を変更する
git remote set-url origin https://github.com/user/new-repo.git変更を元に戻すまたはリセットする
# 作業ディレクトリの変更を破棄する
git restore file.txt
git checkout -- file.txt # 従来の方法
# ステージング解除する
git restore --staged file.txt
git reset HEAD file.txt # 従来の方法
# 直前のコミットを修正する
git commit --amend -m "New message"
# コミットをリバートする(新しいコミットを作成)
git revert <commit-hash># 直前のコミットにリセットする(注意!)
git reset --soft HEAD~1 # 変更をステージされたままにする
git reset --mixed HEAD~1 # 変更をステージされていない状態に保つ
git reset --hard HEAD~1 # すべての変更を破棄する
# 特定のコミットにリセットする
git reset --hard <commit-hash>変更を一時的に保存する
# 現在の変更をスタッシュする
git stash
git stash save "Work in progress"
# スタッシュ一覧を表示する
git stash list
# スタッシュを適用する
git stash apply # スタッシュを保持する
git stash pop # 適用してスタッシュを削除する
# 特定のスタッシュを適用する
git stash apply stash@{0}
# スタッシュを削除する
git stash drop stash@{0}
git stash clear # すべてのスタッシュを削除する差分と履歴を検査する
# ステージされていない変更を表示する
git diff
# ステージされた変更を表示する
git diff --staged
git diff --cached
# コミット間の変更を表示する
git diff commit1 commit2
# 特定のファイルの変更を表示する
git diff file.txt# コミットの詳細を表示する
git show <commit-hash>
# 特定のコミット時点のファイルを表示する
git show <commit-hash>:file.txt
# ファイルの変更者と変更箇所を表示する
git blame file.txt追加の便利な Git コマンド
# 未追跡ファイルをクリーンする
git clean -n # ドライラン
git clean -f # ファイルを削除する
git clean -fd # ファイルとディレクトリを削除する
# リポジトリ内を検索する
git grep "search term"
# ファイルの変更者を表示する
git log --follow file.txt
# エイリアスを作成する
git config --global alias.st status
git config --global alias.co checkout# ファイルの変更を無視する
git update-index --assume-unchanged file.txt
# 再度ファイルの変更を追跡する
git update-index --no-assume-unchanged file.txt
# 追跡中のファイルを表示
git ls-files