Fundamentals 7 min read

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.

php Courses
php Courses
php Courses
Git Cheat Sheet: Common Commands and Usage

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 name
git config --global user.email [<email>]      # set user email
git config --global core.editor [<vim>]       # set default editor

Create Project

git clone <options>                           # clone remote repository
git init [project]                           # initialize a local repository

Add & Commit

git add <file>                               # add file to staging area
git commit -m <commit notes>                # commit staged changes with a message
git commit -am <commit notes>               # add and commit in one step
git commit --amend -m <commit notes>         # amend the previous commit

Show Information

git status                                   # display repository status
git diff [HEAD]                              # show differences
git log                                     # show commit log
git show <commit>                           # show details of a specific commit
git blame <file>                            # show commit info for each line of a file

Revert Changes

git restore <file>                           # discard changes in working tree
git restore --staged <file>                # unstage a file
git reset [--mixed] <commit>                # move HEAD, keep working changes
git reset --soft <commit>                   # move HEAD, keep index and working changes
git reset --hard <commit>                   # move HEAD, discard all changes
git rm <file>                               # remove file from working tree and index
git mv <file>                               # move or rename a file

Branch Management

git branch [--list]                         # list branches
git branch -a                               # list remote branches
git branch <branch>                        # create a new branch
git branch -d|-D <branch>                    # delete a branch
git branch -m <newbranch>                   # rename current branch
git switch <branch>                         # switch to an existing branch
git switch -c <branch>                      # create and switch to a new branch
git merge <branch>                         # merge a branch into current
git tag <tagname>                         # create a tag on current commit
git stash                                   # stash changes
git stash apply                             # apply stashed changes without dropping
git stash drop                              # delete a stash
git stash pop                               # apply and delete stash

Remote Operations

git remote -v                               # list remote repositories
git remote show <origin>                   # show details of a remote
git remote add <origin> <url>               # add a new remote
git remote rm <origin>                     # remove a remote
git remote rename <oldname> <newname>    # rename a remote
git pull [<origin> [<branch>]]            # fetch and merge from remote
git push [-u <origin> <master>]              # push local commits to remote
git push origin --delete <branch>          # delete a remote branch
git fetch                                   # fetch objects from remote

Help

git help <command>                         # detailed help for a command
git <command> -h                            # short help for a command

Checkout (Not Recommended)

The checkout command is considered ambiguous and is discouraged.

git checkout <file>                        # discard changes in working tree
git checkout -f                            # force discard changes in working tree and index
git checkout <branch>                      # switch branches
git checkout -b <branch>                   # create and switch to a new branch
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsGitVersion ControlcommandsCheat Sheet
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.