Git

Current Branch が Dev Branch より遅れている数を計算

git rev-list --left-right --count dev...HEAD

出力(しゅつりょく)

<behind_count> <ahead_count>

(れい)現在(げんざい) のブランチが dev ブランチより 5 コミット(おく) れ、2 コミット先行(せんこう) ):

5 2

特定のブランチにチェックアウト

方法(ほうほう) 1:

git checkout <hash> # detached head
git branch -D feature/xxx # delete local branch
git checkout -b feature/xxx # create new local branch
git push -f origin feature/xxx # push remote

方法(ほうほう) 2:

git checkout feature/xxx
git reset --hard <commit_hash_before_dev_pull>  # 特定のコミットに戻る
git push -f origin feature/xxx

方法(ほうほう) 3:

git checkout feature/xxx
git revert <commit_hash1> <commit_hash2> ...     # dev ブランチからの変更を1つずつ取り消す
git push origin feature/xxx                      # 取り消し結果をプッシュ

reset vs revert

特性(とくせい)git reset --hardgit revert
履歴(りれき) 処理(しょり)指定(してい) コミット以降(いこう) のすべての履歴(りれき)破棄(はき)すべての履歴(りれき)保持(ほじ) し、(ぎゃく) コミットを追加(ついか)
操作(そうさ) 結果(けっか)ブランチを特定(とくてい) のコミット状態(じょうたい)(もど)指定(してい) コミットを()()(ぎゃく) コミットを作成(さくせい)
コミット保持(ほじ)以降(いこう) のコミットを保持(ほじ) せず、完全(かんぜん)削除(さくじょ)すべてのコミットを保持(ほじ)(ぎゃく) コミットで変更(へんこう)取消(とりけし)
コンフリクト処理(しょり)以降(いこう) のすべての変更(へんこう)破棄(はき) するため、コンフリクトが発生(はっせい) しにくいコンフリクトを解決(かいけつ) する必要(ひつよう) がある
追跡(ついせき) 可能性(かのうせい)追跡(ついせき) 不可(ふか)履歴(りれき)破棄(はき) される追跡(ついせき) 可能(かのう)(ぎゃく) コミットで完全(かんぜん)履歴(りれき)保持(ほじ)

最後のコミットを取り消す

変更(へんこう) を working directory に保持(ほじ)

git reset --soft HEAD~1

reflog

git reflog show
git reflog show $(git branch --show-current)

log

git log --oneline
git log --oneline --graph --all

不要なコミットを削除

git checkout feature/a-123
git rebase -i HEAD~N

不要(ふよう)(ぎょう)pickdrop変更(へんこう) し、実行(じっこう)

git push origin feature/a-123 --force

プッシュをシミュレート

git push --force --dry-run

ブランチを強制削除

git branch -D branch_name

ブランチ間のコード行数を比較

git diff --shortstat <base-commit> feature-branch

Worktree

適用(てきよう) シナリオ:hotfix を処理(しょり) する必要(ひつよう) があるが、現在(げんざい)作業(さぎょう) を stash したくない場合(ばあい)

### 現在の位置を確認 ```bash git branch # feature/l-001 ``` ### main から hotfix worktree を作成 ```bash git worktree add -b hotfix-123 .worktrees/hotfix-123 main ``` または: ```bash git worktree add .worktrees/hotfix-123 main ``` ```bash git worktree list /workspace/git-playground 0e439b8 [feature/l-001] /workspace/git-playground/.worktrees/hotfix-123 0e439b8 [main] ``` ### hotfix ディレクトリに切り替え ```bash cd .worktrees/hotfix-123 ``` 2番目(ばんめ)方法(ほうほう)使用(しよう) した場合(ばあい)追加(ついか) で checkout が必要(ひつよう) : ```bash git checkout -b hotfix-123 ``` ### hotfix ブランチで問題を修正 ```bash # ファイルを修正 git add . git commit -m "fix: issue description" ``` ### hotfix をプッシュしてマージ ```bash git push origin hotfix-123 # リモートで PR & Merge を完了 ``` ### プロジェクトルートに戻る ```bash cd ../.. ``` ### hotfix worktree をクリーンアップ ```bash git worktree remove .worktrees/hotfix-123 git fetch git pull origin main ``` ### 元の feature ブランチの開発作業を継続

Demo

git worktree add ../worktree/feature/ABC-12345 prod.1.112.4 -b feature/ABC-12345

Revert / Reset / Rebase

git revert HEAD

git reset HEAD
git reflog
# 3つのモード:mixed, soft, hard

git restore .

バージョン切り替え

git checkout <commit_number>

git checkout main

git checkout HEAD~   # HEAD の1つ前のバージョンに戻る
git checkout HEAD~2  # HEAD の2つ前のバージョンに戻る