Git コマンド チートシート
Git はファイルの変更をリポジトリ内の一連のコミットとして記録します。これらのコマンドは日々の流れ——リポジトリの作成、作業の保存、ブランチ、マージ、リモートとの共有——をカバーします。学習中は手元に置いておきましょう。
| コマンド | 機能 |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
注記
- 多くのコマンドはフラグを受け付けます(-m はメッセージ、-a はすべて)。詳細は 'git help <コマンド>' を実行してください。
- ブランチ名は軽量で捨てやすいものです。機能ブランチでこまめにコミットし、その後 main にマージしましょう。
- rebase や reset といった破壊的なコマンドの前は、作業がコミットまたは stash されていることを確認してください。
よくある質問
- merge と rebase の違いは?
- merge は2つの親を持つ新しいコミットで履歴を統合します。rebase はあなたのコミットを別のブランチの上に再生し、線形の履歴を作りますがコミットIDを書き換えます。
- コミットを取り消すには?
- 'git revert <コミット>' を使うと変更を打ち消す新しいコミットを追加します(安全で共有履歴を維持)。'git reset' は履歴を書き換えるため共有ブランチでは使わないでください。
- git pull と git fetch の違いは?
- fetch はリモートからコミットと参照をダウンロードし、作業ツリーには触れません。pull は fetch してからそれらの変更を現在のブランチにマージまたはリベースします。
- git stash はいつ使う?
- stash は未コミットの変更を一時的に退避し、ブランチの切り替えやきれいなプルを可能にし、後で 'git stash pop' で戻せます。