よく使うGitコマンド
開発ブランチの最新の変更を取り込む場合:
# developブランチの最新を取り込むgit rebase develop
# コンフリクトが発生した場合git add .git rebase --continue
# リベース完了後、強制プッシュgit push origin feature/#211-hoge -f安全なリベースの手順
Section titled “安全なリベースの手順”# 1. 現在の作業を保存git stash
# 2. developブランチを最新に更新git checkout developgit pull origin develop
# 3. 作業ブランチに戻ってリベースgit checkout feature/#211-hogegit rebase develop
# 4. 保存した作業を戻すgit stash pop直前のコミット取り消し
Section titled “直前のコミット取り消し”コミット内容を残して取り消す(推奨)
Section titled “コミット内容を残して取り消す(推奨)”# 直前のコミットを取り消し(変更内容はステージングに残る)git reset --soft HEAD^コミットと変更内容を取り消す
Section titled “コミットと変更内容を取り消す”# 直前のコミットと変更内容を取り消し(危険)git reset --hard HEAD^コミットメッセージだけ修正
Section titled “コミットメッセージだけ修正”# 直前のコミットメッセージを修正git commit --amend -m "新しいコミットメッセージ"一時的に変更を退避させる場合:
基本的な使い方
Section titled “基本的な使い方”# 現在の変更を退避git stash
# メッセージ付きで退避git stash save "作業中の機能"
# 退避した変更の一覧git stash list
# 最新の退避を適用(退避リストから削除)git stash pop
# 特定の退避を適用git stash pop stash@{0}高度な使い方
Section titled “高度な使い方”# 退避を適用するが、リストには残すgit stash apply stash@{0}
# 特定の退避を削除git stash drop stash@{0}
# すべての退避を削除git stash clear
# 退避の内容を確認git stash show -p stash@{0}その他の便利なコマンド
Section titled “その他の便利なコマンド”ブランチ操作
Section titled “ブランチ操作”# ブランチ作成と切り替えを同時にgit checkout -b feature/new-feature
# リモートブランチを追跡git checkout -b feature/remote-feature origin/feature/remote-feature
# ブランチ名を変更git branch -m old-name new-nameコミット履歴
Section titled “コミット履歴”# 簡潔な履歴表示git log --oneline -10
# グラフ表示git log --graph --oneline --all
# 特定ファイルの変更履歴git log -p path/to/file# ステージングされていない変更を確認git diff
# ステージングされた変更を確認git diff --staged
# 特定のコミット間の差分git diff commit1..commit2リモート操作
Section titled “リモート操作”# リモートブランチを削除git push origin --delete branch-name
# すべてのリモートブランチを取得git fetch --all
# リモートの情報を確認git remote -v危険な操作(慎重に使用)
Section titled “危険な操作(慎重に使用)”# 特定のコミットまで戻す(履歴を書き換える)git reset --hard <commit-hash>
# 強制プッシュ(チーム開発では要相談)git push --force-with-lease origin branch-name
# ファイルをGit管理から除外(ファイル自体は残る)git rm --cached file-nameトラブルシューティング
Section titled “トラブルシューティング”マージコンフリクトの解決
Section titled “マージコンフリクトの解決”# コンフリクトしているファイルを確認git status
# コンフリクトを解決後git add <resolved-file>git commit間違えてコミットした場合
Section titled “間違えてコミットした場合”# 特定のファイルを前のコミットから除外git reset HEAD^ path/to/filegit commit --amendリベースを中止
Section titled “リベースを中止”# リベース作業を中止して元の状態に戻すgit rebase --abort