fork, clone, add, commit, fetch, rebase, push流程測試


1. 假定原做者的名稱叫gittest, 我叫uniquejava. java

原做者新建了一個項目叫nocrazy。 因而路徑爲gittest/nocrazy
作了兩次提交
提交一:
a.txt
this is a

提交二:b.txt
this is b. git


2. 我cyper某天fork了這個項目,因而有origin指向uniquejava/nocrazy, 而且新增一個upstream指向原做者gittest/nocrazy

$ git clone https://git.oschina.net/uniquejava/nocrazy.git
$ cd nocrazy
➜  nocrazy git:(master) $ git remote add upstream https://git.oschina.net/gittest/nocrazy.git
➜  nocrazy git:(master) $ git remote -v
origin https://git.oschina.net/uniquejava/nocrazy.git (fetch)
origin https://git.oschina.net/uniquejava/nocrazy.git (push)
upstream https://git.oschina.net/gittest/nocrazy.git (fetch)
upstream https://git.oschina.net/gittest/nocrazy.git (push)
➜  nocrazy git:(master) $


3. gittest正常提交了一個新版本 
c.txt
this is c

git log --oneline
e63667b this is c
11f5850 this is b
1cbdb56 this is a fetch

4. Cyper想把fork同步到和gittest如出一轍,怎麼辦?
辦法一(思考未試驗):把本身的fork刪除從新fork? 而後呢在本地git pull origin master

辦法二:
git fetch upstream
git checkout master
git merge upstream/master

方法二須要push才能到origin麼。(yes)


➜  nocrazy git:(master) $ ll
total 16
drwxr-xr-x  13 cyper  staff   442B Aug 26 05:11 .git/
-rw-r--r--   1 cyper  staff    10B Aug 26 05:07 a.txt
-rw-r--r--   1 cyper  staff    10B Aug 26 05:07 b.txt
➜  nocrazy git:(master) $ git fetch upstream
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://git.oschina.net/gittest/nocrazy
 * [new branch]      master     -> upstream/master
➜  nocrazy git:(master) $ git diff ..upstream/master
diff --git a/c.txt b/c.txt
new file mode 100644
index 0000000..5da75cf
--- /dev/null
+++ b/c.txt
@@ -0,0 +1 @@
+this is c

使用以上git diff命令能夠看到author新增了一個c.txt文件。
➜  nocrazy git:(master) $ git merge upstream/master
Updating 11f5850..e63667b
Fast-forward
 c.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 c.txt
➜  nocrazy git:(master) $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean
➜  nocrazy git:(master) $ git fetch upstream
➜  nocrazy git:(master) $ git merge upstream/master
Already up-to-date.
➜  nocrazy git:(master) $ git diff ..origin/master
diff --git a/c.txt b/c.txt
deleted file mode 100644
index 5da75cf..0000000
--- a/c.txt
+++ /dev/null
@@ -1 +0,0 @@
-this is c

經過以上能夠看到git merge只是把upstream上的代碼拉到(本地工做目錄+本地倉庫),須要咱們push origin
➜  nocrazy git:(master) $ git push origin master
Username for 'https://git.oschina.net': uniquejava
Password for 'https://uniquejava@git.oschina.net':
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://git.oschina.net/uniquejava/nocrazy.git
   11f5850..e63667b  master -> master
➜  nocrazy git:(master) $ ll
total 24
drwxr-xr-x  14 cyper  staff   476B Aug 26 05:21 .git/
-rw-r--r--   1 cyper  staff    10B Aug 26 05:07 a.txt
-rw-r--r--   1 cyper  staff    10B Aug 26 05:07 b.txt
-rw-r--r--   1 cyper  staff    10B Aug 26 05:14 c.txt

到這裏, 步驟4驗證結束。。。成功的將fork上的代碼更新到最新版。



5. 假設上一步成功執行, 效果應該和我從新fork一遍最新的項目是同樣的。 this


6. 重複3:即gitetst提交了一個新版本
d.txt
this is d. spa

7. 在6以前,Cyper由於要修復defect #1, 已經開始基於c.txt作修改。就沒有來得及及時同步
cyper修改this is c變成this is c_cyper
而後正常的add, commit , (Cyper此時是否能夠push?)

➜  nocrazy git:(master) ✗ $ git commit -m "[nocrazy-1] fix c."
[master 77fd57f] [nocrazy-1] fix c.
 1 file changed, 1 insertion(+), 1 deletion(-)
此時已經有了分歧。gittest上有新的提交, cyper也有新的提交。

cyper想基於最新的代碼從新提交。
7-1 假定cyper沒有push
他想先更新fork,而後基於 author最新的代碼去作提交
git fetch upstream
git checkout master
git merge upstream/master 這一步是用git rebase upstream/master仍是用git merge upstream/master

=====

➜  nocrazy git:(master) $ git rebase origin/master(這步只是想驗證base到當前base的狀況,無效果)
Current branch master is up to date.
➜  nocrazy git:(master) $ git fetch upstream
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://git.oschina.net/gittest/nocrazy
   e63667b..380a8dd  master     -> upstream/master
➜  nocrazy git:(master) $ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: [nocrazy-1] fix c.
➜  nocrazy git:(master) $ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean
➜  nocrazy git:(master) $

以上這步作了什麼???原來git rebase upstream/master把upstream上的代碼拉到(本地工做目錄+本地倉庫),而且自動作了合併, 因此你的本了一會兒多出來兩個commit須要push, 所以有Your branch is ahead of 'origin/master' by 2 commits.
➜  nocrazy git:(master) $ git log --oneline
8530647 [nocrazy-1] fix c.
380a8dd this is d
e63667b this is c
11f5850 this is b
1cbdb56 this is a

如今只需作push origin。。。就好了!
➜  nocrazy git:(master) $ git push origin master
Username for 'https://git.oschina.net': uniquejava
Password for 'https://uniquejava@git.oschina.net':
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 566 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To https://git.oschina.net/uniquejava/nocrazy.git
   e63667b..8530647  master -> master

因此在發pull request 以前不要去作push . (若是在rebase以前就作了push,須要怎麼補救???)


到目前爲止,cyper的遠程倉庫中是最新的代碼,而原做者的代碼不新,由於他的代碼沒有包含你的修改

8. 發pull request
8-1, 假設gittest在你發pull request前,沒有提交任何代碼。。

過程很簡單。
接下來gittest review,他點擊某個代碼變更行的前面加了一個comment: 少了一個標點符號 。。

若是你此時使用git fetch upstream會沒有任何輸出
而後你
➜  nocrazy git:(master) $ git rebase upstream/master
Current branch master is up to date.

這是由於你當前沒有任何commit是須要rebase的, 由於全部commit都已經push到了origin. 除非。。


與時同時, gittest正常添加了一個新文件
e.txt
this is e.

你從新fetch, 
➜  nocrazy git:(master) $ git fetch upstream
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://git.oschina.net/gittest/nocrazy
   380a8dd..fd2e641  master     -> upstream/master
➜  nocrazy git:(master) $ git diff ..upstream/master
diff --git a/c.txt b/c.txt
index 3af12b3..5da75cf 100644
--- a/c.txt
+++ b/c.txt
@@ -1 +1 @@
-this is c_cyper
+this is c
diff --git a/e.txt b/e.txt
new file mode 100644
index 0000000..5641475
--- /dev/null
+++ b/e.txt
@@ -0,0 +1 @@

能夠看到,當前你的本地倉庫和upstream的區別是。。你改了c文件而upstream加了e文件。。

你從新rebase
➜  nocrazy git:(master) $ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: [nocrazy-1] fix c.
➜  nocrazy git:(master) $ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean
➜  nocrazy git:(master) $


這時和第一次有所不同, 

➜  nocrazy git:(master) $ git log --oneline
8c4c4b6 [nocrazy-1] fix c.
fd2e641 this is e
380a8dd this is d
e63667b this is c
11f5850 this is b
1cbdb56 this is a

查看一下有什麼不同
➜  nocrazy git:(master) $ git diff ..origin/master
diff --git a/e.txt b/e.txt
deleted file mode 100644
index 5641475..0000000
--- a/e.txt
+++ /dev/null
@@ -1 +0,0 @@
-this is e
是由於你本身的遠程倉庫沒有包含e文件。。。按照提示。 你須要作一次pull
可是pull會產生分支 。。
嘗試一下而後立馬撤銷(http://stackoverflow.com/questions/1223354/undo-git-pull-how-to-bring-repos-to-old-state)

➜  nocrazy git:(master) $ git pull
Merge made by the 'recursive' strategy.
➜  nocrazy git:(master) $ git log --oneline
9d7a324 Merge branch 'master' of https://git.oschina.net/uniquejava/nocrazy
8c4c4b6 [nocrazy-1] fix c.
fd2e641 this is e
8530647 [nocrazy-1] fix c.
380a8dd this is d
e63667b this is c
11f5850 this is b
1cbdb56 this is a
➜  nocrazy git:(master) $ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

可見, git pull生成的commit history多麼的混亂!(相對於當前的origin/master, 
一會兒多出了3條commit history)
9d7a324 Merge branch 'master' of https://git.oschina.net/uniquejava/nocrazy
8c4c4b6 [nocrazy-1] fix c.
fd2e641 this is e

趕忙撤銷並換成git rebase
➜  nocrazy git:(master) $ git show HEAD
commit 9d7a324e5e562475b9585ed029817fc98d09a1d4
Merge: 8c4c4b6 8530647
Author: cyper <345343747@qq.com>
Date:   Wed Aug 26 06:11:07 2015 +0800


    Merge branch 'master' of https://git.oschina.net/uniquejava/nocrazy
原來。。HEAD並非指向遠程倉庫的head..

➜  nocrazy git:(master) $ git show head
➜  nocrazy git:(master) $ git show HEAD
➜  nocrazy git:(master) $ git show master
➜  nocrazy git:(master) $ git show origin/master
。。。前三個信息都是指向本地倉庫的頭,是沒一個commit id.最後一個是我想要的頭,是我pull以前的commit id
因此有三種方法
git reset --hard HEAD^3
或git reset --hard origin/master
或者git reset --hard 8530647b .net

重作第7步。。
➜  nocrazy git:(master) $ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: [nocrazy-1] fix c.
➜  nocrazy git:(master) $ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean
➜  nocrazy git:(master) $

如今由於本地倉庫纔是最新的。。你須要force push到origin而不須要理pull操做。。。
➜  nocrazy git:(master) $ git push origin master --force
Username for 'https://git.oschina.net': uniquejava
Password for 'https://uniquejava@git.oschina.net':
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 569 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To https://git.oschina.net/uniquejava/nocrazy.git
 + 8530647...3e23afc master -> master (forced update)


git rebase產生了完美的commit history:
3e23afc [nocrazy-1] fix c.
fd2e641 this is e
380a8dd this is d
e63667b this is c
11f5850 this is b
1cbdb56 this is a

rem

相關文章
相關標籤/搜索