0

This question accurately resembles this question: Git merge doesn't merge a file the second time

I will just copy and paste the original question since it describes the background of my question:

I have a master branch with some stuff, let's say file README. I also have a dev branch, child of master, with some additional files, let's say file README2. I do the following: git merge dev from master and get the file README2 merged into master. Now I remove it: git rm README2 Now when I merged dev again, I expected that I will get README2 file merged into master, because it's not in the master anymore. But Git reports there are no changes and nothing to merge. This actually suits me, but I don't understand how is this so, since branch dev at this point clearly has README2 file, while master doesn't.

However, I'm not asking why it happens, I understand that git rebase/merge works at the level of the commit tree, not of individual files, but I would like to ask - in this instance, how would you get back the README2 file from branch dev ? Or in other words, is there a simple way to re-apply the merge as if it happens the first time, without having to delete commits?

EDIT: to put it another way, is there a way to overwrite the files with the files in the other branch, while being asked to solve conflicts? (the exact functionality of using merge)

sir-haver
  • 3,096
  • 7
  • 41
  • 85
  • The simple answer is: You `git revert` the commit that removed the file. – j6t Aug 28 '22 at 20:45
  • Thanks that makes sense, but in case I don't remember what I changed, there's no way to somehow overwrite my files with the other branch files while asking to solve conflicts (exactly like with merge) ? – sir-haver Aug 28 '22 at 20:51
  • Fwiw the format of this question is quite poor. it would benefit from the edit being reworded as the title and showing, not describing, the situation. It’s an odd/confusing choice to quote a different question. – AD7six Aug 28 '22 at 21:22

1 Answers1

1

how would you get back the README2 file from branch dev

You could use the new git restore command (Git 2.23, Q3 2019), and restore the missing file after your merge:

git switch main
git restore -s dev -- ./README2
# add and commit
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250