コミット、ブランチ、マージ、リベース、リモートなどの必須Gitコマンド。
git config --global user.name "Name"セットアップ & 設定Set global author name
例: git config --global user.name "Jane Doe"
git config --global user.email "email"セットアップ & 設定Set global author email
例: git config --global user.email "[email protected]"
git config --listセットアップ & 設定List all configuration settings
git config --global core.editor "code"セットアップ & 設定Set VS Code as default editor
git config --global alias.<alias> <cmd>セットアップ & 設定Create a command alias
例: git config --global alias.st status
git initセットアップ & 設定Initialize a new local repository
git init <dir>セットアップ & 設定Initialize a repo in a specific directory
例: git init my-project
git clone <url>セットアップ & 設定Clone a remote repository
例: git clone https://github.com/user/repo.git
git clone <url> <dir>セットアップ & 設定Clone into a specific folder
例: git clone https://github.com/user/repo.git my-dir
git statusステージング & 基本Show working tree status
git status -sステージング & 基本Short/compact status output
git add <file>ステージング & 基本Stage a specific file
例: git add README.md
git add .ステージング & 基本Stage all changes in current directory
git add -pステージング & 基本Interactively stage hunks of changes
git commit -m "msg"ステージング & 基本Commit staged changes with a message
例: git commit -m "feat: add login page"
git commit -am "msg"ステージング & 基本Stage tracked files and commit in one step
git commit --amend破壊的ステージング & 基本Modify the most recent commit (message or content)
git diffステージング & 基本Show unstaged changes
git diff --stagedステージング & 基本Show staged changes (vs last commit)
git diff <branch1>..<branch2>ステージング & 基本Compare two branches
例: git diff main..feature
git rm <file>ステージング & 基本Remove a file from working tree and index
例: git rm old-file.txt
git rm --cached <file>ステージング & 基本Untrack a file without deleting it
例: git rm --cached secret.txt
git mv <old> <new>ステージング & 基本Move or rename a tracked file
例: git mv old.js new.js
git branchブランチとマージList local branches
git branch -aブランチとマージList all branches (local + remote)
git branch <name>ブランチとマージCreate a new branch
例: git branch feature/auth
git checkout <branch>ブランチとマージSwitch to an existing branch
例: git checkout main
git checkout -b <branch>ブランチとマージCreate and switch to a new branch
例: git checkout -b feature/auth
git switch <branch>ブランチとマージSwitch branches (modern syntax)
例: git switch main
git switch -c <branch>ブランチとマージCreate and switch (modern syntax)
例: git switch -c feature/auth
git branch -d <branch>ブランチとマージDelete a merged branch
例: git branch -d feature/auth
git branch -D <branch>破壊的ブランチとマージForce delete a branch (unmerged too)
例: git branch -D feature/old
git branch -m <old> <new>ブランチとマージRename a branch
例: git branch -m old-name new-name
git merge <branch>ブランチとマージMerge a branch into current branch
例: git merge feature/auth
git merge --no-ff <branch>ブランチとマージMerge with a merge commit (no fast-forward)
git merge --squash <branch>ブランチとマージSquash all commits from branch into one staged change
git merge --abortブランチとマージAbort an in-progress merge
git remote -vリモート操作List remote connections with URLs
git remote add <name> <url>リモート操作Add a new remote
例: git remote add origin https://github.com/user/repo.git
git remote remove <name>リモート操作Remove a remote connection
例: git remote remove origin
git remote rename <old> <new>リモート操作Rename a remote
例: git remote rename origin upstream
git fetchリモート操作Download all changes from remote (no merge)
git fetch <remote>リモート操作Fetch from a specific remote
例: git fetch origin
git pullリモート操作Fetch and merge changes from remote
git pull --rebaseリモート操作Fetch and rebase onto remote branch
git push <remote> <branch>リモート操作Push branch to remote
例: git push origin main
git push -u origin <branch>リモート操作Push and set upstream tracking
例: git push -u origin feature/auth
git push --force-with-lease破壊的リモート操作Safe force push (fails if others pushed)
git push --tagsリモート操作Push all tags to remote
git push origin --delete <branch>破壊的リモート操作Delete a remote branch
例: git push origin --delete feature/old
git restore <file>破壊的変更を元に戻すDiscard changes in working directory
例: git restore index.html
git restore --staged <file>変更を元に戻すUnstage a file (keep changes)
例: git restore --staged index.html
git reset HEAD <file>変更を元に戻すUnstage a file (older syntax)
例: git reset HEAD index.html
git reset --soft HEAD~1変更を元に戻すUndo last commit, keep changes staged
git reset --mixed HEAD~1変更を元に戻すUndo last commit, unstage changes (default)
git reset --hard HEAD~1破壊的変更を元に戻すUndo last commit, discard all changes
git reset --hard <commit>破壊的変更を元に戻すReset to a specific commit, discard everything after
git revert <commit>変更を元に戻すCreate a new commit that undoes a specific commit
例: git revert a1b2c3d
git revert HEAD変更を元に戻すRevert the most recent commit
git clean -fd破壊的変更を元に戻すRemove untracked files and directories
git clean -n変更を元に戻すDry run: show what would be cleaned
git stashスタッシュStash current working directory changes
git stash push -m "msg"スタッシュStash with a descriptive message
例: git stash push -m "WIP: auth feature"
git stash listスタッシュList all stashes
git stash popスタッシュApply most recent stash and remove it
git stash apply stash@{n}スタッシュApply a specific stash without removing
例: git stash apply stash@{0}
git stash drop stash@{n}スタッシュDelete a specific stash
例: git stash drop stash@{0}
git stash clear破壊的スタッシュRemove all stashes
git stash branch <branch>スタッシュCreate a branch from a stash
例: git stash branch feature/from-stash
git rebase <branch>リベース & 高度な操作Rebase current branch onto another
例: git rebase main
git rebase -i HEAD~nリベース & 高度な操作Interactive rebase for last n commits
例: git rebase -i HEAD~3
git rebase --continueリベース & 高度な操作Continue rebase after resolving conflicts
git rebase --abortリベース & 高度な操作Abort an in-progress rebase
git cherry-pick <commit>リベース & 高度な操作Apply a specific commit to current branch
例: git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>リベース & 高度な操作Apply a range of commits
例: git cherry-pick a1b2c3d..f6e5d4c
git bisect startリベース & 高度な操作Start binary search for a bug
git bisect good <commit>リベース & 高度な操作Mark a commit as good in bisect
git bisect bad <commit>リベース & 高度な操作Mark a commit as bad in bisect
git bisect resetリベース & 高度な操作End bisect session
git worktree add <path> <branch>リベース & 高度な操作Check out a branch into a separate directory
git log調査 & ログShow commit history
git log --oneline調査 & ログCompact one-line log
git log --oneline --graph --all調査 & ログVisual branch graph in terminal
git log -n <count>調査 & ログShow last N commits
例: git log -n 5
git log --author="name"調査 & ログFilter commits by author
例: git log --author="Jane Doe"
git log --since="2 weeks ago"調査 & ログShow commits since a date
git log -p <file>調査 & ログShow changes to a specific file over time
例: git log -p README.md
git log --follow <file>調査 & ログFollow renames of a file in history
git show <commit>調査 & ログShow details of a specific commit
例: git show a1b2c3d
git blame <file>調査 & ログShow who last modified each line
例: git blame src/app.tsx
git shortlog -sn調査 & ログSummarize commits by author
git reflog調査 & ログShow history of HEAD movements (recovery tool)
git grep "pattern"調査 & ログSearch working directory for a pattern
例: git grep "TODO"
git tagタグList all tags
git tag <name>タグCreate a lightweight tag
例: git tag v1.0.0
git tag -a <name> -m "msg"タグCreate an annotated tag
例: git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>タグTag a past commit
例: git tag -a v0.9.0 a1b2c3d
git show <tag>タグShow tag metadata and commit
例: git show v1.0.0
git push origin <tag>タグPush a specific tag to remote
例: git push origin v1.0.0
git push origin --tagsタグPush all tags to remote
git tag -d <tag>タグDelete a local tag
例: git tag -d v1.0.0
git push origin --delete <tag>タグDelete a remote tag
例: git push origin --delete v1.0.0
git mergeはマージコミットを作成して2つのブランチを組み合わせ、両ブランチの完全な履歴を保持します。git rebaseは一方のブランチのコミットを別のブランチに移動し、より線形でクリーンなコミット履歴を作成するために履歴を書き直します。公開ブランチや共有作業にはmergeを、マージ前にローカルのフィーチャーブランチを整理するにはrebaseを使用します。
git reset --soft HEAD~1を使うと、変更をステージングした状態で最後のコミットを取り消せます。git reset HEAD~1(または--mixed)は変更のステージを解除しますが、作業ディレクトリには残します。git reset --hard HEAD~1はコミットとすべての変更を完全に破棄します(破壊的操作)。
git fetchはリモートから変更をダウンロードしますが、現在のブランチにはマージしません。リモートトラッキングブランチ(origin/main)を更新します。git pullはgit fetch + git mergeです(または--rebaseフラグを使ったgit rebase)。変更を統合する前に確認したい場合はfetchを使用します。
コンフリクトのあるマージ/リベース後、Gitはコンフリクトしたファイルを'<<<<<<', =======, >>>>>>マーカーでマークします。各コンフリクトファイルを開き、どの変更を残すかを手動で選択し、マーカーを削除してから、git add <ファイル>でファイルをステージし、git merge --continueまたはgit commitで完了させます。