Merging master branch to another Git branch doesn't delete the files within branch

So, I started using Git just a couple of weeks back and facing a problem for which I can’t seem to find a proper answer. The problem is quite simple, actually.

  1. At the start of the release, we had a master, and that’s it.
  2. There were 3 branches created off of master, let’s say X, Y and Z.
  3. There were about 50 files deleted from X, and pushed back to master like 3 days ago.
  4. Master correctly shows that those 50 files were deleted.
  5. There was frequent interbranch merges between Y and Z for the last 3 days.
  6. In an attempt to get all the changes together, I first update Y with all the changes in Z, and finally tried to ‘Merge’ the changes from X into Y hoping that would delete the files in my current branch (Y) that were originally deleted in X and later pushed into Master.

However, the merge doesn’t delete the files, and I still have them all in Y. However, when I go the commit logs in SourceTree, I can clearly see the changeset where the 50 files got removed. Doesn’t merge delete the files?

  • git cherry picking to a different base directory
  • Trying to understand advanced git branching
  • git log the difference between 1 branch from another
  • How to apply diff between commits to current head in git?
  • git merge changes from another repo
  • Can git figure out that the branch to be merged has obsolete changes?
  • What's the difference between 'git merge' and 'git rebase'?
  • Does git change unmodified and uncommited files in merge?
  • One Solution collect form web for “Merging master branch to another Git branch doesn't delete the files within branch”

    This sounds like a caching problem. You had all the files in Y but then deleted them in X. For whatever reason, file additions and deletions tend to do weird stuff like this.

    I’d try doing a loop over each file that you wanted to delete and remove it from the cache with

    git rm --cached <file>

    Heres a ruby script that will probably do it for you assuming you are on branch Y (not tested):

    #! /usr/bin/ruby
    
    diff = `git diff --name-status X` #this will give you the name and status as (A, M, D, etc.)
    
    # The output will look like this:
    # A \t added_file
    # D \t deleted_file
    # M \t modified_file
    # etc.
    
    diff = diff.split #splits on new lines
    deleted_files = diff.map do |entry|
      status_name_pair = entry.split('\t')
      return status_name_pair.last if status_name_pair.first == 'D'
    end
    
    deleted_files.each do |filename|
      `git rm --cached #{filename}`
    end
    

    Or if all your deleted files are in one directory you can just do:

    git rm --cached <dir>
    
    Git Baby is a git and github fan, let's start git clone.