title: git命令总结
categories: Git
Git基本配置
git config --list # 查看git基本配置
git config --system -l # 查看git系统配置
git config --global -l # 查看git用户配置
设置用户配置
git config --system user.name "用户名"
git config --system user.email 邮箱
# 使用git时必须配置此项
初始化仓库
git init
拉取远程仓库或分支
git clone <url> # 克隆一个仓库到本地
git clone -b <分支名> <远程仓库地址> # 克隆远程仓库指定分支到本地
git pull # 拉取主分支最新更改
git pull origin <分支名> # 拉取某个分支最新更改
添加文件到暂存区
git add <file> # 添加指定文件到git的暂存区
git add . # 添加全部文件到git的暂存区
查看工作区的状态
git status # 可以查看在你上次提交之后是否有对文件进行再次修改
提交文件
git commit -m "提交的说明" # 把暂存区的文件提价到当前分支
git commit -a # 参数设置修改文件后不需要执行git add 命令,直接来提交
git commit --amend # 修改最近一次的提交说明
推送文件到远程仓库
git push
git push origin master # 将本地的master分支推送到origin主机的master分支
git push -f origin master # 版本不同时,强行推送到远程
查看提交日志
git log
git blame <file> # 查看指定文件的修改记录
版本回退
git reset --hard # 撤销工作区和暂存区的所有修改
git reset --hard^ # 回退到上一个版本
git reset --hard^^ # 回退到上上一个版本
git reset <版本号> # 回退到指定的版本
文件删除
git rm <file> # 将文件从暂存区和工作区中删除
git rm -f <file> # 强行从暂存区和工作区中删除修改后的文件
git rm --cached <file> # 从暂存区中删除文件,保留工作区
文件删除
git rm <file> # 将文件从暂存区和工作区中删除
git rm -f <file> # 强行从暂存区和工作区中删除修改后的文件
git rm --cached <file> # 从暂存区中删除文件,保留工作区
对远程仓库的操作
git remote # 列出已经存在的远程分支
git remote -v # 列出详细信息,显示对应的克隆地址
git remote add url # 添加一个远程仓库
git remote rm name # 删除远程仓库
git remote set-url origin [url] # 修改远程仓库地址
git remote rename old_name new_name # 修改仓库名
分支操作
git branch # 查看当前分支并列出所有分支
git branch <name> # 创建一个新的分支
git branch -d <name> # 删除某个分支
git checkout <name> # 切换到某个分支
git checkout -b <name> # 基于当前分支,创建并切换的到新分支
git marge <name> # 把某个分支合并到当前分支