本文最后更新于 2026-03-18T10:06:53+08:00
在日常开发中,Git 是一个非常重要的工具,熟练掌握 Git 的常用命令,可以显著提高你的效率。以下是一些 Git 常用命令,分为几个核心类别进行整理。
1. Git 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --list
git config --global core.editor "vim"
git config --global core.autocrlf true
|
2. 仓库操作
1 2 3 4 5
| git init
git clone [仓库地址]
|
3. 文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git status
git add [文件名] git add .
git reset [文件名]
git commit -m "提交信息"
git commit --amend -m "新的提交信息"
git commit --no-edit
|
4. 分支操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| git branch
git branch [分支名]
git checkout [分支名]
git checkout -b [分支名]
git branch -d [分支名] git branch -D [分支名]
git merge [分支名]
git log --oneline --graph --decorate
|
5. 远程操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git remote -v
git remote add origin [仓库地址]
git push -u origin [分支名] git push
git pull [远程仓库] [分支名]
git fetch [远程仓库]
git branch --set-upstream-to=origin/[远程分支] [本地分支]
|
6. 日志查看
1 2 3 4 5 6 7 8
| git log
git log --oneline
git log [文件名]
|
7. 回退操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git checkout -- [文件名]
git reset --soft HEAD^
git reset --mixed HEAD^
git reset --hard [指定版本的commit_id]
git checkout [commit_id] -- [文件名]
|
8. 标签操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git tag [标签名]
git tag -a [标签名] -m "标签信息"
git tag
git push origin [标签名]
git push origin --tags
git tag -d [标签名]
git push origin --delete [标签名]
|
9. Stash(暂存功能)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git stash
git stash list
git stash pop
git stash apply stash@{编号}
git stash drop
git stash clear
|
10. 配合 .gitignore 文件
在项目根目录创建 .gitignore 文件,用来忽略不需要追踪的文件。
1 2 3 4 5 6 7
| touch .gitignore
*.log temp/ !important/
|
这些命令覆盖了 Git 的大部分常用功能,建议在实际开发中多加练习,熟悉这些命令的使用方式。