本文整理 Git 日常使用频率最高的命令,包含用户配置、仓库初始化、远程地址、分支提交和常见协作命令。
如果只需要快速查询某条命令,可以配合阅读:Git 速查表。
配置用户信息
1 2
| git config --global user.name "LeroiLiu" git config --global user.email "your-email@example.com"
|
查看当前配置:
1 2
| git config --global --list git config --local --list
|
如果某个项目需要单独使用不同身份,可以在项目目录里去掉 --global:
1 2
| git config user.name "LeroiLiu" git config user.email "your-email@example.com"
|
创建新仓库
1 2 3 4 5 6 7 8
| mkdir project-name cd project-name git init touch README.md git add README.md git commit -m "first commit" git remote add origin git@gitee.com:leroiliu/project-name.git git push -u origin master
|
如果仓库名就是 qq,完整示例可以写成:
1 2 3 4 5 6 7 8
| mkdir qq cd qq git init touch README.md git add README.md git commit -m "first commit" git remote add origin git@gitee.com:leroiliu/qq.git git push -u origin master
|
如果默认分支使用 main,可以这样设置:
1 2
| git branch -M main git push -u origin main
|
关联已有仓库
已有本地项目时,可以直接添加远程地址:
1 2 3
| cd existing_git_repo git remote add origin git@gitee.com:leroiliu/project-name.git git push -u origin master
|
示例:
1 2 3
| cd existing_git_repo git remote add origin git@gitee.com:leroiliu/qq.git git push -u origin master
|
如果远程地址填错了,可以修改:
1 2
| git remote set-url origin git@gitee.com:leroiliu/project-name.git git remote -v
|
示例:
1
| git remote set-url origin git@gitee.com:leroiliu/qq.git
|
分支操作
创建并切换分支:
1
| git switch -c feature-name
|
老版本 Git 也可以使用:
1 2
| git branch feature-name git checkout feature-name
|
简单示例:
1 2 3 4 5
| git branch branch git checkout branch git add . git commit -m branch git push origin branch
|
提交并推送分支:
1 2 3
| git add . git commit -m "feat: add feature name" git push -u origin feature-name
|
查看分支:
1 2
| git branch git branch -a
|
删除本地分支:
1
| git branch -d feature-name
|
删除远程分支:
1
| git push origin --delete feature-name
|
查看改动
1 2 3 4
| git status git diff git diff --staged git log --oneline --graph --decorate --all
|
常用判断:
拉取和推送
1 2 3
| git fetch origin git pull git push
|
第一次推送新分支时建议加 -u,后续就可以直接 git push:
1
| git push -u origin feature-name
|
暂存临时改动
临时切换分支但当前改动还不想提交时,可以使用 stash:
1 2 3
| git stash push -m "work in progress" git stash list git stash pop
|
如果只是想恢复最新一次暂存但不删除记录,可以使用:
标签
创建版本标签:
1 2
| git tag v1.0.0 git push origin v1.0.0
|
查看标签:
删除本地和远程标签:
1 2
| git tag -d v1.0.0 git push origin --delete v1.0.0
|