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 --hard | git revert |
|---|---|---|
| 履歴 処理 | 指定 コミット以降 のすべての履歴 を破棄 | すべての履歴 を保持 し、逆 コミットを追加 |
| 操作 結果 | ブランチを特定 のコミット状態 に戻 す | 指定 コミットを取 り消 す逆 コミットを作成 |
| コミット保持 | 以降 のコミットを保持 せず、完全 に削除 | すべてのコミットを保持 、逆 コミットで変更 を取消 |
| コンフリクト処理 | 以降 のすべての変更 を破棄 するため、コンフリクトが発生 しにくい | コンフリクトを解決 する必要 がある |
| 追跡 可能性 | 追跡 不可 、履歴 が破棄 される | 追跡 可能 、逆 コミットで完全 な履歴 を保持 |
最後のコミットを取り消す
変更 を working directory に保持 :
git reset --soft HEAD~1reflog
git reflog showgit reflog show $(git branch --show-current)log
git log --onelinegit log --oneline --graph --all不要なコミットを削除
git checkout feature/a-123
git rebase -i HEAD~N不要
な行
の pick を drop に変更
し、実行
:
git push origin feature/a-123 --forceプッシュをシミュレート
git push --force --dry-runブランチを強制削除
git branch -D branch_nameブランチ間のコード行数を比較
git diff --shortstat <base-commit> feature-branchWorktree
適用 シナリオ: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-12345Revert / 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つ前のバージョンに戻る