初始化配置
# 查询配置信息 git config --list # 列出当前配置 git config --local --list # 列出仓库配置 git config --global --list # 列出全局配置 git config --system --list # 列出系统配置 # 配置用户信息 git config --global user.name <user> # 配置用户名 git config --global user.email <email> # 配置用户邮箱 # 其他配置 git config --global merge.tool vimdiff # 配置解决冲突时使用哪种差异分析工具,比如要使用vimdiff git config --global alias.co checkout # 配置别名 git config --global color.ui auto # 配置git命令输出为彩色的 git config --global core.editor vi # 配置git使用的文本编辑器 git config --global credential.helper cache # 配置到缓存 默认15分钟 git config --global credential.helper 'cache --timeout=3600' # 修改缓存时间
基本操作
# 增加文件 git add . # 添加当前目录的所有文件到暂存区 git add <file>... # 添加指定文件到暂存区 git add <dir> # 添加指定目录到暂存区,包括子目录 # 删除文件 git rm <file>... # 将工作区,暂存区文件删除 git rm --cached <file> # 将暂存区文件删除,工作区保留 # 提交文件 git commit -m <message> # 提交暂存区的所有文件到本地库 git commit <file>... -m <message> # 提交暂存区的指定文件到本地库 # 撤销文件 # reset [<mode>] 参数说明:(--soft:在本地库移动 HEAD 指针,--mixed:在本地库移动 HEAD 指针, 重置暂存区,--hard:在本地库移动 HEAD 指针, 重置暂存区, 重置工作区) git reset --hard HEAD # 将暂存区文件恢复到工作区 git reset --hard <id> # 基于索引值, 可前进后退 git reset --hard HEAD^ # 使用^符号:只能前进 (一个^表示前进一步,n 个表示前进 n 步) git reset --hard HEAD~n # 使用~符号:只能前进 (表示前进 n 步) # 比较文件 git diff <file> # 比较工作区和暂存区文件差异 git diff <id> <file> # 比较工作区和本地库文件差异 git diff <id1> <id2> # 比较本地库两个文件差异 git diff <branch1> <branch2> # 比较两个分支之间的文件差异 # 重命名文件 git mv <file> <renamefile> # 重命名文件 # 查看工作区、暂存区状态 git status # 查看所有文件状态 git status <file> # 查看指定文件状态 # 查看历史记录 git log # 查看所有历史记录 (多屏显示控制方式:空格向下翻页, b 向上翻页, q 退出) git log <file> # 查看指定文件历史记录
git log <dir> # 查看指定目录历史记录 git log --pretty=oneline # 每条记录只显示一行,只能查看之前历史记录 git log --oneline # 每条记录只显示一行,只能查看之前历史记录 git reflog # 查看所有分支的所有操作记录
远程操作
# 创建远程库地址别名 git remote add <remote> <url> # 创建远程库url别名 # 修改远程库地址url git remote set-url <remote> <url> # 修改远程库url地址 # 删除远程库地址别名 git remote rm <remote> # 删除远程库url别名 # 查看远程库 git remote -v # 查看所有远程库 git remote show <remote> # 查看某个远程仓库的信息 # 推送远程库 git push # 推送本地所有分支到远程仓库 git push <remote> <branch> # 推送本地指定分支到远程仓库 git push <remote> <local-branch>:<remote-branch> # 推送本地指定分支到远程仓库指定分支 git push <remote> --force # 强行推送当前分支到远程仓库,即使有冲突 git push <remote> --all # 推送所有分支到远程仓库 # 克隆远程库 git clone <url> # 克隆项目(clone命令功能:1.完整的把远程库下载到本地; 2.创建<remote>远程地址别名; 3.初始化本地库) # 拉取远程库 git fetch <remote> <branch> # 从远程库拉取指定分支到本地库 git pull # 从远程库拉取所有分支到本地库,并合并到工作区(pull = fetch + merge) git pull <remote> <branch> # 从远程库拉取指定分支到本地库,并合并到工作区(pull = fetch + merge)
分支操作
# 查看分支 git branch # 列出所有本地分支 git branch -r # 列出所有远程分支 git branch -a # 列出所有本地和远程分支 # 新建分支 git branch <branch> # 新建本地分支,但依然停留在当前分支 git checkout -b <branch> # 新建本地分支,并切换到该分支 # 删除分支 git branch -d <branch> # 删除本地分支 git branch -dr <remote/branch> # 删除远程分支 git push <remote> --delete <branch> # 删除远程分支 # 切换分支 git checkout <branch> # 切换到指定分支 git checkout - # 切换到上一个分支 # 合并分支 git merge <branch> # 合并指定分支到当前分支
版本操作
# 查看版本 git tag # 查看本地库版本 git tag -r # 查看远程库版本 # 创建版本 git tag <tag> # 创建本地库版本 git tag -a <tag> -m <message> # 创建本地库版本(带注释) git push <remote> <tag> # 创建远程库版本(本地库版本push到远程) # 删除版本 git tag -d <name> # 删除本地库版本 git push <remote> --delete tag <tag> # 删除远程库版本 # 切换版本 git checkout <tag> # 切换版本 # 拉取远程版本 git pull <remote> --tags # 拉取远程库所有版本到本地 # 推送远程版本 git push <remote> <tag> # 推送本地库指定版本到远程 git push <remote> --tags # 推送本地库所有版本到远程
其他操作
git help <command> # 显示command的help
参考地址:https://wenku.baidu.com/view/88ae903084254b35effd3487.html