2012-11-29 30 views
5

假設我有2個分支,masterother合併後Git主分支根本沒有新文件

我去other分支,添加2個文件,提交併推送。

現在我進入master分支,將文件添加到不同的目錄並提交它們。然後我合併other

問題是我在other中添加的文件沒有顯示出來。 Git說它是最新的,但它不是!文件丟失。

如何強制master添加文件other或以某種方式手動添加它們?

編輯卡爾:

我做了以下的我所知,雖然沒有顯示出來的變化是幾個星期了。我剛剛意識到他們今天不在場。

$ git branch 
*other 
master 
$ git add . 
$ git commit -m 'cool new features' 
$ git push origin other 
$ git checkout master 
$ git merge other 
$ git add . 
$ git commit -m 'merged cool new features from other' 
$ git push origin master 

我在Github上,並且文件不在那裏。其他文件已提交併顯示,但兩個文件夾沒有匹配的內容。這些文件存在於other中,但不在master中。爲了澄清,這些文件並不是新的。但我認爲如果不存在,合併將至少將文件複製到master

+0

嗨,你確定你的文件已被提交?合併期間,Git沒有理由將它們隱藏起來。你能發佈你所做的所有命令嗎? (這將幫助找出問題) –

+0

git add。,git commit -m'something',git push,git checkout,git merge。可能現在和現在的每一個訂單都有 – AJcodez

+0

如果你在'other'分支中做'git status',會發生什麼? –

回答

3

像這樣:

[email protected]:~/stackoverflow$ git init . 
Initialized empty Git repository in /home/karl/stackoverflow/.git/ 
[email protected]:~/stackoverflow$ touch common_file_a 
[email protected]:~/stackoverflow$ touch common_file_b 
[email protected]:~/stackoverflow$ git add . 
[email protected]:~/stackoverflow$ git commit -m "commit common files" 
[master (root-commit) 89a5cd0] commit common files 
0 files changed 
create mode 100644 common_file_a 
create mode 100644 common_file_b 
[email protected]:~/stackoverflow$ git checkout -b other 
Switched to a new branch 'other' 
[email protected]:~/stackoverflow$ mkdir other 
[email protected]:~/stackoverflow$ touch other/other_file_a 
[email protected]:~/stackoverflow$ touch other/other_file_b 
[email protected]:~/stackoverflow$ git add . 
[email protected]:~/stackoverflow$ git commit -m "commit other files" 
[other 9c7409c] commit other files 
0 files changed 
create mode 100644 other/other_file_a 
create mode 100644 other/other_file_b 
[email protected]:~/stackoverflow$ git checkout master 
Switched to branch 'master' 
[email protected]:~/stackoverflow$ touch master_file_a 
[email protected]:~/stackoverflow$ touch master_file_b 
[email protected]:~/stackoverflow$ git add . 
[email protected]:~/stackoverflow$ git commit -m "commit master files" 
[master 3558768] commit master files 
0 files changed 
create mode 100644 master_file_a 
create mode 100644 master_file_b 
[email protected]:~/stackoverflow$ ls 
common_file_a common_file_b master_file_a master_file_b 
[email protected]:~/stackoverflow$ git merge other 
Merge made by the 'recursive' strategy. 
0 files changed 
create mode 100644 other/other_file_a 
create mode 100644 other/other_file_b 
[email protected]:~/stackoverflow$ ls 
common_file_a common_file_b master_file_a master_file_b other 
[email protected]:~/stackoverflow$ ls other 
other_file_a other_file_b 

如果你得到不同的結果,你要麼缺少一個步驟,將一個額外的步驟的地方,或者你得到某種錯誤的你不告訴我們關於,就像合併衝突一樣。我們無法知道爲什麼這麼基本的東西不適合你,除非你發佈了命令和輸出結果,就像我上面做的那樣。

2

這有點晚,但對於發現此問題的其他用戶,我將描述AJcodez觀察到的問題將發生的情況。如果您執行的是git checkout master; git merge other,則在此期間或之後,您將刪除master中的一些新文件(並可能忘記該事實),然後再次執行git checkout master; git merge other,則「0123」將不會再出現在master中,因爲它們是不是新的。合併只關心與合併基礎有關的更改,合併基礎是通過兩個分支可達到的最新的提交。在第二次合併期間,合併基礎與第一次合併期間不同,在第二次合併期間的合併基礎是在第一次合併期間提交的other的提示。如果other從那時起沒有更改新文件,那麼master中的刪除是最新的更改,因此刪除新文件將是第二次合併後的狀態。

如果這看起來不方便,你可以通過創建一個臨時黨支部(姑且稱之爲merge_branch),使用--squash合併other進去,提交其合併到master和刪除臨時黨支部繞過它。這可能不是理想的解決方案(例如,已解決的合併衝突可能需要再次解決),但是原來的情況又可能是錯誤的結果。這裏是我的意思是代碼:

git log other # find the commit where 'other' originally branched off 
git checkout -b merge_branch COMMIT_ID # go there & create branch 
# without '--squash', the following merge would simply be a fast-forward 
# merge and wouldn't solve our problem, because 'merge_branch' would then 
# point to the same commit as 'other' and so the later merge into 
# 'master' would produce the same unsatisfactory result. 
git merge --squash other # does not create a new commit by itself 
git commit # better add an explanation for what you did and why 
# 'merge_branch' now contains everything you did in 'other' since it 
# branched off from 'master', but squashed into a single new commit 
git checkout master 
git merge merge_branch 
git branch --delete merge_branch 
1

我通過在主分支創建具有相同名稱的空文件解決了這個問題:

假設,分支other包含一個新的文件newfile.txt這不是以某種方式合併到master

git checkout master 
touch newfile.txt 
git add newfile.txt 
git commit -m "create newfile.txt" 
git merge other 

這是一種骯髒,但工程。