Searchable reference of Git commands with examples
Showing 71 of 71 commands.
git config --global user.name "<name>"
Set the name attached to your commits.
git config --global user.name "Jane Doe"
git config --global user.email "<email>"
Set the email attached to your commits.
git config --global user.email "jane@example.com"
git config --global init.defaultBranch <name>
Choose the default branch name for new repos.
git config --global init.defaultBranch main
git config --global core.editor <editor>
Set the editor Git uses for commit messages, rebase, etc.
git config --global core.editor "code --wait"
git init
Initialize a new repository in the current directory.
git clone <url>
Clone a remote repository to a local directory.
git clone git@github.com:org/repo.git
git branch
List local branches; the current one is starred.
git branch -a
List local and remote-tracking branches.
git branch <name>
Create a new branch from the current commit.
git branch feature/login
git switch <branch>
Switch to an existing branch (modern alternative to checkout).
git switch develop
git switch -c <name>
Create a new branch and switch to it.
git switch -c feature/login
git checkout <branch>
Classic branch switch (still works everywhere).
git checkout main
git branch -d <name>
Delete a branch that has been merged.
git branch -d feature/login
git branch -D <name>
Force-delete a branch even if unmerged.
git branch -D experiment
git branch -m <new>
Rename the current branch.
git branch -m main
git merge <branch>
Merge another branch into the current one.
git merge feature/login
git rebase <branch>
Replay current branch's commits on top of another branch.
git rebase main
git status
Show working tree status (staged, unstaged, untracked).
git add <path>
Stage a file or directory for the next commit.
git add src/app.ts
git add -p
Interactively stage hunks of changes.
git add .
Stage every change in the current directory.
git restore --staged <path>
Unstage a file while keeping the working-tree changes.
git restore --staged src/app.ts
git commit -m "<message>"
Create a commit with the given message.
git commit -m "fix: handle null user"
git commit -am "<message>"
Stage all tracked, modified files and commit.
git commit -am "chore: bump deps"
git commit --amend
Replace the last commit (message and/or contents).
git commit --amend --no-edit
git remote -v
Show configured remotes and their URLs.
git remote add <name> <url>
Add a new remote repository.
git remote add origin git@github.com:org/repo.git
git remote set-url <name> <url>
Change the URL of an existing remote.
git remote set-url origin git@github.com:org/repo.git
git fetch <remote>
Download new commits from a remote without merging.
git fetch origin
git pull
Fetch and integrate changes from the upstream branch.
git pull --rebase
Fetch then replay local commits on top of upstream.
git pull --rebase origin main
git push
Send local commits to the upstream branch.
git push -u <remote> <branch>
Push and set the upstream tracking reference.
git push -u origin feature/login
git push --force-with-lease
Force push, but only if the remote hasn't moved.
git stash
Save uncommitted changes and revert to clean working tree.
git stash -u
Stash including untracked files.
git stash push -m "<message>"
Stash with a descriptive name.
git stash push -m "wip: login form"
git stash list
List all stashes.
git stash pop
Apply the latest stash and remove it from the stash stack.
git stash apply <ref>
Apply a specific stash without removing it.
git stash apply stash@{1}
git stash drop <ref>
Discard a specific stash entry.
git stash drop stash@{0}
git log
Show commit history of the current branch.
git log --oneline --graph --decorate --all
Compact graph view of the entire history.
git log -p <path>
Show commit history with diffs for a file.
git log -p src/auth.ts
git log --author=<name>
Filter history by author.
git log --author=jane
git log --since=<date>
Show commits newer than a given date.
git log --since="2 weeks ago"
git shortlog -sn
Summarize commits per author (sorted, numbered).
git blame <file>
Show who last modified each line of a file.
git blame src/app.ts
git reflog
Show movements of HEAD — invaluable for recovering lost commits.
git restore <file>
Discard working-tree changes for a file.
git restore src/app.ts
git restore --source=<commit> <file>
Restore a file from a specific commit.
git restore --source=HEAD~1 src/app.ts
git reset HEAD <file>
Unstage a file (legacy form of restore --staged).
git reset HEAD src/app.ts
git reset --soft HEAD~1
Undo the last commit but keep changes staged.
git reset --mixed HEAD~1
Undo the last commit and unstage the changes (default).
git reset --hard HEAD~1
Discard the last commit and all its changes. Dangerous.
git revert <commit>
Create a new commit that undoes a previous one (safe for shared history).
git revert a1b2c3d
git clean -fd
Delete untracked files and directories. Use --dry-run first.
git tag
List existing tags.
git tag <name>
Create a lightweight tag at HEAD.
git tag v1.0.0
git tag -a <name> -m "<message>"
Create an annotated tag (preferred for releases).
git tag -a v1.0.0 -m "Release 1.0.0"
git push <remote> <tag>
Push a single tag to a remote.
git push origin v1.0.0
git push --tags
Push all local tags to the remote.
git tag -d <name>
Delete a local tag.
git tag -d v1.0.0
git push <remote> --delete <tag>
Delete a tag on the remote.
git push origin --delete v1.0.0
git diff
Show unstaged changes vs the index.
git diff --staged
Show what would be committed.
git diff <a>..<b>
Show diff between two refs.
git diff main..feature/login
git diff --stat
Summarize changes per file (lines +/-).
git show <commit>
Show the content of a commit (message + diff).
git show a1b2c3d
git cherry-pick <commit>
Apply the changes from a single commit onto the current branch.
git cherry-pick a1b2c3d
git bisect start / good / bad
Binary-search history to find the commit that introduced a bug.
git bisect start && git bisect bad && git bisect good v1.0