For common Git operations in everyday development. It’s organized into categories to help you get started or get started.
📦 Basic Commands
order
corresponds English -ity, -ism, -ization
git init
Initialize a Git repository (create a .git folder).
git clone <url>
Clone the remote repository to local.
git status
View the current working directory status (file modifications, staging, etc.).
git add <file>
Adds a file to the staging area.
git commit -m "message"
Commit staging area files to the local repository and add commit information.
git log
View submission history.
🌱 Branch Management
order
corresponds English -ity, -ism, -ization
git branch
View a list of local branches.
git branch <branch-name>
Create a new branch.
git checkout <branch-name>
Switch to the specified branch.
git switch <branch-name>
Switch to the specified branch (recommended new command).
git merge <branch-name>
Merges the specified branch into the current branch.
git branch -d <branch-name>
Deletes the merged branch.
git branch -D <branch-name>
Forces the branch to be deleted.
🌍 Remote Warehouse Operations
order
corresponds English -ity, -ism, -ization
git remote -v
View the remote repository address.
git remote add <name> <url>
Add a remote repository.
git pull
Pull code from remote repositories and merge it.
git push
Pushes the current branch code to the remote repository.
git push -u origin <branch-name>
Push the branch and set up the upstream (trace) branch.
git fetch
Get the latest data from the remote repository, but do not merge it automatically.
git remote remove <name>
Remove the remote repository connection.
🔍 View & Compare
order
corresponds English -ity, -ism, -ization
git diff
View changes that have not been staged in the workspace.
git diff --staged
View changes to the staging area.
git log --oneline
Succinctly view submission history.
git show <commit-id>
View the details of a particular commit.
git blame <file>
View the commit records corresponding to each line of the file.
♻️ Undo and Rollback
order
corresponds English -ity, -ism, -ization
git checkout -- <file>
Undo changes to workspace files.
git restore <file>
Undo file modification (new command).
git reset HEAD <file>
Moves the file from the staging area back to the work area.
git reset --soft <commit-id>
Fall back to a commit, keeping all changes.
git reset --mixed <commit-id>
Falls back and clears the staging area, but keeps the workspace changes.
git reset --hard <commit-id>
Revert back and clear all changes.
git revert <commit-id>
Generate a new commit that undoes the contents of a particular commit.
🏷️ Tag Management
order
corresponds English -ity, -ism, -ization
git tag
View all tags.
git tag <tag-name>
Create Tags.
git tag -d <tag-name>
Delete the label.
git push origin <tag-name>
Push the specified label.
git push origin --tags
Push all tags to the remote.
🗃️ Stash (temporary storage)
order
corresponds English -ity, -ism, -ization
git stash
Staging currently uncommitted changes.
git stash list
View all staging records.
git stash apply
Restores the contents of the most recent stash.
git stash drop
Deletes the most recent stash.
git stash pop
Recover and delete the most recent stash.
🛠️ Other useful commands
order
corresponds English -ity, -ism, -ization
git config --global user.name "Your Name"
Set the global username.
git config --global user.email "you@example.com"
Set the global mailbox.
git config --list
View the current Git configuration.
git clean -f
Deletes files that are not being tracked.
git cherry-pick <commit-id>
Apply a commit to the current branch.
git rebase <branch>
The current branch bases itself on the target branch.
🧪 Examples of common branching operations
# 拉取 PR 分支代码(如 GitHub Pull Request)
git fetch origin pull/376/head:pr-review
# Switch to the test branch
git checkout pr-review
# Switch back to the main branch
git checkout main
# Merge test branches
git merge pr-review
# Merge into one commit
git merge --squash pr-review
git commit -m "Merge all changes"
# Fall back to a commit
git reset --hard <commit-id>
# Delete the local branch
git branch -d pr-review
# Force a local branch to be deleted
git branch -D pr-review
# 删除远程分支
git push origin --delete <branch-name>
# Clear remote branch cache
git fetch -p
# 强制覆盖远程分支
git push origin main --force
📚 延伸阅读
If you use Git a lot, you might want to bookmark this post for future reference! Feel free to add your favorite Git tips in the comments.