简体   繁体   中英

Force Git To Cause Merge Conflict

I have few files which I would like to "invalidate" in my dev_branch lets call it branch "A" so once merging with another dev_branch lets call it branch "B" I get a merge conflict. Those few files I mention have been previously edited in that other dev_branch called B.

Here is what I tried so far:

I removed the files from index cache of my branch "A" and added them back again.

git checkout -b feature/A origin/feature/A
git -rm -r --cached "myFolder"
git commit -am "removed the files"
cd "myFolder"
git add .
git commit -am "added back the files"

So far the branch A is two commits away from master and branch B just one with edited files and now I tried to merge but still not getting the merge conflict

git merge origin/feature/B

why cant I cause merge conflict? both have commits not in master branch how do I force a merge conflict without individually changing each files content?

In order to get a conflict in any one particular file, you need to have Git:

  1. compute the merge base commit, as it does for most merges;
  2. find that this commit is an ancestor of both branch-tip commits;
  3. find that one particular file in the merge base differs from that same file in both tip commits;
  4. and last, find that combining those changes results in a conflict.

For more about the first three, see any of many previous questions. I have a long form answer here that goes into details of merge strategies, as well as discussing the normal process of finding the merge base given the default kind of merge. For much more about point 4, see when exactly does a git merge conflict arise .

(The contents of your index / staging-area at the time you run git merge are not relevant to this particular process, except that git merge normally requires that the index be "clean", ie, match the current commit, to start.)

You can also get what I call a high level conflict, as eftshift0 notes in a comment . For this to occur, the merge base version of the file can be:

  • missing: this is an "add/add" conflict
  • have a different name than in one of the two tip commits, and be missing in the other tip commit: this is a "rename/delete" conflict
  • have different content in base vs one tip, and be deleted in the other tip: this is a "modify/delete conflict"
  • have three different names in the three commits: this is a "rename/rename" conflict

(I think that covers all possible cases).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM