Sublime text2、Git、Github三者協同


Sublime text2、Git、Github三者協同

一、Git安裝

大家可以到Git的官網上去下載相應環境的Git安裝文件。

我的是win7,安裝到D:\Program Files\Git

二、Git配置及生成密鑰

建立一個工作目錄(我的G:\Workspace\git\Arrow)。

開始菜單->cmd命令進入命令行->進入到工作目錄,運行以下命令,設置Git提交代碼時你自己的用戶信息。

1 git config --global user.name "username"
2 git config --global user.email "[email protected]"

運行後可以使用下面命令查看配置:

1 git config -l

在Sublime Text中使用的時候還需要設置 push.default參數。使用命令行窗口的時候沒有問題,在Sublime Text中用push命令的時候就提示需要設置這個參數。

push.default參數主要是設置在執行push命令是的策略,主要的選項有以下幾個:
nothing : Do not push anything
matching : Push all matching branches (default)
tracking : Push the current branch to whatever it is tracking
current : Push the current branch
這裏我們手動設置成默認值:

1 git config --global push.default matching

到Git安裝目錄下,找到”Git Bash.vbs”,運行之,並執行以下命令:

1 $ ssh-keygen -t rsa

程序會提示您輸入密鑰的文件名,比如輸入id,按回車即可。
然後會要求你輸入一個密碼,將來在使用密鑰的時候需要提供這個密碼。可以輸入,也可以不輸入直接回車(無論輸入還是不輸入,都會要求你確認一次)。確認完畢後,程序將生成一對密鑰存放在以下文件夾:
C:\Users\Administrator[這裏替換成你的用戶名]\.ssh

密鑰分成兩個文件,一個私鑰(id_rsa)、一個公鑰(id_rsa.pub)。私鑰保存在您的電腦上,公鑰交項目負責人添加到服務器上。用戶必須擁有與服務器公鑰所配對的私鑰,才能訪問服務器上的代碼庫。

三、連接到Github

訪問Github主頁,註冊一個賬號並登錄。

在右上角找到Settings->SSH keys-> Add SSH key ->拷貝在公鑰(id_rsa.pub)文件中的所有的文本->完成了對GitHub上SSH Key公鑰的添加。

這樣就可以使用git去連接github上的repository了。

四、Sublime text2中安裝Git插件

常規插件方式安裝,不再累述。

安裝完Git插件後,在Sublime text2菜單->Preferences->Package settings -> Git -> Setting-Default中修改
“git_command”: false 爲”git_command”: “D:/Program Files/Git/bin/git.exe”;這個目錄依據你的安裝目錄。

五、Sublime text2中使用Git插件

1.Sublime text2菜單欄 -> Project -> Add Folder to Project增加工作目錄到側邊欄。

2.使用“Ctrl+Shift+p”打開命令窗口,輸入“Git:init”來初始化git化境

3.輸入“Git:add”來添加新增和修改的文件

4.輸入“Git:commit”來提交,Sublime Text會自動跳出一個文本文件,你可以在文件的最上方輸入這次更改的comments,當關閉文件的同時,commit操作自動觸發。其實就是把comments當做 git commit -m的參數。

5.Github上新建一個與本地工作目錄同名的倉庫,切換到工作目錄下,運行以下命令:
git remote add origin [email protected]:benjamin-zuo/Arrow.git
其中[email protected]:benjamin-zuo/Arrow.git爲SSH clone鏈接。

6.工作目錄下運行git pull origin master,更新文件

7.工作目錄下運行git push orgin master,提交文件。

8.回到Github,刷新查看提交結果。


之後可以在Sublime text2命令窗口中直接輸入git:pull,git:push,git:diff等來進行相關操作。
//以下是原創
ps:注意,如果這裏如果git push(pull) 用的是ssh且設置私鑰,會報錯,具體解決請看一下
//以上是原創

六、常見問題

1.執行下面語句報錯
git remote add origin [email protected]:defnngj/hello-world.git
錯誤提示:fatal: remote origin already exists.

解決辦法:
先git remote rm origin
然後在執行:
$ git remote add origin [email protected]:defnngj/hello-world.git

2.執行下面語句報錯
git push origin master
錯誤提示:error:failed to push som refs to…….
解決辦法:
$ git pull origin master //先把遠程服務器github上面的文件拉先來,再push 上去。

3.git 給遠程庫 添加多個url地址
增加第一個地址 git remote add origin url1
增加第二個地址 git remote set-url –add origin url2
增加第三個地址 git remote set-url –add origin url3


注意:使用git push origin master時,你可以push到origin的多個url地址,
但是使用 git pull時,只能拉取origin裏的一個url地址,這個url默認爲 你添加的到origin的第一個地址。可以使用git config -e命令到Git的config文件中更改默認url。
//以下是原創
4.git push(pull) ssh 報錯
錯誤提示
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

解決辦法
  1. 打開git bash
  2. 輸入eval `ssh-agent -s`
  3. 輸入ssh-add 私鑰地址
注意:該方法每次登錄git bash時候都要重新設置,自動化腳本(放在C:\users\your-user-name\下):         
但是,sublime text 上還是有同樣的問題,唯有在git bash 上進行更新和提交
5.github返回“Empty Reply From Server”
     錯誤提示:fatal: unable to access ‘https://github.com/xxxxx’: Empty reply from server
    .解決:用ssh的方式取代https(http://codefunny.github.io/blog/2014/11/24/test/
七、一些資料(文檔)

Generating SSH keys

另一個git插件:sublimegit
https://sublimegit.net/
//以上是原創

轉載聲明:

本文標題:Sublime text2、Git、Github三者協同

本文鏈接:http://www.zuojj.com/archives/1051.html,轉載請註明轉自Benjamin-專注前端開發和用戶體驗



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章