Git Commands Reference

Quick reference for common Git commands. Init, clone, branch, commit, push, pull and more.

Back to all tools on ToolForge

More in Developer Tools

Initialize & Clone

CommandDescription
git initCreate 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 1Shallow clone—only fetch the latest commit (faster for large repos)
git clone <url> --branch [name]Clone a specific branch instead of default

Config

CommandDescription
git config --listShow 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 trueEnable colored output in terminal
git config --global alias.co checkoutCreate a git alias (git co = git checkout)

Branch Operations

CommandDescription
git branchList all local branches
git branch -rList all remote-tracking branches
git branch -aList 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 --abortAbort a conflicted merge in progress

Commit & Status

CommandDescription
git statusShow working tree status (staged, unstaged, untracked)
git status -sShow 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 -AStage all changes in the entire repository
git add -pInteractively 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 --amendModify the most recent commit (message or content)
git commit --amend --no-editAmend commit without changing the message
git logShow commit history (newest first)
git log --onelineShow compact one-line-per-commit log
git log --graph --oneline --allShow visual branch topology
git log -pShow 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 diffShow unstaged changes (working directory vs index)
git diff --stagedShow staged changes (index vs HEAD)
git diff HEADShow all changes (working directory vs HEAD)
git diff [branch1] [branch2]Show differences between two branches

Remote & Sync

CommandDescription
git remote -vList all configured remotes with URLs
git remote -v showShow 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 --allPush all local branches to origin
git push origin --tagsPush all tags to origin
git push -u origin [branch]Push and set upstream (track remote branch)
git push --forceForce push (overwrites remote—use with caution)
git push --force-with-leaseSafer force push (fails if remote changed)
git pull [remote] [branch]Fetch and merge remote changes into current branch
git pull --rebaseFetch and rebase instead of merge
git fetch [remote]Download objects and refs from remote
git fetch --allFetch from all remotes
git fetch --pruneFetch and remove stale remote-tracking branches

Stash & Restore

CommandDescription
git stashSave uncommitted changes to stash stack
git stash save "message"Stash with a descriptive message
git stash listList all stashed changes
git stash showShow changes in most recent stash
git stash show stash@{n}Show changes in a specific stash
git stash popApply most recent stash and remove it
git stash applyApply 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 clearDelete 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

CommandDescription
git rebase [branch]Rebase current branch onto another branch
git rebase -i HEAD~nInteractive rebase for last n commits
git rebase --continueContinue rebase after resolving conflicts
git rebase --abortAbort an in-progress rebase
git rebase --skipSkip 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 --continueContinue after resolving cherry-pick conflicts
git cherry-pick --abortAbort cherry-pick in progress

Tags

CommandDescription
git tagList all tags
git tag -a v1.0 -m "message"Create an annotated tag
git tag v1.0Create a lightweight tag
git tag -d v1.0Delete a local tag
git push origin v1.0Push a specific tag to remote
git push origin --tagsPush all tags to remote
git show v1.0Show 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

StateDescriptionCommand to Change
UntrackedNew file, not in Gitgit add
UnstagedModified, not stagedgit add
StagedReady to commitgit commit
CommittedSaved in .git directorygit push
PushedOn remote repositoryN/A

Common Git Workflows

WorkflowDescriptionBest For
Feature BranchCreate branch per feature, merge to mainMost teams, GitHub flow
Git FlowDevelop → Release → Main with hotfixesRelease-cycled projects
Trunk-BasedShort-lived branches, frequent merges to mainCI/CD, continuous deployment
ForkingFork repo, work in fork, PR to upstreamOpen 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.