Git Commands Cheatsheet
gitEssential Git commands for daily development workflow.
Setup
git config --global user.name "Your Name"Configure the author name to be used with your commits globally
git config --global user.name "John Doe"git config --global user.email "email@example.com"Configure the email address to be used with your commits globally
git config --global user.email "john@example.com"git config --listDisplay all Git configuration settings for the current repository
git config --list --show-origingit init [directory]Create a new Git repository in the current or specified directory
git init my-projectgit init --bare [directory]Create a bare repository without a working directory for server use
git init --bare project.gitgit clone <url> [directory]Download a repository and its entire version history
git clone https://github.com/user/repo.gitgit clone --depth <depth> <url>Clone only the specified number of commits for faster downloads
git clone --depth 1 https://github.com/user/repo.gitgit clone -b <branch> <url>Clone a repository and checkout a specific branch
git clone -b develop https://github.com/user/repo.gitStaging
git statusShow the working tree status including staged and unstaged changes
git status -sgit add <file>Add a specific file to the staging area
git add index.jsgit add .Add all new and modified files in current directory to staging
git add .git add <pattern>Add files matching a pattern to the staging area
git add *.jsgit add -p [file]Interactively choose hunks of patch to add to staging
git add -p src/app.jsgit reset HEAD <file>Remove a file from the staging area but keep changes in working directory
git reset HEAD index.jsgit reset HEADUnstage all files while preserving changes in working directory
git reset HEADgit checkout -- <file>Discard changes in working directory for a specific file
git checkout -- index.jsgit diffShow changes between working directory and staging area
git diff src/app.jsgit diff --stagedShow changes between staging area and last commit
git diff --stagedgit diff <commit1> <commit2>Show changes between two specific commits
git diff HEAD~2 HEADCommitting
git commit -m "message"Record staged changes to the repository with a message
git commit -m "Add user authentication"git commit -am "message"Stage all tracked modified files and commit in one step
git commit -am "Fix login bug"git commitOpen the default editor to write a detailed commit message
git commitgit commit --amendModify the most recent commit message or add staged changes to it
git commit --amend -m "Updated message"git commit --amend --no-editAdd staged changes to last commit without changing the message
git commit --amend --no-editgit commit --allow-empty -m "message"Create a commit with no changes useful for triggering CI builds
git commit --allow-empty -m "Trigger build"git revert <commit>Create a new commit that undoes changes from a specified commit
git revert abc1234git revert -n <commit>Revert changes but do not automatically create a commit
git revert -n HEAD~3Branching
git branchList all local branches in the repository
git branch -agit branch <branch-name>Create a new branch at the current commit
git branch feature/logingit branch -d <branch-name>Delete a branch that has been fully merged
git branch -d feature/logingit branch -D <branch-name>Force delete a branch even if not fully merged
git branch -D experimentalgit branch -m <old> <new>Rename a branch from old name to new name
git branch -m old-name new-namegit checkout <branch-name>Switch to an existing branch
git checkout developgit checkout -b <branch-name>Create a new branch and switch to it immediately
git checkout -b feature/authgit switch <branch-name>Switch to an existing branch using modern syntax
git switch maingit switch -c <branch-name>Create a new branch and switch to it using modern syntax
git switch -c feature/apigit merge <branch-name>Merge specified branch into the current branch
git merge feature/logingit merge --no-ff <branch>Create a merge commit even if fast-forward is possible
git merge --no-ff feature/logingit merge --abortAbort the current merge operation and restore pre-merge state
git merge --abortgit rebase <branch>Reapply commits on top of another base branch
git rebase maingit rebase -i <commit>Interactively edit reword squash or reorder commits
git rebase -i HEAD~3git cherry-pick <commit>Apply changes from a specific commit to current branch
git cherry-pick abc1234Remote
git remote -vShow all configured remote repositories with URLs
git remote -vgit remote add <name> <url>Add a new remote repository with a shortname
git remote add origin https://github.com/user/repo.gitgit remote remove <name>Remove a remote repository reference
git remote remove origingit remote rename <old> <new>Rename a remote repository reference
git remote rename origin upstreamgit remote show <name>Display detailed information about a remote repository
git remote show origingit fetch <remote>Download objects and refs from remote without merging
git fetch origingit fetch --allFetch from all configured remote repositories
git fetch --all --prunegit pull <remote> <branch>Fetch from remote and merge into current branch
git pull origin maingit pull --rebase <remote> <branch>Fetch and rebase current branch on top of remote branch
git pull --rebase origin maingit push <remote> <branch>Upload local branch commits to remote repository
git push origin maingit push -u <remote> <branch>Push and set remote branch as upstream for current branch
git push -u origin feature/logingit push --forceForce push overwriting remote history use with caution
git push --force-with-lease origin featuregit push <remote> --delete <branch>Delete a branch from the remote repository
git push origin --delete feature/oldHistory
git logShow commit history for the current branch
git log --oneline -10git log --onelineShow condensed commit history with one line per commit
git log --oneline --graphgit log --graph --allShow commit history as ASCII graph for all branches
git log --graph --all --decorategit log -pShow commit history with full diff for each commit
git log -p -2git log --author="name"Filter commit history by author name or email
git log --author="John"git log --since="date" --until="date"Filter commits within a date range
git log --since="2024-01-01" --until="2024-12-31"git log -- <file>Show commit history for a specific file
git log --follow -- src/app.jsgit show <commit>Display detailed information and diff for a specific commit
git show abc1234git blame <file>Show who last modified each line of a file with commit info
git blame -L 10,20 src/app.jsgit bisect startStart binary search to find the commit that introduced a bug
git bisect startgit bisect bad [commit]Mark a commit as bad containing the bug
git bisect bad HEADgit bisect good [commit]Mark a commit as good not containing the bug
git bisect good abc1234git bisect resetEnd bisect session and return to original branch
git bisect resetStashing
git stashSave uncommitted changes and revert working directory to clean state
git stashgit stash push -m "message"Stash changes with a descriptive message for identification
git stash push -m "WIP: login feature"git stash -uStash changes including untracked files
git stash -ugit stash listShow all stashed changesets
git stash listgit stash apply [stash]Apply stashed changes without removing from stash list
git stash apply stash@{0}git stash pop [stash]Apply stashed changes and remove from stash list
git stash popgit stash show [stash]Show summary of changes in a stash
git stash show -p stash@{0}git stash drop [stash]Remove a specific stash from the list
git stash drop stash@{1}git stash clearRemove all stashed entries
git stash clear