0

How to merge branch in git which deleted files in master all the while keeping the deleted files intact in the master branch?

For example, in my test branch I deleted a folder called /sounds and in my master I have this /sounds folder as well, how can I merge my test branch into master and keeping the /sounds folder?

Johnny Bra
  • 101
  • 2
  • 12
  • You have deleted the /sounds folder from master also ? – Piyush Apr 08 '20 at 19:53
  • Is it for a one time merge, or you want to have a rule for all future merge ? This can help https://stackoverflow.com/questions/928646/how-do-i-tell-git-to-always-select-my-local-version-for-conflicted-merges-on-a-sp/930495#930495 – Ôrel Apr 08 '20 at 19:55

2 Answers2

1

The key to understanding this is to recognize a couple of facts:

  • Files aren't in branches. Files are in commits.
  • A merge commit is a commit.
  • Each commit stores a full snapshot of all files.
  • The files that are in a new commit you make—including a new merge commit—comes from the files that are in Git's index at the time you run git commit or git merge --continue.

Hence, if git merge is going to make a commit that you don't like, you can do one of two things:

  • Allow it to make the commit you don't like, then add a second commit to fix it up.
  • Or: make sure git merge doesn't make the commit yet, using git merge --no-commit. The merge will stop before committing. Then, adjust the index to contain the files (and versions of those files) that you want to appear in the commit.

If your question is about the mechanics of getting some file(s) from some commit(s), to put into the next commit, that part is super-trivial, once you understand what a Git commit is and does. For instance, suppose you do this:

git checkout master

git checkout -b otherbranch
git rm -r sounds
git commit

git checkout master
git merge otherbranch

This actually makes the merge commit, assuming there are no merge conflicts (there won't be), and the new merge commit lacks the sounds/ files. But why is this a big deal? You want the sounds files back, just grab them from the commit that comes one step before the new merge commit, along the "main" line:

git checkout master~1 -- sounds/

and then make a new commit:

git commit

that extends master by one more commit again. The new commit has the same files that the merge commit has, except that it also has all the sounds/ files too.

Making the merge while retaining the files produces what some call an evil merge (see Evil merges in git?), but it's equally simple. Replace the git merge step with:

git merge --no-commit otherbranch
git checkout HEAD -- sounds/
git merge --continue

and you make a merge commit that doesn't delete the sounds/ files, because it copied all the sounds/ files from the commit that was the tip of master just a moment ago, before you made the new merge commit.

torek
  • 448,244
  • 59
  • 642
  • 775
0

Restore the files in the branch, then merge it.

git checkout test
git restore --source master sounds
git add sounds
git commit

git diff master should now show no changes to sounds/. Now merge as normal.

If you can't change that branch, make a new branch off it, restore, and merge that branch.

git checkout test
git checkout -b test2
...then restore as above...
Schwern
  • 153,029
  • 25
  • 195
  • 336