20130907.Git学习记录

亡梦爱人 提交于 2020-02-29 08:48:16

<div><strong>1、任何文件在Git内都只有三种状态:</strong></div><blockquote style="MARGIN-RIGHT: 0px" dir=ltr> <div><font color=#ff0000>①已提交(committed):</font>已提交表示该文件已经被安全地保存在本地数据库中了;</div> <div><font color=#ff0000>②已修改(modified):</font>已修改表示修改了某个文件,但还没有提交保存;</div> <div><font color=#ff0000>③和已暂存(staged):</font>已暂存表示把已修改的文件放在下次提交时要保存的清单中。</div></blockquote><div>由此我们看到Git管理项目时,文件流转的三个工作区域:Git&nbsp;的本地数据目录,工作目录以及暂存区域。</div><div>&nbsp;</div><div><strong>2、先决条件:</strong></div><div>1)在GitHub上面注册一个账号并激活</div><div>2)安装windows下的git客户端如msysgit</div><div>&nbsp;</div><div>*****************************华丽的分割线*****************************</div><div></div><div><strong><span style="FONT-SIZE: 24px"><font size=6>一、使用SSH建立本地Git跟GitHub的连接</font></span></strong></div><div>SSH是什么,我也不清楚,只知道很多连接相关的都叫SSH比如说XShell连接Linux主机的时候就是SSH,应该是一种连接协议吧,需要找一个谷歌。</div><div>&nbsp;</div><div><strong><span style="COLOR: #ff0000">1、查看本地是否存在SSH:</span></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215846_kApe.jpg" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/6214331.jpg"></div><div>No&nbsp;such&nbsp;file&nbsp;or&nbsp;directory&nbsp;说明不存在ssh,则需要去创建一个新的ssh&nbsp;keys,使用命令建立SSH连接(看第3点)。</div><div>&nbsp;</div><div></div><div><strong><span style="COLOR: #ff0000">2、运行命令:</span></strong></div><div><span style="COLOR: #32cd32">$&nbsp;ssh-keygen&nbsp;-t&nbsp;rsa&nbsp;-C&nbsp;<a href="mailto:your_email@youremail.com" data_ue_src="mailto:your_email@youremail.com">your_email@youremail.com</a></span></div><div><img src="http://static.oschina.net/uploads/img/201309/17215847_8ucL.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/6758010.png"></div><div>执行完后在当前登录的用户下建立了文件夹:</div><div><img src="http://static.oschina.net/uploads/img/201309/17215848_lI5a.jpg" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/6800925.jpg"></div><div>&nbsp;</div><div></div><div><strong><span style="COLOR: #ff0000">3、将新生产的SSH密钥导入到Github账户中</span></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215848_hApL.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/7844369.png"></div><div>其中key是id_rsa.pub里面的文件内容:</div><div><img src="http://static.oschina.net/uploads/img/201309/17215849_ti8F.jpg" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/7946737.jpg"></div><div>&nbsp;</div><div><strong><span style="COLOR: #ff0000">4、接下来测试一下我们的git跟github是否能连接上</span></strong></div><div>①输入命令:ssh&nbsp;-T&nbsp;<a href="mailto:git@github.com" data_ue_src="mailto:git@github.com">git@github.com</a></div><div><img src="http://static.oschina.net/uploads/img/201309/17215849_NkoV.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/8057841.png"></div><div>②输入yes,提示</div><div><img src="http://static.oschina.net/uploads/img/201309/17215849_IeQ6.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/8128915.png"></div><div>③输入之前设置的密码:直接提示连接被关闭</div><div><img src="http://static.oschina.net/uploads/img/201309/17215850_qw2w.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/8198211.png"></div><div>④接着执行上面的命令,再一次输入密码,发现</div><div><img src="http://static.oschina.net/uploads/img/201309/17215850_k5kS.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/8268115.png"></div><div></div><div>以上设置为了让我们本地的Git跟Github连接上,接下来在本地配置git以便能够更好的进行签入和签出文件</div><div>&nbsp;</div><div>*****************************华丽的分割线*****************************</div><div>&nbsp;</div><div></div><div><strong><span style="FONT-SIZE: 32px">二、配置Git并新建项目</span></strong></div><div><strong><font color=#ff0000>1、使用命令</font>&nbsp;</strong> <div>git&nbsp;config&nbsp;--global&nbsp;user.name&nbsp;“Your&nbsp;Name”</div><div>git&nbsp;config&nbsp;--global&nbsp;user.email&nbsp;<a href="mailto:“you@example.com" data_ue_src="mailto:“you@example.com">“you@example.com</a>”</div><div>如图:</div></div><div><img src="http://static.oschina.net/uploads/img/201309/17215850_Gne8.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/15413085.png"></div><div>操作之后windows下当前登陆用户会多一个.gitconfig文件,</div><div><img src="http://static.oschina.net/uploads/img/201309/17215851_sdP0.jpg" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/7245326.jpg"></div><div>文件内的内容为:</div><div><img src="http://static.oschina.net/uploads/img/201309/17215852_70R6.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/15442320.png"></div><div>&nbsp;</div><div></div><div><strong><font color=#ff0000>2、新建一个仓库</font><img src="http://static.oschina.net/uploads/img/201309/17215852_Nfj9.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/8777209.png"></strong></div><div>&nbsp;</div><div></div><div><strong><font color=#ff0000>3、上传文件到GitHub中</font></strong></div><div><strong><span style="COLOR: #ff0000"><font color=#000000></font></span></strong>&nbsp;</div><div><strong><span style="COLOR: #ff0000"><font color=#0000ff>①在的盘下新建一个目录名为jstest,跟前面建立的仓库名一样</font></span></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215852_BeZT.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/8952788.png"></div><div>&nbsp;</div><div><strong><span style="COLOR: #ff0000"><font color=#0000ff>②跳转到d:\jstest目录下</font></span></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215853_QIDb.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/9104327.png"></div><div>或者直接在cmd命令下进行,不过要将git的bin目录添加到环境变量下面</div><div><img src="http://static.oschina.net/uploads/img/201309/17215853_r4ei.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/9327970.png"></div><div>需要将git的bin路径添加到环境变量</div><div>&nbsp;</div><div><strong><span style="COLOR: #ff0000"><font color=#0000ff>③新建一个README文件</font></span></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215853_Y4M8.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/9402991.png"></div><div></div><div><img src="http://static.oschina.net/uploads/img/201309/17215853_WGiX.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/9427202.png"></div><div>&nbsp;</div><div><strong><font color=#0000ff>④签入到本地文件库中</font></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215853_9tbs.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/9502878.png"></div><div>first&nbsp;commit&nbsp;是签入备注</div><div>&nbsp;</div><div><strong>整个步骤如下图:</strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215854_fWhk.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/10363349.png"></div><div><div>以上就是Git的一个简单使用</div></div><div>&nbsp;</div><div>*****************************华丽的分割线*****************************</div><div>&nbsp;</div><div><strong><font color=#ff0000>3、使用纯命令行创建项目并更新到GitHub上面(需要先在GitHub上创建仓库)</font></strong></div><div></div><div><img src="http://static.oschina.net/uploads/img/201309/17215854_t4w3.png" data_ue_src="C:\Users\ADMINI~1\AppData\Local\Temp\Wiz\baidu_1378569532501_files/10385626.png"></div><div>&nbsp;</div><div>*****************************华丽的分割线*****************************</div><div>&nbsp;</div><div><strong><span style="FONT-SIZE: 36px">三、Git不能支持中文的解决方法</span></strong></div><div><p>1、ls不能显示中文目录<br>解决办法:在git/etc/git-completion.bash中增加一行:<br>alias&nbsp;ls='ls&nbsp;--show-control-chars&nbsp;--color=auto'</p><p>2、git&nbsp;commit不能提交中文注释<br>解决办法:修改git/etc/inputrc中对应的行:<br>set&nbsp;output-meta&nbsp;on<br>set&nbsp;convert-meta&nbsp;off&nbsp;</p><p>3、git&nbsp;log无法显示中文注释<br>解决办法:在git/etc/profile中增加一行:<br>export&nbsp;LESSCHARSET=iso8859</p><p>来源于网络</p><p>*****************************华丽的分割线*****************************</p><p><span style="FONT-SIZE: 36px"><strong>四、Git一些命令详解(不断更新)</strong></span></p></div><div><strong><font color=#0000ff>1、从GitHub上克隆一个项目</font></strong> </div><div>克隆地址在每一个项目的首页都有,有很种形式的方式</div><div><div>1)git方式:</div><div><a href="mailto:git@github.com:liujiangbei/prototype.git">git@github.com:liujiangbei/prototype.git</a></div></div><div><div>2)https方式</div><div><a href="https://github.com/liujiangbei/prototype.git">https://github.com/liujiangbei/prototype.git</a></div><div>3)subversion方式</div><div><a href="https://github.com/liujiangbei/prototype">https://github.com/liujiangbei/prototype</a></div><div>这里使用git方式:$ git clone <a href="mailto:git@github.com:liujiangbei/jstest.git">git@github.com:liujiangbei/jstest.git</a></div></div><div>&nbsp;</div><div><strong><font color=#0000ff>2、使用git diff查看差异(这个缺点太多了,请直接右键单击项目然后选择Git gui)</font></strong></div><div><img src="http://static.oschina.net/uploads/img/201309/17215854_gx9A.png"></div><div>&nbsp;</div><div><strong><font color=#0000ff size=3>3、将一个本地已经存在的项目迁移到GitHub上的步骤:</font></strong></div><div>1)切换到工作目录:cd /d d:\github\jstest</div><div><img src="http://static.oschina.net/uploads/img/201309/17215855_6DiV.png"></div><div><div>1)新建一个文件夹,跟项目名称一模一样例如jstest</div><div><img src="http://static.oschina.net/uploads/img/201309/17215855_Rfwn.png"></div></div><div>2)将项目文件复制到jstest文件夹下</div><div><img src="http://static.oschina.net/uploads/img/201309/17215856_zBAR.png"></div><div>&nbsp;</div><div>4)进入项目根目录夹并初始化git:git init</div><div><img src="http://static.oschina.net/uploads/img/201309/17215857_qTXx.png"></div><div>5)添加所有文件:git add .</div><div><img src="http://static.oschina.net/uploads/img/201309/17215858_dker.png"></div><div>6)提交到本地:git commit -m "start project"</div><div><img src="http://static.oschina.net/uploads/img/201309/17215858_1qJp.png"></div><div>7)添加到远程服务器:git remote add origin <a href="mailto:git@github.com:liujiangbei/pytest.git">git@github.com:liujiangbei/pytest.git</a></div><div><img src="http://static.oschina.net/uploads/img/201309/17215859_RAnd.png"></div><div>8)更新到GitHub上:git push -u&nbsp;origin master</div><div><img src="http://static.oschina.net/uploads/img/201309/17215900_hnCg.png"></div><div>参考文章:</div><div><a href="https://github.com/peterluo/LearningPythonDiary/blob/master/1.How%20to%20use%20git.md">https://github.com/peterluo/LearningPythonDiary/blob/master/1.How%20to%20use%20git.md</a></div><div>&nbsp;</div><div><font color=#0000ff><strong>4、创建一个仓库</strong></font></div><div><strong><font color=#ff0000>Create a new repository on the command line</font></strong></div><div><code style="FONT-SIZE: 12px; BORDER-TOP: medium none; FONT-FAMILY: Consolas, 'Liberation Mono', Courier, monospace; BORDER-RIGHT: medium none; WHITE-SPACE: pre; BORDER-BOTTOM: medium none; PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; BORDER-LEFT: medium none; PADDING-RIGHT: 0px; BACKGROUND-COLOR: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px"><div>touch README.md</div><div>git init</div><div>git add README.md</div><div>git commit -m "first commit"</div><div>git remote add origin </div><div><span class=js-live-clone-url><a href="https://github.com/liujiangbei/jstest">https://github.com/liujiangbei/jstest</a></span></div><div><span class=js-live-clone-url></span>git push -u origin master</div><div>&nbsp;</div><div></code><strong><font color=#ff0000>Push an existing repository from the command line</font></strong></div><div><code style="FONT-SIZE: 12px; BORDER-TOP: medium none; FONT-FAMILY: Consolas, 'Liberation Mono', Courier, monospace; BORDER-RIGHT: medium none; WHITE-SPACE: pre; BORDER-BOTTOM: medium none; PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; BORDER-LEFT: medium none; PADDING-RIGHT: 0px; BACKGROUND-COLOR: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px">git remote add origin <span class=js-live-clone-url><a href="https://github.com/liujiangbei/jstest">https://github.com/liujiangbei/jstest</a></span></code></div><div><code style="FONT-SIZE: 12px; BORDER-TOP: medium none; FONT-FAMILY: Consolas, 'Liberation Mono', Courier, monospace; BORDER-RIGHT: medium none; WHITE-SPACE: pre; BORDER-BOTTOM: medium none; PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; BORDER-LEFT: medium none; PADDING-RIGHT: 0px; BACKGROUND-COLOR: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px"><span class=js-live-clone-url></span>git push -u origin master</code></div><!--EndFragment--></div><div>&nbsp;</div><div><strong><font color=#0000ff>5、命令条目</font></strong></div><div>1)添加文件:git add file</div><div>2)移除文件:git rm file</div><div>3)从远程获取最新版本并merge到本地:git pull origin master (当然要进入项目的活动目录里)</div><div><img src="http://static.oschina.net/uploads/img/201309/17215900_QHwu.png"></div><div><strong>4).更新项目(新加了文件):</strong></div><div><div>$cd ~/jstest</div><div>$git add .&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //这样可以自动判断新加了哪些文件,或者手动加入文件名字</div><div>$git commit -m "something for you!"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //提交到本地仓库</div><div>$git push -u origin master&nbsp;&nbsp;&nbsp; //不是新创建的,不用再add 到remote上了</div><div><strong>5).更新项目(没新加文件,只有删除或者修改文件):</strong></div><div>$cd ~/jstest</div><div>$git commit -a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //记录删除或修改了哪些文件</div><div>$git push origin master&nbsp; //提交到github</div><div><strong>6).忽略一些文件,比如*.o等:</strong></div><div>$cd ~/jstest</div><div>$vim .gitignore&nbsp;&nbsp;&nbsp;&nbsp; //把文件类型加入到.gitignore中,保存</div><div>然后就可以git add . 能自动过滤这种文件</div><div><strong>7).clone代码到本地:</strong></div><div>$git clone <a href="mailto:git@github.com:liujb/jstest.git">git@github.com:<font color=#000000>liujb</font>/jstest.git</a></div><div>假如本地已经存在了代码,而仓库里有更新,把更改的合并到本地的项目:</div><div>$git fetch origin&nbsp;&nbsp;&nbsp; //获取远程更新</div><div>$git merge origin/master //把更新的内容合并到本地分支</div><div><strong>8).撤销</strong></div><div>$git reset</div></div><div>&nbsp;</div><div>*****************************华丽的分割线*****************************</div><div><font color=#ff0000><strong>有用的连接:</strong></font></div><div>&nbsp;</div><div><div>更新自己fork的项目</div><div><a href="http://www.shizuwu.cn/post/669.html">http://www.shizuwu.cn/post/669.html</a></div><div><a href="http://my.oschina.net/luffyke/blog/70336">http://my.oschina.net/luffyke/blog/70336</a></div><div>&nbsp;</div><div>git中pull和fetch的区别</div></div><div><a href="http://blog.csdn.net/hudashi/article/details/7664457">http://blog.csdn.net/hudashi/article/details/7664457</a></div><div>&nbsp;</div><div><div></div><div><div>*****************************华丽的分割线*****************************</div><div><strong><font color=#ff0000>常见错误</font></strong></div><div>1.$ git remote add origin git@github.com:WadeLeng/hello-world.git</div><div>错误提示:fatal: remote origin already exists.</div><div>解决办法:$ git remote rm origin</div><div>然后在执行:$ git remote add origin git@github.com:WadeLeng/hello-world.git 就不会报错误了</div><div>2. $ git push origin master</div><div>错误提示:error:failed to push som refs to</div><div>解决办法:$ git pull origin master //先把远程服务器github上面的文件拉先来,再push 上去。</div><div><font color=#ff0000></font>&nbsp;</div></div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><br /><br /><div><a title="来自为知笔记(Wiz)" href="http://www.wiz.cn/i/6e010c58">来自为知笔记(Wiz)</a></div><br /><br /><div><h3>附件列表</h3><ul></ul></div><p>&nbsp;</p>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!