Git 常用命令
设置查看:
git config --list # 查看所有
git config user.name # 查看用户名
git config --local --list # 查看仓库级别配置
git config --global --list # 查看用户级别配置
用户名和邮箱:
1. --local
作用范围:当前 Git 仓库
保存位置:Git 仓库的 .git/config
git config --local user.name "Your Name"
git config --local user.email "youremail@example.com"
2. --global
作用范围:当前用户
保存位置:该用户主目录下的 .gitconfig
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
3. --system
作用范围:系统
保存位置:/etc/gitconfig
注意:对所有用户和所有 Git 仓库都有效,一般需要sudo权限
sudo git config --system core.editor "nano"
4. 不带参数
读取/写入顺序:仓库级别 > 用户级别 > 系统级别
git config user.name "Your Name"
git config user.email "your.email@example.com"
查看项目状态:
git status
文件修改提交到本地暂存区:
git add <filename1> <filename2> # 提交某个文件
git add -A # 提交全部
提交当前工作空间的修改内容:
git commit -m "提交说明" # 本地
add、commit 撤销:
git reset --soft HEAD^ # commit 撤销
git reset HEAD # add 撤销
pull、push:
git pull origin <branchname>
git pull <url> <branchname>
git push origin <branchname> # 把已经commit的内容传到远程服务器
git push <url> <branchname>
# url 查看
git remote -v
分支:
git branch # 查看本地分支
git branch -r # 查看远程分支
git branch -vv # 查看分支合并状态
git checkout <branch_name> # 切换到存在分支
git checkout -b <new_branch_name> # 创建并切换到新的分支
git fetch origin # 切换到远程分支1:先拉取
git checkout origin/<branch_name> # 切换到远程分支2:后切换