Git Commands Reference
Quick reference for common Git commands. Init, clone, branch, commit, push, pull and more.
Back to all tools on ToolForge
Initialize & Clone
| Command | Description |
|---|---|
git init | Create a new Git repository in the current directory |
git init [project-name] | Create a new directory and initialize it as a Git repository |
git clone <url> | Clone a remote repository to your local machine |
git clone <url> [directory-name] | Clone into a specifically named directory |
git clone <url> --depth 1 | Shallow clone—only fetch the latest commit (faster for large repos) |
git clone <url> --branch [name] | Clone a specific branch instead of default |
Config
| Command | Description |
|---|---|
git config --list | Show all current Git configuration settings |
git config --global user.name "name" | Set global author name for all repositories |
git config --global user.email "email" | Set global author email for all repositories |
git config user.name "name" | Set repository-specific author name (omit --global) |
git config user.email "email" | Set repository-specific author email |
git config --global core.editor "code --wait" | Set default editor (example: VS Code) |
git config --global color.ui true | Enable colored output in terminal |
git config --global alias.co checkout | Create a git alias (git co = git checkout) |
Branch Operations
| Command | Description |
|---|---|
git branch | List all local branches |
git branch -r | List all remote-tracking branches |
git branch -a | List all branches (local and remote) |
git branch [name] | Create a new branch from current commit |
git checkout -b [branch] | Create and switch to a new branch |
git checkout [branch] | Switch to an existing branch |
git switch [branch] | Switch to branch (Git 2.23+, clearer intent) |
git switch -c [branch] | Create and switch to branch (Git 2.23+) |
git checkout -b [branch] [start-point] | Create branch from a specific commit/branch |
git branch -d [branch] | Delete a merged branch (safe delete) |
git branch -D [branch] | Force delete unmerged branch (loses changes) |
git branch -m [old] [new] | Rename a branch |
git merge [branch] | Merge specified branch into current branch |
git merge --no-ff [branch] | Merge and always create a merge commit |
git merge --abort | Abort a conflicted merge in progress |
Commit & Status
| Command | Description |
|---|---|
git status | Show working tree status (staged, unstaged, untracked) |
git status -s | Show status in short, compact format |
git add [file] | Stage a specific file for commit |
git add . | Stage all changes in current directory and subdirectories |
git add -A | Stage all changes in the entire repository |
git add -p | Interactively stage changes patch by patch |
git commit -m "message" | Commit staged changes with a message |
git commit -am "message" | Add all tracked files and commit (skips new files) |
git commit --amend | Modify the most recent commit (message or content) |
git commit --amend --no-edit | Amend commit without changing the message |
git log | Show commit history (newest first) |
git log --oneline | Show compact one-line-per-commit log |
git log --graph --oneline --all | Show visual branch topology |
git log -p | Show log with diffs for each commit |
git log --author="name" | Filter commits by author |
git log --since="2 weeks ago" | Filter commits by date range |
git diff | Show unstaged changes (working directory vs index) |
git diff --staged | Show staged changes (index vs HEAD) |
git diff HEAD | Show all changes (working directory vs HEAD) |
git diff [branch1] [branch2] | Show differences between two branches |
Remote & Sync
| Command | Description |
|---|---|
git remote -v | List all configured remotes with URLs |
git remote -v show | Show detailed remote information |
git remote add origin <url> | Add a remote named "origin" |
git remote add [name] <url> | Add a remote with a custom name |
git remote remove [name] | Remove a remote |
git remote rename [old] [new] | Rename a remote |
git push [remote] [branch] | Push local branch to remote |
git push origin --all | Push all local branches to origin |
git push origin --tags | Push all tags to origin |
git push -u origin [branch] | Push and set upstream (track remote branch) |
git push --force | Force push (overwrites remote—use with caution) |
git push --force-with-lease | Safer force push (fails if remote changed) |
git pull [remote] [branch] | Fetch and merge remote changes into current branch |
git pull --rebase | Fetch and rebase instead of merge |
git fetch [remote] | Download objects and refs from remote |
git fetch --all | Fetch from all remotes |
git fetch --prune | Fetch and remove stale remote-tracking branches |
Stash & Restore
| Command | Description |
|---|---|
git stash | Save uncommitted changes to stash stack |
git stash save "message" | Stash with a descriptive message |
git stash list | List all stashed changes |
git stash show | Show changes in most recent stash |
git stash show stash@{n} | Show changes in a specific stash |
git stash pop | Apply most recent stash and remove it |
git stash apply | Apply most recent stash (keep in stack) |
git stash apply stash@{n} | Apply a specific stash |
git stash drop stash@{n} | Delete a specific stash |
git stash clear | Delete all stashes |
git stash branch [name] | Create branch from stash and apply it |
git restore [file] | Restore file in working directory (Git 2.23+) |
git restore --staged [file] | Unstage file (restore index only) |
git restore --source=[commit] [file] | Restore file from a specific commit |
Rebase & Cherry-Pick
| Command | Description |
|---|---|
git rebase [branch] | Rebase current branch onto another branch |
git rebase -i HEAD~n | Interactive rebase for last n commits |
git rebase --continue | Continue rebase after resolving conflicts |
git rebase --abort | Abort an in-progress rebase |
git rebase --skip | Skip current commit during rebase |
git rebase --onto [newbase] [upstream] | Rebase branch onto new base, excluding upstream |
git cherry-pick <commit> | Apply a specific commit to current branch |
git cherry-pick [commit1] [commit2] | Cherry-pick multiple commits |
git cherry-pick --continue | Continue after resolving cherry-pick conflicts |
git cherry-pick --abort | Abort cherry-pick in progress |
Tags
| Command | Description |
|---|---|
git tag | List all tags |
git tag -a v1.0 -m "message" | Create an annotated tag |
git tag v1.0 | Create a lightweight tag |
git tag -d v1.0 | Delete a local tag |
git push origin v1.0 | Push a specific tag to remote |
git push origin --tags | Push all tags to remote |
git show v1.0 | Show tag information and commit |
About Git Commands Reference
This Git commands reference provides comprehensive coverage of version control operations from basic initialization to advanced rebasing and cherry-picking. Git is the industry-standard distributed version control system, essential for collaborative software development.
The commands are organized by functional category, making it easy to find the right command whether you're setting up a repository, managing branches, syncing with remotes, or recovering from mistakes.
Git Workflow Basics
Typical development workflow: 1. Start work: git checkout main git pull origin main git checkout -b feature/new-feature 2. Make changes: git add . git commit -m "Implement feature" 3. Sync with remote: git push -u origin feature/new-feature 4. Later, update branch: git fetch origin git rebase origin/main 5. When done: git push # Create pull request on GitHub/GitLab
Understanding Git File States
| State | Description | Command to Change |
|---|---|---|
| Untracked | New file, not in Git | git add |
| Unstaged | Modified, not staged | git add |
| Staged | Ready to commit | git commit |
| Committed | Saved in .git directory | git push |
| Pushed | On remote repository | N/A |
Common Git Workflows
| Workflow | Description | Best For |
|---|---|---|
| Feature Branch | Create branch per feature, merge to main | Most teams, GitHub flow |
| Git Flow | Develop → Release → Main with hotfixes | Release-cycled projects |
| Trunk-Based | Short-lived branches, frequent merges to main | CI/CD, continuous deployment |
| Forking | Fork repo, work in fork, PR to upstream | Open source contributions |
Resolving Conflicts
When merge conflicts occur, Git marks files like this: <<<<<<< HEAD Your changes here ======= Incoming changes from merge/rebase >>>>>>> branch-name To resolve: 1. Edit file to keep desired changes (or combine both) 2. Remove the <<<<<<<, =======, and >>>>>>> markers 3. git add <file> to mark as resolved 4. git commit (or git merge --continue / git rebase --continue) Use git status to see which files still have conflicts. Use git mergetool for visual conflict resolution.
Frequently Asked Questions
- What is the difference between git pull and git fetch?
- git fetch downloads commits, files, and refs from a remote repository but doesn't integrate them into your working files—it only updates remote-tracking branches. git pull performs git fetch followed by git merge, automatically integrating changes into your current branch. Use fetch when you want to review changes before merging; use pull for quick updates.
- What is the difference between git merge and git rebase?
- git merge combines branches by creating a new merge commit, preserving complete history including when branches diverged. git rebase moves or replays commits from one branch onto another, creating a linear history by rewriting commits. Merge is safer for shared branches; rebase creates cleaner history but should never be used on public/shared branches.
- How do I undo a git commit?
- To undo the last commit but keep changes: git reset --soft HEAD~1. To discard changes entirely: git reset --hard HEAD~1. For already-pushed commits, use git revert <commit> which creates a new commit that undoes the specified commit—this is safe for shared branches. Never use reset --hard on pushed commits.
- What is git stash and when should I use it?
- git stash temporarily saves uncommitted changes (working directory and index) without committing, restoring the working directory to match HEAD. Use it when you need to switch branches but aren't ready to commit. Common commands: git stash save 'message', git stash list, git stash pop (apply and remove), git stash apply (keep in stack).
- How do I resolve merge conflicts in git?
- When git can't auto-merge, it marks conflicts in files with <<<<<<<, =======, and >>>>>>> markers. Edit files to choose desired changes (or combine both), remove markers, then git add <file> to mark resolved. After resolving all conflicts, complete with git commit (or git merge --continue). Use git mergetool for visual conflict resolution.
- What is git cherry-pick and when should I use it?
- git cherry-pick <commit> applies the changes from a specific commit onto your current branch, creating a new commit with the same changes but different hash. Use it to selectively apply commits without merging entire branches—common for hotfixes, backporting to release branches, or recovering commits from abandoned branches.