ここでは、一般的な操作や作業中に遭遇するシーンを記録します。
いくつかの概念を統一する必要があります。
- workspace(作業スペース):あなたのローカル環境
- staging area(ステージングエリア / キャッシュエリア):コマンド
git add ファイルパス
を入力すると、そのファイルの変更がこのステージングエリアに置かれます - local repository(リポジトリまたはローカルリポジトリ):コマンド
git commit -m "今回の変更の説明"
を入力すると、変更がローカルリポジトリに置かれます - remote repository(リモートリポジトリ):コマンド
git push
を入力すると、変更がリモートリポジトリに置かれます。例えば、GitHub や GitLab などです
それらの関係は以下の図のようになります。
Mermaid Loading...
以下は設定可能な一般的なエイリアス(別名)です。
alias.amd=commit --amend --no-edit
alias.am=commit --amend
alias.br=branch --sort=-committerdate
alias.ci=commit
alias.co=checkout
alias.cp=cherry-pick
alias.df=diff
alias.drop=stash drop
alias.pci=!git fetch origin master && git rebase origin/master && git push
alias.la=log --oneline --decorate --graph --all
alias.last=log -1 HEAD --stat
alias.ls=log --graph --pretty=format:'%Cred%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %C(yellow)%D%Creset' --abbrev-commit
alias.lg=log --stat
alias.pl=pull --rebase origin master
alias.pop=stash pop
alias.pr=pull --rebase --recurse-submodules
alias.rc=rebase --continue
alias.ri=rebase -i HEAD~10
alias.root=rev-parse --show-toplevel
alias.save=stash save
alias.sl=stash list
alias.st=status
alias.sup=submodule update --init --recursive
alias.unstage=reset HEAD --
また、記事で言及されているコマンドはテスト済みですが、実際の作業環境で使用する前に必ず効果を試してください。取り返しのつかない結果を防ぐためです。
すべての <>
を含む説明は変数であり、必要に応じて置き換えるもので、置き換える際にはコマンドにこの記号を含めないでください。
前のブランチに素早く切り替える#
git co -
ローカルブランチを削除する#
git branch -d <branch-name>
リモートブランチを削除する#
git push origin --delete <branch-name>
リモートで削除されたがローカルにまだ存在するブランチを削除する#
git remote prune origin
ローカルブランチの名前を変更する#
git branch -m <new-branch-name>
commit を元に戻す(新しい commit の形式で)#
git revert <commit-id>
特定の commit に戻り、その後の commit を削除する#
revert との違いは、reset または commit-id 以降のすべての commit を抹消することです。
git reset <commit-id> # デフォルトは--mixedパラメータ
# --mixed: 作業スペースの変更コードを削除せず、commitを取り消し、git add .を取り消します。
# --soft: 作業スペースの変更コードを削除せず、commitを取り消し、git add .を取り消しません。
# --hard: 作業スペースの変更コードを削除し、commitを取り消し、git add .を取り消します。
git reset --mixed HEAD^ # 前のバージョンに戻ります。HEADを現在の前のcommitにリセットし、ステージングエリアをHEADと一致させますが、作業スペースは変更されません。
git reset --soft HEAD~3 # 3つ前のバージョンに戻ります。commit情報のみが戻り、ステージングエリアと作業スペースは戻る前と同じです。再度commitする場合は、そのままcommitできます。
git reset --hard <commit-id> # 指定したcommit-idの状態に完全に戻ります。ステージングエリアと作業スペースも指定したcommit-idのバージョンの内容に変わります。
現在の変更を保存するが commit しない#
git stash
現在の変更を保存し、untrack(新規追加された)ファイルを含む#
git stash -u
すべての stash をリスト表示する#
git stash list
特定の stash 状態に戻る#
git stash apply <stash@{n}> # このnを変更
最近の状態に戻り、stash リストから削除する#
git stash pop
すべての stash を削除する#
git stash clear
特定の stash 内の特定のファイルの変更を適用する#
git diff <stash@{n}> -- <file-path> # 適用前にdiffを確認できます
git co <stash@{n}> -- <file-path>
すべての track ファイルをリスト表示する#
git ls-files -t
すべての untrack ファイルをリスト表示する#
git ls-files --others
untrack ファイルを削除する#
clean
コマンドは新しく作成されたファイルを削除するために使用できます。ファイル名を指定しない場合、すべての作業中の untracked ファイルが削除されます。注意点は 2 つあります。
- clean 後、削除されたファイルは回復できません。
- tracked ファイルの変更には影響せず、untracked ファイルのみが削除されます。
git clean <file-path> -f # 実際には手動で削除した方が良いです
untrack ディレクトリを強制的に削除する#
git clean <directory-name> -df # 新しく作成されたディレクトリとファイルが削除されるだけで、回復できません
gitignore に記録されたファイルを削除する#
git clean -X -f
A ブランチにあり B ブランチにない commit を表示する#
git log A ^B
任意のブランチの特定のファイルの内容を表示する#
git show <branch-name>:<file-name>
commit メッセージの内容キーワードで commit を検索する#
git log --all --grep='<finding-message>'