Git シーン別活用
シーン | コマンド | 説明 |
空のcommitを作成する | git commit --allow-empty -m | ㅤ |
addをスキップして直接commitする
tracked ファイルのみ自動 add | git commit -a -m | ㅤ |
直前のcommitを修正する | git commit --amend -m
git push --force | 最後のcommitのみ直接修正(amend)可能。それ以前のcommitを修正する場合はrebaseが必要で、SHA-1は変更される。
すでにリモートにpushしている場合は、git push --force が必要。
それ以外の場合、mergeの履歴が残ってしまう。 |
直前のcommitに変更を追加する | git add .
git commit --amend --no-edit
git push --force | SHA-1は変更される。
既にリモートにpush済みの場合、git push --force が必要。
そうしないとconflictが発生する。 |
複数のcommitを1つにまとめる、
commitを削除する、または順番を変更する
| git rebase -i
git rebase -i --root | |
Gitで空のフォルダを追加する | mkdir images
touch images/.keep | 空のフォルダはGitで追跡できないため、
.keep という空ファイルを作成して追跡する必要がある。 |
ファイルの各行ごとに、作者と日時を表示する | git blame | ㅤ |
2つ前のcommitのディレクトリで現在のファイルを上書きする | git checkout HEAD~2 . | indexも更新される。 |
コードがまだpushされていない場合、commitを取り消してやり直す、
ommitを取り消した後に再び元のcommitを復元 | git reset e12d8ef^もしくは
git reset master^もしくは
git reset HEAD^もしくは
git reset e12d8ef | 実は特定のcommitに移動することです
^ は直前の親commit、~n は n 個前のcommitを表す
mixedモード
softモード
hardモード |
コードが既にpushされている場合、新しいcommitを作って巻き戻す | git revert HEAD --no-edit | 実は新しいcommitを作って巻き戻す
2回実行すると実質的に元に戻す |
作業ディレクトリのすべての変更を破棄する | git restore .
git clean -fd | ㅤ |
削除・上書きしてしまった commit を確認する | git reflog | 実はHEADの移動履歴を確認することです |
リモートブランチを完全に削除する | git checkout --orphan temp-branch
git commit --allow-empty -m
git push --force origin temp-branch:branch | 親のないブランチを作成する
空のcommitを作成
ローカルのブランチでリモートの branch ブランチを強制的に上書きする |
ローカルのブランチを削除する | git branch -d branch-name | ㅤ |
リモートのブランチを削除する | git push origin --delete branch-name | ㅤ |
リモートに新しいブランチを作成する | git checkout -b new-feature
git push -u origin new-feature | -u リモートブランチとローカルブランチを紐付ける |
ブランチ一覧を表示 | git branch -a | ローカル&リモートのブランチ一覧 |
ローカルブランチの名前を変更する | git branch -m cat tiger | ブランチ名 cat を tiger に変更する |
特定のcommit上に新しいブランチを作成する | git branch bird 657fce7 | 657fce7 上に bird というブランチを作成 |
マージする | git merge dogもしくは
git rebase dog | 現在のブランチに dog ブランチをマージする
現在のブランチに dog ブランチをマージするが,SHA-1を変わるはずですm |
You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you merge | git add .
git commit(merge)
git rebase --continue(rabase) | マージ後にコンフリクトを解決したが、まだコミットしていない状態 |
マージを途中で放棄 | git merge --abort
git rebase --abort | ㅤ |
作業の途中でcommitせずに一時的に変更を退避する | git add .
git commit -m
git reset HEAD^
もしくは
git stash
git stash pop stash@{2} | ㅤ |
他のブランチの一部ファイルやディレクトリだけ欲しい | git cherry-pick 6a498ec | SHA-1は新しく計算される |
ローカルブランチをリモートのブランチに接続する | git remote add origin git@github.com | ㅤ |
現在の作業内容を一時保存して、別のブランチで復元する | git stash -u
git checkout xxx
git stash pop | ㅤ |
VimでNormalモードに入る | ESC or ctrl+[ | ㅤ |
VimでInsertモードに入る | i = insert
a = append
o = new line | ㅤ |
Vim を終了 | :w save
:q quit
:wq save and quit | ㅤ |