Search code examples
gitgit-pullunlink

Git Merge: error: unable to unlink old <file>: No such file or directory


I'm pulling a branch into master in order to merge it.

Command I'm running is (while having master checked out): git pull origin feature/some_branch

Unfortunately it seems my colleagues have performed some (what I would think benign) file deletions on there, and now git spews out a error: unable to unlink old 'somefile': No such file or directory.

I've tried to look online but most of the references for this error concern permissions, which is not the case here.

The file in question is not present on master before the merge, while is it in the new branch.

The problem is that master wasn't updated in a long time so there are way too many changes and files affected for me to start figuring the code out.

I just need master to contain all the changes that came from the new branch. We never commit anything to master directly, always through merges.

What I've tried so far:

  • Using --force parameter, same issue
  • git reset origin/master --hard and running the pull again, same issue

How can I update master with another, more recent branch without caring for such issues, and while keeping its history?


Solution

  • I just need master to contain all the changes that came from the new branch.

    Then you can force the merge, in order to reflect the content of that branch feature/some_branch.

    But instead of using merge --ours master, you can use a similar idea, which preserve the first-parent history:

    git checkout master
    
    # make merge commit but without conflicts!!
    # the contents of 'ours' will be discarded later
    git merge -s ours feature/some_branch    
    
    # make temporary branch to merged commit
    git branch tmp         
    
    # get contents of working tree and index to the one of feature/some_branch
    git reset --hard feature/some_branch
    
    # reset to our merged commit but 
    # keep contents of working tree and index
    git reset --soft tmp
    
    # change the contents of the merged commit
    # with the contents of feature/some_branch
    git commit --amend
    
    # get rid off our temporary branch
    git branch -D tmp
    
    # verify that the merge commit contains only contents of feature/some_branch
    git diff HEAD feature/some_branch