20130907.Git學習記錄

<div><strong>一、任何文件在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>二、先決條件:</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>1、使用SSH創建本地Git跟GitHub的鏈接</font></span></strong></div><div>SSH是什麼,我也不清楚,只知道不少鏈接相關的都叫SSH好比說XShell鏈接Linux主機的時候就是SSH,應該是一種鏈接協議吧,須要找一個谷歌。</div><div>&nbsp;</div><div><strong><span style="COLOR: #ff0000">一、查看本地是否存在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">二、運行命令:</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">三、將新生產的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">四、接下來測試一下咱們的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">2、配置Git並新建項目</span></strong></div><div><strong><font color=#ff0000>一、使用命令</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>二、新建一個倉庫</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>三、上傳文件到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>三、使用純命令行建立項目並更新到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">3、Git不能支持中文的解決方法</span></strong></div><div><p>一、ls不能顯示中文目錄<br>解決辦法:在git/etc/git-completion.bash中增長一行:<br>alias&nbsp;ls='ls&nbsp;--show-control-chars&nbsp;--color=auto'</p><p>二、git&nbsp;commit不能提交中文註釋<br>解決辦法:修改git/etc/inputrc中對應的行:<br>set&nbsp;output-meta&nbsp;on<br>set&nbsp;convert-meta&nbsp;off&nbsp;</p><p>三、git&nbsp;log沒法顯示中文註釋<br>解決辦法:在git/etc/profile中增長一行:<br>export&nbsp;LESSCHARSET=iso8859</p><p>來源於網絡</p><p>*****************************華麗的分割線*****************************</p><p><span style="FONT-SIZE: 36px"><strong>4、Git一些命令詳解(不斷更新)</strong></span></p></div><div><strong><font color=#0000ff>一、從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>二、使用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>三、將一個本地已經存在的項目遷移到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>四、建立一個倉庫</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>五、命令條目</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>html

相關文章
相關標籤/搜索