Git 教程 - Git 基本用法
jopen
13年前
<span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">Git 是当前最流行的版本控制程序之一,文本包含了 Git 的一些基本用法 创建 git 仓库 初始化 git 仓库</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">mkdir</span> project <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 创建项目目录</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#7a0874;font-weight:bold;padding-top:0px;">cd</span> project <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 进入到项目目录</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> init <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 初始化 git 仓库。此命令会在当前目录新建一个 .git 目录,用于存储 git 仓库的相关信息</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">初始化提交</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">touch</span> README <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> add . <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 将当前目录添加到 git 仓库中, 使用 git add -A 则是添加所有改动的文档</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> commit <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">-m</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#ff0000;padding-top:0px;">"Initial commit"</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> remote add origin <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">@</span>github.com:lugir <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>repo.git <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 设置仓库</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">修补提交(修补最近一次的提交而不创建新的提交)</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> commit <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">--amend</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">-m</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#ff0000;padding-top:0px;">"commit message."</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">提交冲突时可以合并后再推送</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> pull <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 获取远程版本库提交与本地提交进行合并</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> push <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 提交</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">使用别人的仓库</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> clone http: <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">//</span>path <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>to <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>git.git <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># clone 的内容会放在当前目录下的新目录</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">将代码从本地回传到仓库</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> push <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">-u</span> origin master </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">使用 git status 查看文件状态</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> status </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">查看提交日志</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> log <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 查看提交信息</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> log <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">--pretty</span>=oneline <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 以整洁的单行形式显示提交信息</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">Git 分支</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> branch <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 查看分支</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> branch <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">6</span>.x- <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">1</span>.x <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 添加分支 6.x-1.x</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> branch checkout master <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 切换到主分支</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> branch <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">-d</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">6</span>.x- <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">1</span>.x <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 删除分支 6.x-1.x</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> push origin :branchname <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 删除远端分支</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">Git 标签</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> tag <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 查看分支</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> tag <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">6</span>.x- <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">1.0</span> <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 添加标签 6.x-1.0</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> show <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">6</span>.x- <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">1.0</span> <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 查看标签 6.x-1.0 的信息</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> tag <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">-a</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">6</span>.x- <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;padding-top:0px;">1.0</span> 965e066 <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 为之前提交的信息记录 965e066 加上标签</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> push <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">--tags</span> <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 提交时带上标签信息</span> <br /> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> push origin : <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>refs <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>tags <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>tagname <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 删除远端标签</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">从 git 仓库中导出项目</span> <div class="geshifilter"> <div class="bash geshifilter-bash"> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">git</span> archive <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">--format</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#c20cb9;font-weight:bold;padding-top:0px;">tar</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#660033;padding-top:0px;">--output</span> <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>path <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>to <span style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;color:#000000;font-weight:bold;padding-top:0px;">/</span>file.tar master <span style="padding-bottom:0px;font-style:italic;margin:0px;padding-left:0px;padding-right:0px;color:#666666;padding-top:0px;"># 将 master 以 tar 格式打包到指定文件</span> </div> </div> <span style="font-family:Verdana, Tahoma, 'DejaVu Sans', sans-serif;color:#4f4f4f;">使用 Git 的一些基本守则: 当要commit/提交patch时:</span> <ul> <li>使用 git diff --check 检查行尾有没有多余的空白</li> <li>每个 commit 只改一件事情。如果一个文档有多个变更,使用 git add --patch 只选择文档中的部分变更进入 stage</li> <li>写清楚 commit message</li> </ul>