ESC
Git コマンド

Setup & Config

Command Description Example
git config --global user.name "Name" Set the name attached to all commits globally git config --global user.name "John Doe"
git config --global user.email "email" Set the email attached to all commits globally git config --global user.email "[email protected]"
git config --global core.editor "editor" Set the default text editor for commit messages git config --global core.editor "code --wait"
git config --global color.ui auto Enable helpful colorization of command line output git config --global color.ui auto
git config --list List all configuration settings and their values git config --list --show-origin
git config --global alias.<alias> <command> Create a shorthand alias for a git command git config --global alias.st status
git config --global --unset <key> Remove a specific global config setting git config --global --unset core.editor
git config --global init.defaultBranch main Set the default branch name for new repositories git config --global init.defaultBranch main

Basic Workflow

Command Description Example
git init Initialize a new empty git repository in current directory git init my-project
git clone <url> Clone a remote repository into a new directory git clone https://github.com/user/repo.git
git clone <url> <dir> Clone a repository into a specific directory name git clone https://github.com/user/repo.git my-dir
git status Show the state of the working directory and staging area git status -s (short format)
git add <file> Stage a specific file for the next commit git add index.html
git add . Stage all changes in the current directory git add .
git add -p Interactively stage chunks/hunks of changes git add -p (choose y/n for each hunk)
git commit -m "message" Commit staged changes with an inline commit message git commit -m "feat: add login page"
git commit -am "message" Stage all tracked changes and commit in one step git commit -am "fix: correct typo in header"
git commit --amend Modify the most recent commit (message or content) git commit --amend -m "Updated message"
git diff Show unstaged changes between working tree and index git diff src/app.js
git diff --staged Show staged changes that will go into the next commit git diff --staged
git rm <file> Remove a file from the working tree and staging area git rm obsolete.txt
git mv <old> <new> Move or rename a file, staging the change automatically git mv old-name.txt new-name.txt

Branching

Command Description Example
git branch List all local branches; current branch is marked with * git branch -a (list local and remote branches)
git branch <name> Create a new branch at the current commit git branch feature/login
git checkout <branch> Switch to an existing branch git checkout main
git checkout -b <branch> Create and switch to a new branch in one command git checkout -b feature/signup
git switch <branch> Switch to an existing branch (modern syntax) git switch develop
git switch -c <branch> Create and switch to a new branch (modern syntax) git switch -c feature/dashboard
git branch -d <branch> Delete a merged branch (safe delete) git branch -d feature/login
git branch -D <branch> Force delete a branch regardless of merge status git branch -D old-experiment
git branch -m <old> <new> Rename a branch git branch -m old-name new-name
git merge <branch> Merge a branch into the current branch git merge feature/login
git merge --no-ff <branch> Merge with a merge commit, preserving branch history git merge --no-ff feature/login
git merge --squash <branch> Squash all branch commits into one staged change git merge --squash feature/login
git rebase <branch> Reapply commits on top of another branch git rebase main
git rebase -i HEAD~n Interactively rebase last n commits (squash, reorder, edit) git rebase -i HEAD~3
git cherry-pick <commit> Apply changes from a specific commit onto current branch git cherry-pick a1b2c3d

Remote

Command Description Example
git remote -v List all remote connections with their URLs git remote -v
git remote add <name> <url> Add a new remote connection 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 connection git remote rename origin upstream
git fetch <remote> Download changes from remote without merging git fetch origin
git fetch --all Fetch from all remotes git fetch --all --prune
git pull Fetch and merge changes from the tracking remote branch git pull origin main
git pull --rebase Fetch and rebase instead of merge git pull --rebase origin main
git push <remote> <branch> Push a branch to a remote repository git push origin main
git push -u <remote> <branch> Push and set upstream tracking for the branch git push -u origin feature/login
git push --force-with-lease Force push but fail if remote has unexpected changes (safer than --force) git push --force-with-lease origin feature/login
git push <remote> --delete <branch> Delete a remote branch git push origin --delete old-branch
git push --tags Push all local tags to the remote git push origin --tags

History & Log

Command Description Example
git log Show the commit history for the current branch git log --oneline --graph --decorate
git log --oneline Show compact one-line commit history git log --oneline -10 (last 10 commits)
git log --author="Name" Filter commit history by author name git log --author="John" --oneline
git log --since=<date> Show commits after a specified date git log --since="2024-01-01" --until="2024-12-31"
git log --grep="pattern" Search commit messages matching a pattern git log --grep="fix" --oneline
git log -p <file> Show changes introduced by each commit for a file git log -p src/app.js
git log --follow <file> Follow a file's history across renames git log --follow -p old-name.txt
git show <commit> Show details and diff of a specific commit git show a1b2c3d
git blame <file> Show who last modified each line of a file git blame -L 10,20 src/app.js
git shortlog -sn Show commit count per author, sorted by number git shortlog -sn --all
git diff <branch1>..<branch2> Show changes between two branches git diff main..feature/login
git bisect start Start a binary search for the commit that introduced a bug git bisect start; git bisect bad; git bisect good v1.0

Undo Changes

Command Description Example
git restore <file> Discard changes in working directory (unstaged) git restore index.html
git restore --staged <file> Unstage a file, keeping changes in working directory git restore --staged index.html
git reset HEAD~1 Undo last commit, keeping changes staged git reset HEAD~1 (soft reset by default)
git reset --soft HEAD~n Undo last n commits, keeping all changes staged git reset --soft HEAD~3
git reset --mixed HEAD~n Undo last n commits, unstaging changes (default) git reset HEAD~2
git reset --hard HEAD~n Undo last n commits and discard all changes permanently git reset --hard HEAD~1
git revert <commit> Create a new commit that undoes changes from a commit (safe for shared branches) git revert a1b2c3d
git revert HEAD Create a new commit that reverses the last commit git revert HEAD --no-edit
git stash Save uncommitted changes to a temporary stash git stash push -m "WIP: login feature"
git stash pop Apply the most recent stash and remove it from stash list git stash pop
git stash list List all stashed changesets git stash list
git stash apply stash@{n} Apply a specific stash without removing it git stash apply stash@{2}
git stash drop stash@{n} Remove a specific stash entry git stash drop stash@{0}
git clean -fd Remove untracked files and directories from working tree git clean -fd (use -n first to preview)

Advanced

Command Description Example
git tag <name> Create a lightweight tag at the current commit git tag v1.0.0
git tag -a <name> -m "msg" Create an annotated tag with a message git tag -a v1.0.0 -m "Release version 1.0.0"
git tag -d <name> Delete a local tag git tag -d v1.0.0
git submodule add <url> Add a git repository as a submodule git submodule add https://github.com/user/lib.git lib
git submodule update --init --recursive Initialize and update all submodules git submodule update --init --recursive
git reflog Show a log of all reference changes, useful for recovering lost commits git reflog show HEAD
git worktree add <path> <branch> Check out a branch into a new linked working tree git worktree add ../hotfix-tree hotfix/urgent
git archive --format=zip HEAD > out.zip Create a zip archive of the repository at HEAD git archive --format=tar HEAD | gzip > repo.tar.gz
git gc Run garbage collection to optimize repository performance git gc --aggressive --prune=now
git rev-parse HEAD Get the full SHA of the current HEAD commit git rev-parse --short HEAD (short SHA)

よくある質問

Git はファイルの変更を時系列で追跡する分散型バージョン管理システムです。複数の開発者が互いの作業を上書きすることなく同じプロジェクトで共同作業でき、すべての変更の完全な履歴を保持し、メインコードに影響を与えずにブランチで実験でき、以前の任意の状態に簡単に戻せます。ソースコード管理の業界標準です。

目的に応じていくつかの方法があります。「git revert HEAD」は最後のコミットを取り消す新しいコミットを作成します。履歴を保持するため共有ブランチでも安全です。「git reset --soft HEAD~1」は最後のコミットを取り消しますが、変更はステージングエリアに残ります。「git reset --mixed HEAD~1」(デフォルト)はコミットを取り消してステージングも解除しますが、変更は作業ディレクトリに残ります。「git reset --hard HEAD~1」はコミットを取り消してすべての変更を永久に破棄します。慎重に使用してください。共有リポジトリにすでにプッシュしたコミットへの --hard リセットは避けてください。

「git merge」は2つの親コミットを持つ新しいマージコミットを作成して2つのブランチを統合します。両方のブランチの完全な履歴が保持され非破壊的ですが、ログがマージコミットで煩雑になる場合があります。「git rebase」はコミットを別のブランチの上に移動または再現し、直線的でクリーンな履歴を生成します。ただし rebase はコミット履歴を書き換えるため、すでに共有リモートブランチにプッシュしたコミットを絶対に rebase しないでください。他のコントリビューターに問題が生じます。

HEAD は現在チェックアウトされているコミットを指す特別なポインターで、リポジトリ履歴における「今いる場所」を Git に伝えます。通常、HEAD は現在のブランチの先端を指しています。「git checkout」でブランチを切り替えると HEAD は新しいブランチに移動します。HEAD~1 は HEAD の1つ前のコミット、HEAD~2 は2つ前を指します。「切り離された HEAD」状態は HEAD がブランチ名ではなく直接コミットハッシュを指しているときに発生します。

マージ競合が発生すると、Git はマージを一時停止して競合ファイルをマークします。各競合ファイルを開くと、Git が競合マーカーを挿入しています。「<<<<<<< HEAD」(あなたの変更)、「=======」(区切り線)、「>>>>>>> ブランチ名」(取り込まれる変更)の形式です。正しいコードが残るようにファイルを編集し、競合マーカーをすべて削除します。次に「git add <ファイル>」で解決済みファイルをステージングし、「git commit」でマージを完了します。VS Code、IntelliJ、または「git mergetool」などのツールは競合解決を視覚的に行えるインターフェースを提供します。