Home > database >  Git merge loses a few files
Git merge loses a few files

Time:09-21

I have two git branch. One is "master" who has a lot of materials. The other one is called "simple". When I did originally cloned "Simple" from "Master", I removed a lot of files from Simple, so "Simple" is a stripped down version of "master". A couple weeks later, some amount of work has been done on "simple" and when i switch to the "master" branch and try

git merge simple

I notice that some of my codes, but not all of them (that were not in the "simple" branch) is currently missing. I have to do a reset to a pre-merged commitment to find all the files

Let me know what to do

CodePudding user response:

When Git performs a merge, it considers exactly three points: the two heads being merged (in this case, simple and master), and a third point, called the merge base, which is usually the common ancestor of those commits.

If you make a change on one branch between the merge base and that head, and you don't make a change on the other side, then that change will be included in the final merge. In your case, the change is deleting a file, and the result has the file being deleted. This is working as intended.

If you want to apply the changes from simple to master without including the deleted files, then you'll need to check out simple to another branch (git checkout -b temp simple) and do an interactive rebase (git rebase -i master), excluding the commits that removed the files. Then, temp will be an extension of master with the relevant commits from simple applied on top. You can then check out master and run git merge temp (either with --ff-only or --no-ff, depending on whether you want a merge commit) and then your changes will be incorporated into `master.

CodePudding user response:

If all of the commits on simple that you wish to merge into master happened after the commits that deleted the files on branch simple, (and from your description it sounds as if that is true), then there is a simple solution:

Use git log simple to determine the last commit ID that contains the deletes that you do not wish to occur on branch master. Let's call that commit ID abcd1234.

Now, checkout master and perform the following two merges in this order:

git switch master
git merge --strategy=ours abcd1234 -m "Merge branch simple without deleting files"
git merge simple

The first merge command tells Git to bring in all the commit IDs on simple up through X, but without taking any of those changes. The second merge will merge in the rest of the commits on simple which have the actual changes you wish to bring into master.

  •  Tags:  
  • git
  • Related