Visual Studio Git本地Repos和GitHub遠程Repos互操做

近期準備將一個項目開源到GitHub中,N-Sharding,.Net版本的分庫分表數據訪問框架。中間遇到了點小問題,整理了一下。html

1. GitHub上Create New Reposgit

2. 代碼Check In到本地Git Reposgithub

3. Push到GitHub遠程Repos緩存

1、GitHub上Create New Repos:N-Sharding框架

2. 代碼Check In到本地Git Repos fetch

 

3. Push到GitHub遠程Reposspa

推送提示一下錯誤:3d

將分支推送到遠程存儲庫時遇到錯誤: rejected Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes  before pushing again.
Error: failed to push some refs to 'https://github.com/*****/N-Sharding.git'
Error: hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

看中間第三句話:Updates were rejected because the tip of your current branch is behindcode

說明本地分支是舊的,遠程Master分支要新。爲何呢?htm

忽然想起來,在GitHub上Create New Repos時,增長了Readme.md。本地沒有這個文件。

一頓百度後:http://www.javashuo.com/article/p-yzashhjv-n.html

解決方案:

1. 打開Git Bash

2. Git拉取最新的代碼到本地,必定要rebase,強制同步更新本地分支

git pull --rebase origin master

關於Git rebase,能夠參考:https://git-scm.com/docs/git-rebase

3. 推送Push到Master主分支

git push -u origin master

Push 成功,代碼成功提交到master。

Tips:關於git merge 與 git rebase

1. git merge 和 git rebase 都是將遠程分支與本地分支合併的一種方法,git merge 會生成一個新的節點,例如A和B都位於同一個HEAD,A提交了2個commit C1和C2,B 提交了2個commit C3和C4,git merge的結果是在C3和C4以後合併生成C5,這樣提交歷史比較清晰,但多了一個C5
2. 假設A已經將C1和C2 push到了遠程分支,那麼B 使用git rebase則會將C3和C4緩存到.git/rebase中,恢復到以前的狀態,更新C1和C2,而後再將C3和C4做爲補丁應用到C2的狀態上。結果以下:
原始狀態->C1->C2->C3'->C4',C3'和C4'爲git 根據C3和C4生成的補丁,log是一條直線,並且沒有多餘的C5,可是平行信息丟失。

關於git pull 與 git pull --rebase

1. git pull = git fetch + git merge
2. git pull --rebase = git fetch + git rebase

 

以上,分享給你們。

 

周國慶

2019/3/17

相關文章
相關標籤/搜索