Git Push(向遠程Github提交本地代碼)

來源:互聯網
上載者:User

Git Push(向遠程Github提交本地代碼)
$ git push git@github.com:hepusheng/linux-2.6.11-study-note.git master // 把本地倉庫提交到遠程倉庫的master分支中

$ git remote add origin git@github.com:hepusheng/linux-2.6.11-study-note.git
$ git push -u origin master

這兩個操作是等價的,第二個操作的第一行的意思是添加一個標記,讓origin指向git@github.com:hepusheng/linux-2.6.11-study-note.git,也就是說你操作origin的時候,實際上就是在操作git@github.com:hepusheng/linux-2.6.11-study-note.git。
    預設情況下這條語句等價於提交本地的master倉庫到遠程倉庫,並作為遠端master分支。

在向遠程提交代碼時出現了錯誤:
1.error:failed to push some refs to ...
當要push代碼到git時,出現提示:
error:failed to push some refs to ...
Dealing with “non-fast-forward” errors
From time to time you may encounter this error while pushing:
$ git push origin master 
To ../remote/ 
 ! [rejected]        master -> master (non-fast forward) 
error: failed to push some refs to '../remote/' 
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.
 
問題(Non-fast-forward)的出現原因在於:git倉庫中已經有一部分代碼,所以它不允許你直接把你的代碼覆蓋上去,特別對於在Github上建立版本倉庫時,因為它為了增加其它人對使用者提交的代碼的可讀性時,預設是要求建立一個README.md檔案,和系統會產生.gitignore 檔案。所以在push化碼到遠程倉庫時,因為我們本地倉庫中沒有類似於README.md或.gitignore 等檔案,這樣就是提示(Non-fast-forward). 有2個方式可以解決這個問題:
 
強推,即利用強覆蓋方式用你本地的代碼替代git倉庫內的內容,這種方式不建議使用。
git push -f
 
2. 先把git的東西fetch到你本地然後merge後再push
$ git fetch
$ git merge
這2句命令等價於
$ git pull 
可是,這時候又出現了如下的問題:
上面出現的 [branch "master"]是需要明確(.git/config)如下的內容
[branch "master"]
    remote = origin
    merge = refs/heads/master
這等於告訴git2件事:
1,當你處於master branch, 預設的remote就是origin。
2,當你在master branch上使用git pull時,沒有指定remote和branch,那麼git就會採用預設的remote(也就是origin)來merge在master branch上所有的改變
如果不想或者不會編輯config檔案的話,可以在bush上輸入如下命令列:
$ git config branch.master.remote origin 
$ git config branch.master.merge refs/heads/master 
之後再重新git pull下。最後git push你的代碼,到此步順利完成時,則可以在Github上看到你建立的倉庫以及你提交到倉庫中檔案了,OK。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.