13

個人的なプロジェクトでgitを使用/学習しようとしています。私とリモートのgitリポジトリ、いくつかのコミットだけがあり、マージの失敗で立ち往生しています。私のファイルの多くには、Gitマージの競合マークアップも含まれています。

gitにすべてを捨てて、私のものを使用するように指示するにはどうすればよいですか?

私がどのようにして私がいる状態になったのかについての具体的な例:

echo A new file > myFile.txt             # example file
git add myFile.txt                       # add file
git commit                               # commit changes
git push                                 # push changes to remote repo
echo A conflicting edit > myFile.txt     # oh, no, forgot some changes
git add myFile.txt                       # add again
git commit --amend                       # amend previous commit
git push                                 # fails. Git suggests to do a pull first
git pull origin HEAD                     # "Automatic merge failed" Now what?
                                         # Just use what I have locally!
4

2 に答える 2

17
git checkout --ours . # checkout our local version of all files
git add -u            # mark all conflicted files as merged/resolved
git commit            # commit the merge

同じリモートオリジンを使用している他のすべての人のレポを壊す可能性のある厄介な代替手段があります。あなたがそれを使用している唯一の人である場合にのみ検討してください。

git reset --hard HEAD # undo that failed merge
git push --force      # replace everything remote with local

説明(これで理解が深まりgitました)
これが発生した理由は、コミットを修正すると「履歴」が変更されるためです。これをローカルで行うことは、他の人に影響を与えないため安全です。ただし、すでにプッシュされたコミットを修正すると、他のリポジトリに影響を与えるため、安全ではありません

于 2012-10-18T14:35:30.513 に答える
6

あなたのGUIはおそらく設定されているだけです--strategy=oursgit merge -s ours <branch>)。これにより、両方のコミットを親として引用してマージが実行されますが、ディレクトリ全体の状態は維持されます。

他のオプションはgit merge -s recursive -X ours <branch>、両方のブランチからファイルを取り込もうとしますが、競合がある場合は常にバージョンを優先する を使用することです。

ドキュメント

次のデモンストレーション シェル スクリプトを使用して、2 つの異なるスタイルの動作を確認できます。

#!/bin/sh

mkdir gittest
cd gittest
git init

git checkout master
echo "Line one" > bar
git add bar
git commit -m "Original commit"

git checkout -b fork1
echo "Line one and something" > bar
echo "Line two" > bam
git add bar bam
git commit -m "Fork1 commit."

git checkout master
git checkout -b fork2
echo "Line one and other stuff" > bar
echo "Line three" > baz
git add bar baz
git commit -m "Fork2 commit."

git checkout fork1
if [ "$1" = "ours" ]; then
  # `ls gittest` => bam bar
  # `cat gittest/bar` => Line one and something
  git merge -s ours fork2
else
  # `ls gittest` => bam bar baz
  # `cat gittest/bar` => Line one and something
  git merge -X ours fork2
fi
于 2012-09-28T02:45:13.697 に答える