欢迎光临
我们一直在努力

Git常见命令速查表

名词

  • master: 默认开发分支
  • origin: 默认远程版本库
  • Index / Stage:暂存区
  • Workspace:工作区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库

一、新建代码库

  1. 在当前目录新建一个Git代码库
<span class="token function">git</span> init
  1. 新建一个目录,将其初始化为Git代码库
<span class="token function">git</span> init <span class="token punctuation">[</span>project-name<span class="token punctuation">]</span> 
  1. 下载一个项目和它的整个代码历史
<span class="token function">git</span> clone <span class="token punctuation">[</span>url<span class="token punctuation">]</span>

二、配置

Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

  1. 显示当前的Git配置
<span class="token function">git</span> config --list 
  1. 编辑Git配置文件
<span class="token function">git</span> config -e <span class="token punctuation">[</span>--global<span class="token punctuation">]</span>  
  1. 设置提交代码时的用户信息
<span class="token function">git</span> config <span class="token punctuation">[</span>--global<span class="token punctuation">]</span> user.name <span class="token string">"[name]"</span> 
<span class="token function">git</span> config <span class="token punctuation">[</span>--global<span class="token punctuation">]</span> user.email <span class="token string">"[email address]"</span>

三、增加/删除/修改文件

  1. 查看状态
<span class="token function">git</span> status   
  1. 查看变更内容
<span class="token function">git</span> <span class="token function">diff</span>          
  1. 添加指定文件到暂存区
<span class="token function">git</span> <span class="token function">add</span> <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">..</span>. 

  1. 添加指定目录到暂存区,包括子目录
<span class="token function">git</span> <span class="token function">add</span> <span class="token punctuation">[</span>dir<span class="token punctuation">]</span> 

  1. 添加当前目录的所有文件到暂存区
<span class="token function">git</span> <span class="token function">add</span> <span class="token builtin class-name">.</span> 

  1. 添加每个变化前,都会要求确认(对于同一个文件的多处变化,可以实现分次提交)
<span class="token function">git</span> <span class="token function">add</span> -p 

  1. 删除工作区文件,并且将这次删除放入暂存区
<span class="token function">git</span> <span class="token function">rm</span> <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">..</span>. 

  1. 停止追踪指定文件,但该文件会保留在工作区
<span class="token function">git</span> <span class="token function">rm</span> --cached <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 改名文件,并且将这个改名放入暂存区
<span class="token function">git</span> <span class="token function">mv</span> <span class="token punctuation">[</span>file-original<span class="token punctuation">]</span> <span class="token punctuation">[</span>file-renamed<span class="token punctuation">]</span>

四、代码提交

  1. 提交暂存区到仓库区
<span class="token function">git</span> commit -m <span class="token punctuation">[</span>message<span class="token punctuation">]</span> 

  1. 提交暂存区的指定文件到仓库区
<span class="token function">git</span> commit <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">..</span>. -m <span class="token punctuation">[</span>message<span class="token punctuation">]</span>

  1. 提交工作区自上次commit之后的变化,直接到仓库区
<span class="token function">git</span> commit -a 

  1. 提交时显示所有diff信息
<span class="token function">git</span> commit -v 

  1. 使用一次新的commit,替代上一次提交(如果代码没有任何新变化,则用来改写上一次commit的提交信息)
 <span class="token function">git</span> commit --amend -m <span class="token punctuation">[</span>message<span class="token punctuation">]</span>  

  1. 重做上一次commit,并包括指定文件的新变化
<span class="token function">git</span> commit --amend <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">..</span>. 

五、分支

  1. 显示所有本地分支
<span class="token function">git</span> branch

  1. 列出所有远程分支
<span class="token function">git</span> branch -r

  1. 列出所有本地分支和远程分支
<span class="token function">git</span> branch -a 

  1. 新建一个分支,但依然停留在当前分支
<span class="token function">git</span> branch <span class="token punctuation">[</span>branch-name<span class="token punctuation">]</span>

  1. 新建一个分支,与指定的远程分支建立追踪关系
<span class="token function">git</span> branch --track <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>remote-branch<span class="token punctuation">]</span>

  1. 删除分支
<span class="token function">git</span> branch -d <span class="token punctuation">[</span>branch-name<span class="token punctuation">]</span>

  1. 删除远程分支
<span class="token function">git</span> push origin --delete <span class="token punctuation">[</span>branch-name<span class="token punctuation">]</span>
<span class="token function">git</span> branch -dr <span class="token punctuation">[</span>remote/branch<span class="token punctuation">]</span> 
<span class="token function">git</span> push <span class="token operator"><</span>remote<span class="token operator">></span> :<span class="token operator"><</span>branch<span class="token operator">></span>

  1. 新建一个分支,并切换到该分支
<span class="token function">git</span> checkout -b <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

  1. 切换到指定分支,并更新工作区
<span class="token function">git</span> checkout <span class="token punctuation">[</span>branch-name<span class="token punctuation">]</span> 

  1. 切换到上一个分支
<span class="token function">git</span> checkout -

  1. 建立追踪关系,在现有分支与指定的远程分支之间
<span class="token function">git</span> branch --set-upstream <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>remote-branch<span class="token punctuation">]</span> 

  1. 合并指定分支到当前分支
<span class="token function">git</span> merge <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

  1. 衍合指定分支到当前分支
<span class="token function">git</span> rebase <span class="token operator"><</span>branch<span class="token operator">></span> 

  1. 选择一个commit,合并进当前分支
<span class="token function">git</span> cherry-pick <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

六、标签

  1. 列出所有本地tag
<span class="token function">git</span> tag

  1. 基于最新提交创建tag
<span class="token function">git</span> tag <span class="token operator"><</span>tagname<span class="token operator">></span>

  1. 删除本地tag
<span class="token function">git</span> tag -d

  1. 删除远程tag
<span class="token function">git</span> push <span class="token operator"><</span>remote<span class="token operator">></span> :<span class="token operator"><</span>tag-name<span class="token operator">></span> 
<span class="token function">git</span> push origin :refs/tags/<span class="token punctuation">[</span>tag-name<span class="token punctuation">]</span>

  1. 查看tag信息
<span class="token function">git</span> show <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

  1. 提交指定tag
<span class="token function">git</span> push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

  1. 提交所有tag
<span class="token function">git</span> push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> --tags

  1. 新建一个分支,指向某个tag
<span class="token function">git</span> checkout -b <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

七、查看信息

  1. 显示有变更的文件
<span class="token function">git</span> status

  1. 显示当前分支的commit历史
<span class="token function">git</span> log

  1. 显示当前分支的commit历史 ,以及每次commit发生变更的文件
<span class="token function">git</span> log -stat

  1. 搜索commit历史,根据关键词
<span class="token function">git</span> log -S <span class="token punctuation">[</span>keyword<span class="token punctuation">]</span>

  1. 显示某个commit之后的所有提交说明,每个commit说明占据一行
<span class="token function">git</span> log HEAD --pretty<span class="token operator">=</span>format:%s

  1. 显示某个文件的版本历史,包括文件改名
<span class="token function">git</span> log --follow <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 
<span class="token function">git</span> whatchanged <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 显示指定文件相关的每一次diff
<span class="token function">git</span> log -p <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 显示过去5次提交
<span class="token function">git</span> log -5 --pretty --oneline 

  1. 显示所有提交过的用户,按提交次数排序
<span class="token function">git</span> shortlog -sn 

  1. 显示指定文件是什么人在什么时间修改过
<span class="token function">git</span> blame <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 显示暂存区和工作区的差异
<span class="token function">git</span> <span class="token function">diff</span>  

  1. 显示暂存区和上一个commit的差异
 <span class="token function">git</span> <span class="token function">diff</span> --cached <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 显示工作区与当前分支最新commit之间的差异
<span class="token function">git</span> <span class="token function">diff</span> HEAD 

  1. 显示两次提交之间的差异
<span class="token function">git</span> <span class="token function">diff</span> <span class="token punctuation">[</span>first-branch<span class="token punctuation">]</span><span class="token punctuation">..</span>.<span class="token punctuation">[</span>second-branch<span class="token punctuation">]</span> 

  1. 显示今天你写了多少行代码
<span class="token function">git</span> <span class="token function">diff</span> --shortstat <span class="token string">"@{0 day ago}"</span> 

  1. 显示某次提交的元数据和内容变化
<span class="token function">git</span> show <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> 

  1. 显示某次提交发生变化的文件
<span class="token function">git</span> show --name-only <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>  

  1. 显示某次提交时,某个文件的内容
<span class="token function">git</span> show <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>:<span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 显示当前分支的最近几次提交
<span class="token function">git</span> reflog

八、远程操作

  1. 下载远程仓库的所有变动
<span class="token function">git</span> fetch <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> 

  1. 取回远程仓库的变化,并与本地分支合并
<span class="token function">git</span> pull <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

  1. 显示所有远程仓库
<span class="token function">git</span> remote -v

  1. 显示某个远程仓库的信息
<span class="token function">git</span> remote show <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> 

  1. 增加一个新的远程仓库,并命名
<span class="token function">git</span> remote <span class="token function">add</span> <span class="token punctuation">[</span>shortname<span class="token punctuation">]</span> <span class="token punctuation">[</span>url<span class="token punctuation">]</span> 

  1. 上传本地指定分支到远程仓库
<span class="token function">git</span> push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> 

  1. 强行推送当前分支到远程仓库,即使有冲突
 <span class="token function">git</span> push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> --force 

  1. 推送所有分支到远程仓库
<span class="token function">git</span> push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> --all

  1. 删除远程分支或标签
<span class="token function">git</span> push <span class="token operator"><</span>remote<span class="token operator">></span> :<span class="token operator"><</span>branch/tag-name<span class="token operator">></span> 

九、撤销

  1. 撤销工作目录中所有未提交文件的修改内容
<span class="token function">git</span> reset --hard HEAD 

  1. 撤销指定的未提交文件的修改内容
<span class="token function">git</span> checkout HEAD <span class="token operator"><</span>file<span class="token operator">></span>

  1. 撤销指定的提交
<span class="token function">git</span> revert <span class="token operator"><</span>commit<span class="token operator">></span>

  1. 退回到之前1天的版本
<span class="token function">git</span> log --before<span class="token operator">=</span><span class="token string">"1 days"</span>

  1. 恢复暂存区的指定文件到工作区
<span class="token function">git</span> checkout <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

  1. 恢复某个commit的指定文件到暂存区和工作区
<span class="token function">git</span> checkout <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

  1. 恢复暂存区的所有文件到工作区
<span class="token function">git</span> checkout <span class="token builtin class-name">.</span> 

  1. 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
<span class="token function">git</span> reset <span class="token punctuation">[</span>file<span class="token punctuation">]</span> 

  1. 重置暂存区与工作区,与上一次commit保持一致
<span class="token function">git</span> reset --hard 

  1. 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
<span class="token function">git</span> reset <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> 

  1. 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
<span class="token function">git</span> reset --hard <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> 

  1. 重置当前HEAD为指定commit,但保持暂存区和工作区不变
<span class="token function">git</span> reset --keep <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> 

  1. 新建一个commit,用来撤销指定commit ( 后者的所有变化都将被前者抵消,并且应用到当前分支 )
<span class="token function">git</span> revert <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> 

十、储藏(贮藏)

  1. 暂时将未提交的变更储藏
<span class="token function">git</span> stash 

  1. 查看现有的储藏
<span class="token function">git</span> stash list

  1. 应用最近的储藏
<span class="token function">git</span> stash apply

  1. 应用某储藏
<span class="token function">git</span> stash --index

  1. 应用某储藏并删除该储藏
<span class="token function">git</span> stash pop

  1. 移除某储藏
<span class="token function">git</span> stash drop <span class="token variable">${stash-name}</span>

十一、其他

  1. 生成一个可供发布的压缩包
<span class="token function">git</span> archive
未经允许不得转载:798VPS » Git常见命令速查表

相关推荐

  • 暂无文章