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
git config --global user.name [<username>] # set user namegit config --global user.email [<email>] # set user emailgit config --global core.editor [<vim>] # set default editorCreate Project
git clone <options> # clone remote repositorygit init [project] # initialize a local repositoryAdd & Commit
git add <file> # add file to staging areagit commit -m <commit notes> # commit staged changes with a messagegit commit -am <commit notes> # add and commit in one stepgit commit --amend -m <commit notes> # amend the previous commitShow Information
git status # display repository statusgit diff [HEAD] # show differencesgit log # show commit loggit show <commit> # show details of a specific commitgit blame <file> # show commit info for each line of a fileRevert Changes
git restore <file> # discard changes in working treegit restore --staged <file> # unstage a filegit reset [--mixed] <commit> # move HEAD, keep working changesgit reset --soft <commit> # move HEAD, keep index and working changesgit reset --hard <commit> # move HEAD, discard all changesgit rm <file> # remove file from working tree and indexgit mv <file> # move or rename a fileBranch Management
git branch [--list] # list branchesgit branch -a # list remote branchesgit branch <branch> # create a new branchgit branch -d|-D <branch> # delete a branchgit branch -m <newbranch> # rename current branchgit switch <branch> # switch to an existing branchgit switch -c <branch> # create and switch to a new branchgit merge <branch> # merge a branch into currentgit tag <tagname> # create a tag on current commitgit stash # stash changesgit stash apply # apply stashed changes without droppinggit stash drop # delete a stashgit stash pop # apply and delete stashRemote Operations
git remote -v # list remote repositoriesgit remote show <origin> # show details of a remotegit remote add <origin> <url> # add a new remotegit remote rm <origin> # remove a remotegit remote rename <oldname> <newname> # rename a remotegit pull [<origin> [<branch>]] # fetch and merge from remotegit push [-u <origin> <master>] # push local commits to remotegit push origin --delete <branch> # delete a remote branchgit fetch # fetch objects from remoteHelp
git help <command> # detailed help for a commandgit <command> -h # short help for a commandCheckout (Not Recommended)
The checkout command is considered ambiguous and is discouraged.
git checkout <file> # discard changes in working treegit checkout -f # force discard changes in working tree and indexgit checkout <branch> # switch branchesgit checkout -b <branch> # create and switch to a new branchSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
