<h1 class=”postTitle”><a id=”cb_post_title_url” class=”postTitle2″ href=”https://www.cnblogs.com/zhangfu/articles/10335471.html”>GIT 常用命令
<div class=”postBody”>
<div id=”cnblogs_post_body” class=”blogpost-body”>
一、新建代码库
$ git init <span class="token punctuation">[project<span class="token operator">-name<span class="token punctuation">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 下载一个项目和它的整个代码历史
<span style="color: #ff0000">$ git <span class="hljs-built_in"><span class="hljs-built_in">clone <span class="token punctuation">[url<span class="token punctuation">]
二、配置
Git的设置文件为.gitconfig
,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
$ git config <span class="token operator">-e <span class="token punctuation">[<span class="token operator">--global<span class="token punctuation">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 设置提交代码时的用户信息
$ git config <span class="token punctuation">[<span class="token operator">--global<span class="token punctuation">] user<span class="token punctuation">.name <span class="token string"><span class="hljs-string"><span class="hljs-string">"[name]"
$ git config <span class="token punctuation">[<span class="token operator">--global<span class="token punctuation">] user<span class="token punctuation">.email <span class="token string"><span class="hljs-string"><span class="hljs-string">"[email address]"
三、增加/删除文件
<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 添加指定目录到暂存区,包括子目录
$ git add <span class="token punctuation">[dir<span class="token punctuation">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 添加当前目录的所有文件到暂存区
<span style="color: #ff0000">$ git add <span class="token punctuation"><span style="color: #ff0000">.<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 添加每个变化前,都会要求确认
<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 对于同一个文件的多处变化,可以实现分次提交
$ git add <span class="token operator">-p<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 删除工作区文件,并且将这次删除放入暂存区
$ git rm <span class="token punctuation">[file1<span class="token punctuation">] <span class="token punctuation">[file2<span class="token punctuation">] <span class="token punctuation">.<span class="token punctuation">.<span class="token punctuation">.<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 停止追踪指定文件,但该文件会保留在工作区
$ git rm <span class="token operator">--cached <span class="token punctuation">[file<span class="token punctuation">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 改名文件,并且将这个改名放入暂存区
$ git mv <span class="token punctuation">[file<span class="token operator">-original<span class="token punctuation">] <span class="token punctuation">[file<span class="token operator">-renamed<span class="token punctuation">]
四、代码提交
<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 提交暂存区的指定文件到仓库区
<span style="color: #ff0000">$ git commit <span class="token punctuation"><span style="color: #ff0000">[file1<span class="token punctuation"><span style="color: #ff0000">] <span class="token punctuation"><span style="color: #ff0000">[file2<span class="token punctuation"><span style="color: #ff0000">] <span class="token punctuation"><span style="color: #ff0000">.<span class="token punctuation"><span style="color: #ff0000">.<span class="token punctuation"><span style="color: #ff0000">. <span class="token operator"><span style="color: #ff0000">-m <span class="token punctuation"><span style="color: #ff0000">[message<span class="token punctuation"><span style="color: #ff0000">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit <span class="token operator">-a<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 提交时显示所有diff信息
$ git commit <span class="token operator">-v<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 使用一次新的commit,替代上一次提交
<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit <span class="token operator">--amend <span class="token operator">-m <span class="token punctuation">[message<span class="token punctuation">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 重做上一次commit,并包括指定文件的新变化
$ git commit <span class="token operator">--amend <span class="token punctuation">[file1<span class="token punctuation">] <span class="token punctuation">[file2<span class="token punctuation">] <span class="token punctuation">.<span class="token punctuation">.<span class="token punctuation">.
五、分支
<span style="color: #ff0000">$ git branch <span class="token operator"><span style="color: #ff0000">-a<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 新建一个分支,但依然停留在当前分支
<span style="color: #ff0000">$ git branch <span class="token punctuation"><span style="color: #ff0000">[branch<span class="token operator"><span style="color: #ff0000">-name<span class="token punctuation"><span style="color: #ff0000">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 新建一个分支,并切换到该分支
<span style="color: #ff0000">$ git checkout <span class="token operator"><span style="color: #ff0000">-b <span class="token punctuation"><span style="color: #ff0000">[branch<span class="token punctuation"><span style="color: #ff0000">]<span class="token comment"><span class="hljs-comment"><span class="hljs-comment"># 新建一个分支,指向指定commit
$ git branch <span class="token punctuation">[branch<span class="token punctuation">] <span class="token punctuation">[commit<span class="token punctuation">]