Quick reference guide for common Git commands with examples
git config --global user.name "[name]"Set your name for commit transactions
Example:
git config --global user.name "John Doe"git config --global user.email "[email]"Set your email for commit transactions
Example:
git config --global user.email "john@example.com"git config --listList all configuration settings
git config --global color.ui autoEnable colorization of command line output
git initInitialize a new Git repository
git clone [url]Clone a repository from a remote URL
Example:
git clone https://github.com/user/repo.gitgit statusShow the working tree status
git add [file]Add file contents to the staging area
Example:
git add index.htmlgit add .Add all changes to the staging area
git add -AAdd all changes (new, modified, deleted) to the staging area
git commit -m "[message]"Commit staged changes with a message
Example:
git commit -m "Add new feature"git commit -am "[message]"Add and commit all tracked files with a message
Example:
git commit -am "Update readme"git commit --amendModify the last commit
git reset [file]Unstage a file while retaining changes
Example:
git reset index.htmlgit reset --hardReset the working directory and staging area to match the most recent commit
git rm [file]Remove a file from the working directory and staging area
Example:
git rm old-file.txtgit mv [file-original] [file-renamed]Move or rename a file
Example:
git mv old.txt new.txtgit branchList all local branches
git branch [branch-name]Create a new branch
Example:
git branch feature/new-featuregit branch -d [branch-name]Delete a branch
Example:
git branch -d feature/old-featuregit branch -D [branch-name]Force delete a branch
Example:
git branch -D feature/abandonedgit checkout [branch-name]Switch to a branch
Example:
git checkout developgit checkout -b [branch-name]Create and switch to a new branch
Example:
git checkout -b feature/new-featuregit switch [branch-name]Switch to a branch (newer alternative to checkout)
Example:
git switch maingit switch -c [branch-name]Create and switch to a new branch
Example:
git switch -c feature/new-featuregit merge [branch]Merge a branch into the current branch
Example:
git merge feature/new-featuregit merge --no-ff [branch]Merge a branch with a merge commit
Example:
git merge --no-ff feature/new-featuregit remote add [name] [url]Add a remote repository
Example:
git remote add origin https://github.com/user/repo.gitgit remote -vList all remote repositories
git fetch [remote]Download objects and refs from a remote repository
Example:
git fetch origingit pullFetch from and integrate with another repository
git pull --rebaseFetch and rebase the current branch
git pushUpdate remote refs along with associated objects
git push [remote] [branch]Push a branch to a remote repository
Example:
git push origin maingit push -u [remote] [branch]Push and set upstream branch
Example:
git push -u origin maingit push --tagsPush all tags to remote repository
git logShow commit logs
git log --onelineShow commit logs in condensed format
git log --graphShow commit logs with ASCII graph
git log --all --decorate --oneline --graphShow visual commit history (dog = decorations, oneline, graph)
git diffShow changes between commits, commit and working tree, etc.
git diff --stagedShow changes staged for commit
git diff [branch1] [branch2]Show differences between two branches
Example:
git diff main developgit show [commit]Show various types of objects
Example:
git show HEADgit stashStash the changes in a dirty working directory
git stash save "[message]"Stash changes with a message
Example:
git stash save "Work in progress"git stash listList all stashes
git stash popApply and remove the most recent stash
git stash applyApply the most recent stash without removing it
git stash dropRemove the most recent stash
git stash clearRemove all stashes
git tagList all tags
git tag [tag-name]Create a lightweight tag
Example:
git tag v1.0.0git tag -a [tag-name] -m "[message]"Create an annotated tag
Example:
git tag -a v1.0.0 -m "Release version 1.0.0"git tag -d [tag-name]Delete a tag
Example:
git tag -d v0.9.0git push [remote] [tag-name]Push a tag to remote
Example:
git push origin v1.0.0git checkout -- [file]Discard changes in working directory
Example:
git checkout -- index.htmlgit restore [file]Restore working tree files (newer alternative)
Example:
git restore index.htmlgit restore --staged [file]Unstage a file
Example:
git restore --staged index.htmlgit revert [commit]Create a new commit that undoes changes from a previous commit
Example:
git revert abc123git reset [commit]Reset current HEAD to the specified state
Example:
git reset HEAD~1git reset --soft [commit]Reset HEAD but keep changes staged
Example:
git reset --soft HEAD~1git reset --hard [commit]Reset HEAD and discard all changes
Example:
git reset --hard HEAD~1git rebase [branch]Reapply commits on top of another base tip
Example:
git rebase maingit rebase -i [commit]Interactive rebase
Example:
git rebase -i HEAD~3git cherry-pick [commit]Apply the changes from a specific commit
Example:
git cherry-pick abc123git reflogShow reference logs (useful for recovering lost commits)
git bisect startStart binary search to find the commit that introduced a bug
git clean -fdRemove untracked files and directories
git status frequently to check your working directory stategit diff before committinggit reflog to recover "lost" commitsHelp others discover this tool!