7

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?

Skwiggs
  • 1,348
  • 2
  • 17
  • 42
  • Have you tried doing a `git merge` with master before trying to pull in the changes from your feature branch? – Jacob Sievers Jul 18 '18 at 09:57
  • @JacobSievers same issue, I'm using pull because I can --force, which is not possible with merge – Skwiggs Jul 18 '18 at 10:45
  • Do you have local commits on `master` that you want to preserve, or is it okay to completely overwrite your `master` with the version in `origin/master`? – Rory O'Kane Jul 18 '18 at 15:48
  • @RoryO'Kane `master` is free from any commits, we never commit to it directly... only through merges. And yes, free to completely overwrite from origin – Skwiggs Jul 19 '18 at 08:21

2 Answers2

4

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Doesn't this change the first parent history away from master's history? – Yazeed Sabri Jul 20 '18 at 17:41
  • @YazeedSabri I believe so. For a solution which preserve the first parent: https://stackoverflow.com/a/4969679/6309 – VonC Jul 20 '18 at 17:46
  • The first part of your solution updates `feature/some_branch` with the contents of `master` first if I understand this correctly, but I'd rather avoid that. I'll give the 2nd part of your answer a go – Skwiggs Jul 23 '18 at 09:49
  • @VonC So I've ran the commands in the 2nd part of your answer and it did the trick. Could you ideally rephrase your answer so that that part is more prominent ? I'll mark it as accepted and deliver the bounty :) – Skwiggs Jul 23 '18 at 12:12
  • 1
    @Skwiggs Sorry, for the late update: I was away. I have rewritten the answer accordingly. – VonC Jul 23 '18 at 18:32
0

To replace your master branch with the version on origin/master, you could try deleting and recreating it. master is not required to be checked out while you do this, so you might be able to avoid errors relating to changing your working directory.

First check out a different branch that you are able to check out:

git checkout feature/some-branch

Then delete your local master branch and create it again, including its tracking information:

git branch --delete master
git branch master origin/master
git branch --set-upstream-to=origin/master master

Finally, try switching to the new master:

git branch checkout master
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • That doesn't keep `master`'s history though, which I'd like to keep. Also local & remotes are all up to date with their counterparts. I think deleting `master` always sounds wrong in any situation to be honest :d – Skwiggs Jul 23 '18 at 12:14
  • @Skwiggs I don't understand what you're asking then. I thought [your comment](https://stackoverflow.com/questions/51398858/git-merge-error-unable-to-unlink-old-file-no-such-file-or-directory/51451663?noredirect=1#comment89806395_51398858) replying to me said that this is what you wanted… – Rory O'Kane Jul 23 '18 at 15:12
  • Well `master` and `origin/master` are essentially the same right ? So overwriting my local `master` with its remote is fine (actually they _should_ always be in sync, and right now they already are). What I want to avoid though is to lose the commit history on `master`. Maybe we both mean something else with `origin/master` ? – Skwiggs Jul 23 '18 at 15:21