github入門及日常使用操作命令(附兩個錯誤及解決辦法)

來源:互聯網
上載者:User

What's git & git·hub?

開發環境:Linux,git工具


1.  註冊個帳號先。

2.產生個SSH再(官網很詳細,step-by-step)


3. 建立個Repository(用來放項目的倉庫)再。

先在github上建立並寫好相關名字,描述之後再在本地建立與之發生“聯絡”的檔案夾。

$cd ~$mkdir github_JFF-RTOS    //本地目錄名與repository的名字不一定相同,相關即可$cd ~/github_JFF-RTOS      //進入github_JFF-RTOS目錄

複製所要託管的代碼到此處:

如果你選中了第3步的4,則你需要先執行

$git pull origin master

From github.com:mintist/JFF-RTOS * branch            master     -> FETCH_HEADMerge made by the 'recursive' strategy. .gitignore |   12 ++++++++++++ README.md  |    4 ++++ 2 files changed, 16 insertions(+) create mode 100644 .gitignore create mode 100644 README.md

注意:那就是github自動幫你產生的兩個檔案,先拉(pull)下來先~~~

否則~~~也沒事,往後走先~~~

$git init                             //初始化

Initialized empty Git repository in /home/mintist/github_JFF-RTOS/.git/

$git add .                            //把所有檔案加入到索引(不想把所有檔案加入,可以用gitignore或add 具體檔案,見下文)$git commit -m "好吧,這還算不上一個RTOS~~~"       //提交到本地倉庫,然後會填寫更新日誌

 4 files changed, 259 insertions(+) create mode 100644 Readme create mode 100644 app.c create mode 100644 core.c create mode 100644 core.h

$git remote add origin git@github.com:mintist/JFF-RTOS.git        //增加到remote
$git push origin master    //推(push)到github上

if(you
git pull origin master as I say ,you will get )

Counting objects: 9, done.
Delta compression using up to 4 threads.Compressing objects: 100% (8/8), done.Writing objects: 100% (8/8), 2.96 KiB, done.Total 8 (delta 1), reused 0 (delta 0)To git@github.com:mintist/JFF-RTOS.git   c13a550..109de37  master -> master

else

To git@github.com:mintist/JFF-RTOS.git
 ! [rejected]        master -> master (non-fast-forward)error: failed to push some refs to 'git@github.com:mintist/JFF-RTOS.git'To prevent you from losing history, non-fast-forward updates were rejectedMerge the remote changes (e.g. 'git pull') before pushing again.  See the'Note about fast-forwards' section of 'git push --help' for details.

如果遇到錯誤,再一下就好了~~

還有平常的一些更新啥的請看下文,親測可用~~~

總結一下ubuntu下github常用的命令,設定部分跳過,假設repository的名字叫hello-world:

 

1.建立一個新的repository:

先在github上建立並寫好相關名字,描述。

$cd ~/hello-world        //到hello-world目錄,本地目錄名與repository的名字不一定相同

$git init                     //初始化

$git add .                   //把所有檔案加入到索引(不想把所有檔案加入,可以用gitignore或add 具體檔案,見下文)

$git commit               //提交到本地倉庫,然後會填寫更新日誌( -m “更新日誌”也可,如$git commit -m “my first vesion of ...”)

$git remote add origin git@github.com:WadeLeng/hello-world.git        //增加到remote

$git push origin master    //push到github上

2.更新項目(新加了檔案):

$cd ~/hello-world

$git add .                  //這樣可以自動判斷新加了哪些檔案,或者手動加入檔案名稱字

$git commit              //提交到本地倉庫,不加參數會提示,注意:^=Ctrl,按照提示來就好了~~~

$git push origin master    //不是新建立的,不用再add 到remote上了

3.更新項目(沒新加檔案,只有刪除或者修改檔案):

$cd ~/hello-world

$git commit -a          //記錄刪除或修改了哪些檔案

$git push origin master  //提交到github

4.忽略一些檔案,比如*.o等:

$cd ~/hello-world

$vim .gitignore     //把檔案類型加入到.gitignore中,儲存

然後就可以git add . 能自動過濾這種檔案

5.clone代碼到本地:

$git clone git@github.com:WadeLeng/hello-world.git

假如本地已經存在了代碼,而倉庫裡有更新,把更改的合并到本地的項目:

$git fetch origin    //擷取遠程更新

$git merge origin/master //把更新的內容合并到本地分支

6.撤銷

$git reset

7.刪除

$git rm  * // 不是用rm

//------------------------------常見錯誤-----------------------------------

1.$ git remote add origin git@github.com:WadeLeng/hello-world.git

 錯誤提示:fatal: remote origin already exists.

 解決辦法:$ git remote rm origin

 然後在執行:$ git remote add origin git@github.com:WadeLeng/hello-world.git 就不會報錯誤了

 2. $ git push origin master

 錯誤提示:error:failed to push som refs to

 解決辦法:$ git pull origin master //先把遠程伺服器github上面的檔案拉先來,再push 上去。

//------------------------------------------------------------------------------

一個中文git手冊:http://progit.org/book/zh/

http://hi.baidu.com/wade_hit/item/848869db05e53af4cb0c391b

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.