Safely remove/overwrite only bothersome files
When you want to merge
:
git checkout -f donor-branch # replace bothersome files with tracked versions
git checkout receiving-branch # tracked bothersome files disappear
git merge donor-branch # merge works
When you want to pull
:
git fetch
git checkout -f origin/mybranch # replace bothersome files with tracked versions
git checkout mybranch # tracked bothersome files disappear
git pull origin/mybranch # pull works
That's all you need to know to use this. Below is an explanation.
Detailed explanation
The Bothersome Files that we are going to remove:
- exist in the donor branch (for
git pull
: the upstream branch),
- do not exist in the receiving branch,
- and are blocking the merge because they are present and untracked in your working directory.
git merge -f
and git pull -f
do not exist, but git checkout -f
does.
We will use git checkout -f
+ git checkout
to track + remove the Bothersome Files, and then your merge can proceed normally.
Step 1. This step forcibly replaces untracked Bothersome Files with tracked versions of the donor branch (it also checks out the donor branch, and updates the rest of the working dir).
git checkout -f donor-branch
Step 2. This step removes the Bothersome Files because they they are tracked in our current (donor) branch, and absent in the receiving-branch
we switch to.
git checkout receiving-branch
Step 3. Now that the Bothersome Files are absent, merging in the donor branch will not overwrite any untracked files, so we get no errors.
git merge donor-branch