2013-07-10 74 views
2

關於我的問題在這裏:Git; code disappeared after mergeGit的日誌和現實不匹配

它再次發生,我不知道怎麼樣。問題是,在提交中更改了一行代碼,但舊的代碼存在於當前文件的repo版本中,這看起來很奇怪。有沒有人對這種情況如何發生以及我們如何避免這種情況有所瞭解?

所討論的行是create_marking方法

函數頭部的提交改變了線(如預期):

commit 925ec3c11006ccca37cf684443d0fad3e1781dca 
Author: xxxx 
Date: Tue Jun 4 14:55:52 2013 

    Comment 

diff --git a/lib/services/MarkingService.class.php b/lib/services/MarkingService.class.php 
index 66a33f7..acf830a 100644 
--- a/lib/services/MarkingService.class.php 
+++ b/lib/services/MarkingService.class.php 
@@ -34,18 +34,12 @@ class MarkingService \{ 
     return '[' . implode(',', self::getCoordinates($marking)) . ']'; 
    \} 
\} 

- public function create_marking($category, $timestamp, $field_id, $lat, $lon, $accuracy, $lat2 = null, $lon2 = null, $accuracy2 = null, $spread = null, $depth = null, $comment = null) \{ 
+ public function create_marking($category, $timestamp, $field_id, $coordinates, $spread = null, $depth = null, $comment = null) \{ 
     $this->validate_category($category); 
     $this->validate_timestamp($timestamp); 

下面兩個提交是在歷史上唯一提交,上述一前一後,而據我所知,他們沒有以任何方式改變了行的問題:

commit 2ab5fb14765caa269c027e8c57b11232b0441625 
Author: xxx 
Date: Thu Jul 4 13:58:26 2013 

    Comment 

diff --git a/lib/services/MarkingService.class.php b/lib/services/MarkingService.class.php 
index 66a33f7..c553876 100644 
--- a/lib/services/MarkingService.class.php 
+++ b/lib/services/MarkingService.class.php 
@@ -8,32 +8,6 @@ class MarkingService { 
     return $instance; 
    } 

- public static function getCoordinates(lmMarking $marking) 
- { 
-  $returnValue = array($marking->getLatitude() . ',' . $marking->getLongitude()); 
-   
-  if (0 < $marking->getLatitude2()) { 
-   $returnValue[] = $marking->getLatitude2() . ',' . $marking->getLongitude2(); 
-  } 
-   
-  return $returnValue; 
- } 
-  
- public static function getCoordinatesJavascript(lmMarking $marking) 
- { 
-  return '[' . implode(',', self::getCoordinates($marking)) . ']'; 
- } 
- 
    public function create_marking($category, $timestamp, $field_id, $lat, $lon, $accuracy, $lat2 = null, $lon2 = null, $accuracy2 = null, $spread = null, $depth = null, $comment = null) { 
     $this->validate_category($category); 
     $this->validate_timestamp($timestamp); 

commit d152befa9977b8fc13df9f3ea3f756217751cb0d 
Merge: 77b20e2 2ab5fb1 
Author: xxx 
Date: Thu Jul 4 14:43:26 2013 

    Comment 

diff --cc lib/services/MarkingService.class.php 
index acf830a,c553876..f5a8bc4 
--- a/lib/services/MarkingService.class.php 
+++ b/lib/services/MarkingService.class.php 
@@@ -131,34 -157,4 +105,34 @@@ class MarkingService 
       throw new InvalidArgumentException("Invalid depth: '$depth'"); 
      } 
     } 
+  
+ private function validateCoordinates($coordinates) 
+ { 
+  $validatorOptions = array(
+   'lat' => array(
+    'field' => 'latitude', 
+    'validator' => FILTER_VALIDATE_FLOAT 
+   ), 
+   'lng' => array(
+    'field' => 'longitude', 
+    'validator' => FILTER_VALIDATE_FLOAT 
+   ), 
+   'accuracy' => array(
+    'field' => 'accuracy', 
+    'validator' => FILTER_VALIDATE_INT 
+   ) 
+  ); 
+   
+  foreach ($coordinates as $coordinate) { 
+   foreach ($coordinate as $key => $value) { 
+    if (false === filter_var($coordinate[$key], $validatorOptions[$key]['validator'])) { 
+     throw new InvalidArgumentException("Invalid " . $validatorOptions[$key]['field'] . ": '" . $value . "'"); 
+    } 
+   } 
+  } 
+ } 
- } 
+ } 

,由於某種原因我無法捉摸,該文件中的行現在讀取

public function create_marking($category, $timestamp, $field_id, $lat, $lon, $accuracy, $lat2 = null, $lon2 = null, $accuracy2 = null, $spread = null, $depth = null, $comment = null) { 

我在做什麼錯?

編輯的git的狀態 輸出

$ git status 
# On branch master 
nothing to commit, working directory clean 

混帳的輸出記錄

$ git log lib/services/MarkingService.class.php 
commit d152befa9977b8fc13df9f3ea3f756217751cb0d 
Merge: 77b20e2 2ab5fb1 
Author: xxx 
Date: Thu Jul 4 14:43:26 2013 +0300 

    Changed the format of Markings block in Sync API method. 
    Send markings coordinates in the API in the new format 

commit 2ab5fb14765caa269c027e8c57b11232b0441625 
Author: xxx 
Date: Thu Jul 4 13:58:26 2013 +0300 

    Changed the format of Markings block in Sync API method. 
    Send markings coordinates in the API in the new format 

commit 925ec3c11006ccca37cf684443d0fad3e1781dca 
Author: xxx 
Date: Tue Jun 4 14:55:52 2013 +0300 

    Changed the makrings coordinates structure. 
    Allow more points in CreateMarking 

混帳登錄--oneline --graph

$ git log --oneline --graph lib/services/MarkingService.class.php 
* d152bef Changed the format of Markings block in Sync API method. Send markings coordinates in the API in the new format 
|\ 
| * 2ab5fb1 Changed the format of Markings block in Sync API method. Send markings coordinates in the API in the new format 
* | 925ec3c Changed the makrings coordinates structure. Allow more points in CreateMarking 
|/ 
* bacfb14 An intermediate commit. HOTFIX: Fix markings 
* 6479513 LETFARM-1863 DB and API support for markings 
+0

'git status'和'git log lib/services/MarkingService.class.php'的輸出是什麼? – Koraktor

+0

我也嘗試重置爲引入所需更改的提交,但該行仍舊是舊版本。 – Vonsild

+0

如果您看看提交2ab5fb14765caa269,則問題行是舊版本。當你更新你的分支時,你是在做'git merge'還是'git rebase'? – Schleis

回答

3

當你拉,做合併,承諾2ab5fb14765caa269c027e8c57b11232b0441625刪除了很大大量的代碼在你做出更改的地方附近,git錯誤地使用提交版本的行來解決合併問題。您可以在該補丁的補丁中看到該行。

Git不跟蹤行更改。它跟蹤給定點上文件的狀態。

Git不以這種方式思考或存儲其數據。相反,Git認爲其數據更像是一組迷你文件系統的一組快照。您每次提交 時,或將項目的狀態保存在Git中,它基本上會拍攝您所有文件在該時刻的外觀,並存儲對該快照的引用。爲了高效,如果 文件沒有更改,Git不會再次存儲該文件 - 只是將鏈接 保存到它已存儲的上一個相同文件。

http://git-scm.com/book/en/Getting-Started-Git-Basics

所以Git是看到了承諾,你在拉,因爲即使你修​​改了行,「解決」的通過選擇行合併的變化。

我以前見過這種情況,它通常是由於刪除/重新排序碰巧發生其他更改的大塊代碼所致。我建議避免這種情況是在更新本地分支時使用git pull --rebasegit rebase

http://git-scm.com/book/en/Git-Branching-Rebasing

這將您的本地提交移動到後任何已在遠程如果Git有申請,提交將要求你改變你的困難提交。這也保持了歷史線性,並且不會讓git解析在你身上不正確地合併。

+0

謝謝,我會嘗試的。但似乎很奇怪,因爲該行在該提交的差異中未顯示爲已更改。 – Vonsild

+0

它在提交中未被標記爲已更改的原因是git正在比較它的父提交和它自己之間的文件狀態。在那個提交行中沒有改變。因此,在查看修補程序時,您看不到更改。 – Schleis

+0

嗯 - 這基本上意味着,你不能相信輸出?因爲它顯然不能反映文件的實際變化 – Vonsild