Git Cheat Sheet: Common Commands and Usage
This cheat sheet provides a concise reference of essential Git commands—including configuration, repository creation, staging, committing, branching, remote handling, and restoration—along with brief explanations and code examples for developers to quickly perform version‑control tasks.
Git Cheat Sheet
git version 2.36.0
Documentation Notation
<> indicates placeholders that need to be replaced.
[] indicates optional items.
| indicates alternatives.
Working tree (working directory), index (staging area), and Git directory (HEAD) correspond to the three main areas of Git.
Initial Configuration
<code>git config --global user.name [<username>] # set user name</code> <code>git config --global user.email [<email>] # set user email</code> <code>git config --global core.editor [<vim>] # set default editor</code>Create Project
<code>git clone <options> # clone remote repository</code> <code>git init [project] # initialize a local repository</code>Add & Commit
<code>git add <file> # add file to staging area</code> <code>git commit -m <commit notes> # commit staged changes with a message</code> <code>git commit -am <commit notes> # add and commit in one step</code> <code>git commit --amend -m <commit notes> # amend the previous commit</code>Show Information
<code>git status # display repository status</code> <code>git diff [HEAD] # show differences</code> <code>git log # show commit log</code> <code>git show <commit> # show details of a specific commit</code> <code>git blame <file> # show commit info for each line of a file</code>Revert Changes
<code>git restore <file> # discard changes in working tree</code> <code>git restore --staged <file> # unstage a file</code> <code>git reset [--mixed] <commit> # move HEAD, keep working changes</code> <code>git reset --soft <commit> # move HEAD, keep index and working changes</code> <code>git reset --hard <commit> # move HEAD, discard all changes</code> <code>git rm <file> # remove file from working tree and index</code> <code>git mv <file> # move or rename a file</code>Branch Management
<code>git branch [--list] # list branches</code> <code>git branch -a # list remote branches</code> <code>git branch <branch> # create a new branch</code> <code>git branch -d|-D <branch> # delete a branch</code> <code>git branch -m <newbranch> # rename current branch</code> <code>git switch <branch> # switch to an existing branch</code> <code>git switch -c <branch> # create and switch to a new branch</code> <code>git merge <branch> # merge a branch into current</code> <code>git tag <tagname> # create a tag on current commit</code> <code>git stash # stash changes</code> <code>git stash apply # apply stashed changes without dropping</code> <code>git stash drop # delete a stash</code> <code>git stash pop # apply and delete stash</code>Remote Operations
<code>git remote -v # list remote repositories</code> <code>git remote show <origin> # show details of a remote</code> <code>git remote add <origin> <url> # add a new remote</code> <code>git remote rm <origin> # remove a remote</code> <code>git remote rename <oldname> <newname> # rename a remote</code> <code>git pull [<origin> [<branch>]] # fetch and merge from remote</code> <code>git push [-u <origin> <master>] # push local commits to remote</code> <code>git push origin --delete <branch> # delete a remote branch</code> <code>git fetch # fetch objects from remote</code>Help
<code>git help <command> # detailed help for a command</code> <code>git <command> -h # short help for a command</code>Checkout (Not Recommended)
The checkout command is considered ambiguous and is discouraged.
<code>git checkout <file> # discard changes in working tree</code> <code>git checkout -f # force discard changes in working tree and index</code> <code>git checkout <branch> # switch branches</code> <code>git checkout -b <branch> # create and switch to a new branch</code>php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.