1. 程式人生 > 其它 >把本地倉庫資料push到github遠端倉庫

把本地倉庫資料push到github遠端倉庫

1、首先為了防止每次都需要輸入GitHub賬號密碼進行資料push,我們採用配置SSH的方式

先在本地初始化一個倉庫

2、如果你沒有 Github 可以在官網https://github.com/註冊。

由於你的本地 Git 倉庫和 GitHub 倉庫之間的傳輸是通過SSH加密的,所以我們需要配置驗證資訊:

使用以下命令生成 SSH Key:(後面的[email protected]改為你在 Github 上註冊的郵箱,之後會要求確認路徑和輸入密碼,我們這使用預設的一路回車就行。)

ssh-keygen -t rsa -C "[email protected]"

成功的話會在C:\Users\(你登陸的使用者名稱)\.ssh下會有一個id_rsa.pub的檔案

.pub結尾那個為公鑰,以文字方式開啟它,複製全部內容,貼上到你的GitHub上面

左邊選擇SSH and GPG keys,然後點選New SSH key按鈕,title 設定標題,可以隨便填,貼上在你電腦上生成的 key。

新增成功之後顯示:

驗證是否連線成功

$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to 
continue connecting (yes/no/[fingerprint])? yes # 輸入 yes Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts. Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access. # 成功資訊

3、在github上建立一個倉庫

4、建立成功後,顯示如下資訊

上面這張圖要注意,SSH後面對應一個連結,HTTPS後面也對應一個連結。我們要使用SSH後面對應那個連結(也就是用[email protected]:tianqixin/runoob-git-test.git)

使用HTTPS那個你在使用github遠端倉庫的時候會彈出來一個框,讓你登入GitHub

SSH也可以在這

5、把工作區的資原始檔提交到暫存區

這裡我們的工作區就是剛才你使用git init命令初始化之後那個目錄

你先弄幾個檔案放在這個目錄下

然後我們使用

git add ./*   #把當前目錄下全部檔案都放到暫存區
git add (具體檔名) #把某檔案存放到暫存區中

之後需要提交commit,把暫存區檔案放到版本庫中

git commit -m "新增 README.md 檔案"        # 提交併備註資訊

遠端連線到GitHub中的某庫

git remote add origin [email protected]:tianqixin/runoob-git-test.git

這個origin是我們給這個遠端連線的庫起的一個名字,你使用git remote命令可以檢視遠端連線的所有庫的在本地的名字,加上-v選項會顯示更多資訊

$ git remote
origin
$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)

然後把本地倉庫的master提交到遠端倉庫(本地化名為origin)

git push -u origin master

6、git push出現的問題

有時候你用完git push之後會出現下面情況

$ git push -u origin master
Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'origin'.

剛開始我以為沒出現紅字就不算錯誤,但是我push之後就和沒有操作一樣,遠端倉庫沒變化,我就一直找不到我操作哪裡出錯,把輸出資訊一搜才知道這就是報錯了。。。

解決辦法:

1、移除
git remote rm origin
2、再次連線
git remote add origin ‘倉庫地址’

7、出現錯誤failed to push some refs to ‘’

$ git push -u origin master
To github.com:qweqwe/Test.git
 ! [rejected]          master -> master (fetch first)
error: failed to push some refs to 'github.com:qweqwe/Test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決:

本質原因:因為遠端庫與本地庫不一致造成的,那麼我們把遠端庫同步到本地庫就可以了 一般問題:發生在本地庫提交到遠端庫的時候。
一般原因:當我們在github版本庫中發現一個問題後,你在github上對它進行了線上的修改;或者你直接在github上的某個庫中新增readme檔案或者其他什麼檔案,但是沒有對本地庫進行同步。這個時候當你再次有commit想要從本地庫提交到遠端的github庫中時就會出現push失敗的問題。 我的問題:本地新建一個資料夾,開啟git bash,輸出git init初始化一個本地庫,然後git pull了遠端庫,修改後提交。 我的原因:遠端庫pull下來後,就是一個完整的庫了,而我當初新建了個庫,而且用這個本地庫(與puul下來的不是一個),和遠端庫同步。 我的解決方法:進入pull下來的庫的資料夾,開啟git bash,並push,就可以了。 解決方案:這個問題是因為遠端庫與本地庫不一致造成的,那麼我們把遠端庫同步到本地庫就可以了。
使用指令:git pull--rebase origin master
這條指令的意思是把遠端庫中的更新合併到本地庫中,-rebase的作用是取消掉本地庫中剛剛的commit,並把他們接到更新後的版本庫之中。

git pull –rebase origin master意為先取消commit記錄,並且把它們臨時 儲存為補丁(patch)(這些補丁放到”.git/rebase”目錄中),之後同步遠端庫到本地,最後合併補丁到本地庫之中。

接下來就可以把本地庫push到遠端庫當中了。

8、git push --rebase作用

git pull = git fetch + git merge FETCH_HEAD 
git pull --rebase =  git fetch + git rebase FETCH_HEAD

那麼來看看git rebase, 在master執行git rebase tmp,操作之後的分支如下:

二者對比可知,rebase沒有產生新的節點,使用rebase的git演進路線(提交樹)是一直向前的,這樣在版本回退時也很容易,用merge的git路線是跳躍的,如果版本回退你也找不到自己想要的版本,如果在merge時出現了衝突那就麻煩了,當前merge就不能繼續進行下去,需要手動修改衝突內容後,add,commit, push. 而rebase 操作的話,會中斷rebase,同時會提示去解決衝突。解決衝突後, 再執行 git rebase –continue 繼續操作,再git push.

8、git的一些操作命令

1)git show branch:file

解釋:

Wherebranchcan be any ref (branch, tag, HEAD, ...) andfileis the full path of the file. To export it you could use

2)git show branch:file > exported_file

解釋:

A simple, newbie friendly way for looking into a file:git gui browser <branch>which lets you explore the contents of any file.

It's also there in the File menu ofgit gui. Most other -more advanced- GUI wrappers (Qgit, Egit, etc..) offer browsing/opening files as well.

9、git add permission denied

git add .

當執行這步操作的時候,發現有permission denied的提示,這裡報錯的可能性

1)文字作為只讀性文字,需要修改資料夾屬性

2)就是ide 編輯器佔用,沒有儲存等可能相關的操作都有可能致使我們提交不上我們的程式碼

這時候就需要關閉ide 當前所使用的編輯器,然後重開就可以,再繼續往下做git的操作(我的是這個原因)

10、git push -u origin master 中-u的作用

加了引數-u後,以後即可直接用git push 代替git push origin master

11、git push <遠端主機名> <本地分支名> <遠端分支名>

原文:https://www.cnblogs.com/qianqiannian/p/6008140.html

git push的一般形式為 git push <遠端主機名> <本地分支名> <遠端分支名> ,例如 git push origin master:refs/for/master ,即是將本地的master分支推送到遠端主機origin上的對應master分支, origin 是遠端主機名,第一個master是本地分支名,第二個master是遠端分支名。

11.1 git push origin master

如果遠端分支被省略,如上則表示將本地分支推送到與之存在追蹤關係的遠端分支(通常兩者同名),如果該遠端分支不存在,則會被新建

11.2 git push origin :refs/for/master

  如果省略本地分支名,則表示刪除指定的遠端分支,因為這等同於推送一個空的本地分支到遠端分支,等同於 git push origin --delete master

11.3 git push origin

   如果當前分支與遠端分支存在追蹤關係,則本地分支和遠端分支都可以省略,將當前分支推送到origin主機的對應分支

11.4 git push

  如果當前分支只有一個遠端分支,那麼主機名都可以省略,形如 git push,可以使用git branch -r ,檢視遠端的分支名

11.5 git push 的其他命令

  這幾個常見的用法已足以滿足我們日常開發的使用了,還有幾個擴充套件的用法,如下:

    (1) git push -u origin master 如果當前分支與多個主機存在追蹤關係,則可以使用 -u 引數指定一個預設主機,這樣後面就可以不加任何引數使用git push,

      不帶任何引數的git push,預設只推送當前分支,這叫做simple方式,還有一種matching方式,會推送所有有對應的遠端分支的本地分支, Git 2.0之前預設使用matching,現在改為simple方式

      如果想更改設定,可以使用git config命令。git config --global push.default matching OR git config --global push.default simple;可以使用git config -l 檢視配置

    (2) git push --all origin 當遇到這種情況就是不管是否存在對應的遠端分支,將本地的所有分支都推送到遠端主機,這時需要 -all 選項

    (3) git push --force origin git push的時候需要本地先git pull更新到跟伺服器版本一致,如果本地版本庫比遠端伺服器上的低,那麼一般會提示你git pull更新,如果一定要提交,那麼可以使用這個命令。

    (4) git push origin --tags //git push 的時候不會推送分支,如果一定要推送標籤的話那麼可以使用這個命令

11.6 關於 refs/for

  // refs/for 的意義在於我們提交程式碼到伺服器之後是需要經過code review 之後才能進行merge的,而refs/heads 不需要

參考連結:https://www.cnblogs.com/Rainingday/p/12364690.html

     https://www.runoob.com/git/git-remote-repo.html

     https://blog.csdn.net/yao_hou/article/details/108178717

     https://www.cnblogs.com/yiduobaozhiblog1/p/9125465.html

     https://www.cnblogs.com/qianqiannian/p/6008140.html

     http://stackoverflow.com/questions/7856416/view-a-file-in-a-different-git-branch-without-changing-branches