Git
Git Commands Cheat Sheet
Practical Git commands for source control, branching, commits, collaboration, DevOps workflows and troubleshooting.
Showing 108 commands.
Git Setup and Configuration
git --versionDisplay the installed Git version.
git config --global user.name "Your Name"Set the global Git username used for commits.
git config --global user.email "you@example.com"Set the global email address used for commits.
git config --global --listDisplay global Git configuration settings.
git config --listDisplay Git configuration settings currently in effect.
git config --global init.defaultBranch mainSet main as the default branch name for newly initialized repositories.
Repository Initialization
git initInitialize a new Git repository in the current directory.
git statusShow the current repository state and changed files.
git status --shortDisplay a compact summary of repository changes.
git add .Stage all changes in the current repository.
git add <file>Stage a specific file.
git commit -m "message"Create a commit containing the staged changes.
Clone and Remote Repositories
git clone https://github.com/user/repo.gitClone a remote Git repository to the local machine.
git remote -vDisplay configured remote repository URLs.
git remote add origin <URL>Add a remote repository named origin.
git remote get-url originDisplay the URL configured for the origin remote.
git fetch originDownload objects and references from the origin without changing the current branch.
git fetch --allFetch updates from all configured remotes.
Status and Inspection
git statusShow staged, unstaged and untracked changes.
git status --shortShow a compact two-column status of changed files.
git diffShow unstaged changes in tracked files.
git diff --cachedShow changes currently staged for the next commit.
git showDisplay details about a commit.
git show <commit>Display a specific commit and its changes.
git branchList local branches.
git branch -aList local and remote-tracking branches.
Add and Commit
git add <file>Stage one specific file.
git add .Stage changes under the current directory.
git add -AStage all additions, modifications and deletions in the repository.
git commit -m "Add feature"Create a commit with a message.
git commit --amendModify the most recent commit.
git commit --amend --no-editUpdate the latest commit without changing its commit message.
Branches
git branchList local branches.
git branch feature/loginCreate a new branch without switching to it.
git switch feature/loginSwitch to an existing branch.
git switch -c feature/loginCreate and switch to a new branch.
git checkout feature/loginSwitch to an existing branch using the older checkout command.
git branch -d feature/loginDelete a local branch after it has been merged.
git branch -D feature/loginForce-delete a local branch. Use carefully.
git branch -m new-nameRename the current branch.
Merge and Rebase
git merge feature/loginMerge a branch into the current branch.
git merge --no-ff feature/loginCreate a merge commit even when a fast-forward merge is possible.
git rebase mainReplay the current branch commits on top of main.
git rebase -i HEAD~3Interactively edit, reorder, squash or drop recent commits.
git rebase --continueContinue a rebase after resolving conflicts.
git rebase --abortCancel an in-progress rebase and return to the previous state.
git merge --abortCancel an in-progress merge when supported by the current repository state.
Push and Pull
git push origin mainPush the local main branch to the origin remote.
git pushPush the current branch to its configured upstream branch.
git push -u origin feature/loginPush a new branch and configure its upstream remote branch.
git pullFetch and integrate changes from the configured upstream branch.
git pull --rebaseFetch and rebase local commits on top of the upstream branch.
git push origin --delete feature/loginDelete a branch from the remote repository.
Stash
git stashTemporarily save uncommitted changes and clean the working tree.
git stash push -m "work in progress"Create a named stash containing current changes.
git stash listList saved stashes.
git stash popApply the latest stash and remove it from the stash list.
git stash applyApply the latest stash without removing it.
git stash dropDelete a stash entry.
git stash clearDelete all stashes. Use carefully.
History and Logs
git logDisplay commit history.
git log --onelineDisplay a compact one-line commit history.
git log --oneline --decorate --graphDisplay a compact graphical branch and commit history.
git log --oneline -10Show the ten most recent commits in compact format.
git log --author="Name"Filter commit history by author.
git log --since="1 week ago"Show commits created within a specified time period.
git reflogShow updates to local branch references, useful for recovering previous states.
Undo Changes
git restore <file>Discard unstaged changes in a tracked file.
git restore --staged <file>Remove a file from the staging area without deleting its working-tree changes.
git reset HEAD <file>Unstage a file using the reset command.
git revert <commit>Create a new commit that reverses the changes introduced by another commit.
git reset --soft HEAD~1Move HEAD back one commit while keeping changes staged.
git reset --mixed HEAD~1Move HEAD back one commit while keeping changes in the working tree but unstaged.
git reset --hard HEAD~1Move HEAD back one commit and discard working-tree changes. Use with extreme care.
Tags and Releases
git tagList local Git tags.
git tag v1.0.0Create a lightweight tag for the current commit.
git tag -a v1.0.0 -m "Release 1.0.0"Create an annotated release tag.
git show v1.0.0Display information about a tag.
git push origin v1.0.0Push a specific tag to the remote repository.
git push origin --tagsPush all local tags to the remote repository.
Diff and Comparison
git diffShow unstaged changes.
git diff --cachedShow staged changes.
git diff main..featureCompare changes between two branches.
git diff HEAD~1 HEADCompare the latest commit with its parent.
git diff --statShow a summary of changed files and line counts.
git diff --name-onlyList files changed between revisions.
Cherry-pick
git cherry-pick <commit>Apply the changes introduced by a specific commit to the current branch.
git cherry-pick --no-commit <commit>Apply a commit's changes without automatically creating a commit.
git cherry-pick --continueContinue a cherry-pick after resolving conflicts.
git cherry-pick --abortCancel an in-progress cherry-pick operation.
Remote Tracking and Branch Management
git branch -rList remote-tracking branches.
git branch -vvShow local branches together with upstream tracking information.
git fetch originDownload the latest remote references without modifying the working branch.
git remote show originDisplay detailed information about a remote repository.
git push -u origin mainPush main and establish its upstream branch.
Git Troubleshooting
git statusStart troubleshooting by checking the repository state.
git log --oneline --decorate --graph -10Inspect recent commits and branch relationships.
git reflogFind previous HEAD and branch states when commits appear to be missing.
git remote -vVerify the configured remote URLs.
git branch -vvCheck branch tracking and upstream configuration.
git fetch --all --pruneFetch remote updates and remove stale remote-tracking references.
git fsck --lost-foundInspect unreachable Git objects when investigating repository recovery.
DevOps Git Workflow
git status --shortCheck exactly what changed before staging files.
git add <file>Stage only the intended file or files.
git commit -m "Describe change"Create a focused commit describing the change.
git pull --rebase origin mainUpdate the local branch while keeping local commits on top of the remote branch.
git push origin mainPush the tested local changes to the main branch.
git log --oneline --decorate --graph -10Verify recent repository history after pushing changes.
Recommended DevOps Git Workflow
# Check repository state git status --short # Review changes git diff # Stage the intended files git add <file> # Review staged changes git diff --cached # Commit git commit -m "Describe the change" # Update from remote git pull --rebase origin main # Push git push origin main # Verify history git log --oneline --decorate --graph -10
DevOpsCommands Tip
Prefer small, focused commits that describe one logical change. Before pushing, review the status and staged diff so unrelated files are not accidentally included.