Fundamentals 7 min read

Common Git Commands and Tips Cheat Sheet

This article provides a comprehensive cheat sheet of frequently used Git commands and practical tips, covering configuration, repository initialization, staging, committing, branching, merging, remote operations, and useful shortcuts for both beginners and experienced developers.

DevOps Engineer
DevOps Engineer
DevOps Engineer
Common Git Commands and Tips Cheat Sheet

Whether you are a Git beginner or an experienced developer, you will occasionally forget a command; this cheat sheet collects essential Git commands and techniques for quick reference.

git config

# 检查 git 配置
git config -l

# 设置你的 git 提交 username 和 email
# 例如:对于公司里项目
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# 例如:对于个人的 GitHub 项目
git config user.name "Your Name"
git config user.email "[email protected]"

# 使用 HTTP/HTTPS 避免每次输入密码
git config --global credential.helper cache

git init

# 初始化一个仓库
git init

git add

# 将文件添加到暂存区
git add file_name

# 将所有文件添加到暂存区
git add .

# 仅将某些文件添加到暂存区, 例如:仅添加所有以 'test*' 开头的文件
git add test*

git status

# 检查仓库状态
git status

git commit

# 提交更改
git commit

# 提交带有消息的更改
git commit -m "This is a commit message"

git log

# 查看提交历史
git log

# 查看提交历史和显示相应的修改
git log -p

# 显示提交历史统计
git log --stat

# 显示特定的提交
git show commit_id

# 以图形方式显示当前分支的提交信息
git log --graph --oneline

# 以图形方式显示所有分支的提交信息
git log --graph --oneline --all

# 获取远程仓库的当前提交日志
git log origin/master

git diff

# 在使用 diff 提交之前所做的更改
git diff
git diff some_file.js
git diff --staged

git rm

# 删除跟踪文件
git rm file_name

git mv

# 重命名文件
git mv old_file_name new_file_name

git checkout

# 切换分支
git checkout branch_name

# 还原未暂存的更改
git checkout file_name

git reset

# 还原暂存区的更改
git reset HEAD file_name
git reset HEAD -p

git commit

# 修改最近的提交信息
git commit --amend

# 修改最近的提交信息为:New commit message
git commit --amend -m "New commit message"

git revert

# 回滚最后一次提交
git revert HEAD

# 回滚指定一次提交
git revert commit_id

git branch

# 创建分支
git branch branch_name

# 创建分支并切到该分支
git checkout -b branch_name

# 显示当前分支
git branch

# 显示所有分支
git branch -a

# 检查当前正在跟踪的远程分支
git branch -r

# 删除分支
git branch -d branch_name

git merge

# 将 branch_name 合并到当分支
git merge branch_name

# 中止合并
git merge --abort

git pull

# 从远程仓库拉取更改
git pull

git fetch

# 获取远程仓库更改
git fetch

git push

# 推送更改到远程仓库
git push

# 推送一个新分支到远程仓库
git push -u origin branch_name

# 删除远程仓库分支
git push --delete origin branch_name

git remote

# 添加远程仓库
git add remote https://repository_name.com

# 查看远程仓库
git remote -v

# 查看远程仓库的更多信息
git remote show origin

Git技巧和窍门

清理已合并分支

清理已经合并的本地分支

git branch --merged master | grep -v "master" | xargs -n 1 git branch -d

.gitignore

指明 Git 应该忽略的故意不跟踪的文件的文件,比如 .gitignore 如下

# 忽略 .vscode 目录
.vscode/

# 忽略 build 目录
build/

# 忽略文件
output.log

.gitattributes

关于 .gitattributes 请参考

https://www.git-scm.com/docs/gitattributes

https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings

gitcommand-lineversion controldevelopment toolsCheat Sheet
DevOps Engineer
Written by

DevOps Engineer

DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.

0 followers
Reader feedback

How this landed with the community

login 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.