11

2つのブランチがあるmasterとしotherます。

私は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にアクセスしましたが、ファイルがありません。他のファイルがコミットされて表示されますが、2つのフォルダーに一致するコンテンツがありません。ファイルはに存在しますが、には存在しotherませんmaster。明確にするために、ファイルは新しいものではありませんでした。masterしかし、ファイルが存在しない場合、マージすると少なくともファイルがコピーされると思いました。

4

4 に答える 4

8

少し遅いですが、この質問を見つけた他のユーザーのために、AJcodezによって観察された問題が発生する状況について説明します。を実行した場合git checkout master; git merge other、その間またはその後にいくつかの新しいファイルを削除しmaster(そしておそらくその事実を忘れて)、後でもう一度実行するgit checkout master; git merge otherと、「新しい」ファイルは新しいものではないため、に再表示されmasterません。マージは、両方のブランチを介して到達可能な最年少のコミットであるマージベースに関連する変更のみを考慮します。2回目のマージ中、merge-baseは最初のマージ時と同じではありません。説明されているシナリオの2回目のマージ中のmerge-baseはother、最初のマージ中のチップであったコミットです。otherそれ以降、新しいファイルを変更しなかった場合は、masterは最新の変更であるため、新しいファイルの削除は2回目のマージ後の状態になります。

それが不便だと思われる場合は、一時ブランチを作成し(それを呼びましょうmerge_branch)、otherを使用してマージし--squash、コミットし、マージして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
于 2016-11-30T15:21:24.810 に答える
4

このような:

karl@Bielefeldt-Server:~/stackoverflow$ git init .
Initialized empty Git repository in /home/karl/stackoverflow/.git/
karl@Bielefeldt-Server:~/stackoverflow$ touch common_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch common_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/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
karl@Bielefeldt-Server:~/stackoverflow$ git checkout -b other
Switched to a new branch 'other'
karl@Bielefeldt-Server:~/stackoverflow$ mkdir other
karl@Bielefeldt-Server:~/stackoverflow$ touch other/other_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch other/other_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/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
karl@Bielefeldt-Server:~/stackoverflow$ git checkout master
Switched to branch 'master'
karl@Bielefeldt-Server:~/stackoverflow$ touch master_file_a
karl@Bielefeldt-Server:~/stackoverflow$ touch master_file_b
karl@Bielefeldt-Server:~/stackoverflow$ git add .
karl@Bielefeldt-Server:~/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
karl@Bielefeldt-Server:~/stackoverflow$ ls
common_file_a  common_file_b  master_file_a  master_file_b
karl@Bielefeldt-Server:~/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
karl@Bielefeldt-Server:~/stackoverflow$ ls
common_file_a  common_file_b  master_file_a  master_file_b  other
karl@Bielefeldt-Server:~/stackoverflow$ ls other
other_file_a  other_file_b

異なる結果が得られる場合は、ステップが欠落しているか、どこかに余分なステップが追加されているか、マージの競合など、通知されていない何らかのエラーが発生しています。上記のように、取得した正確なコマンドと出力を投稿しない限り、なぜそれほど基本的なものが機能しないのかを知る方法はありません。

于 2012-11-29T04:45:18.433 に答える
1

マスターブランチに同じ名前の空のファイルを作成することで、この問題を解決しました。

ブランチotherに、何らかの方法でマージされなかった新しいファイルnewfile.txtが含まれているとしmasterます。

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

ちょっと汚れていますが、動作します。

于 2017-04-08T09:35:30.447 に答える
0

私に起こるのは

git merge other

十分ではありません。とマージする前に、ブランチから変更をプルする必要がありましたother

git checkout other
git pull
git checkout first

そして、私はすることができました

git merge other
于 2018-12-14T14:13:35.197 に答える