The following untracked working tree files would be overwritten by merge, but I don't care

862,352

Solution 1

The problem is that you are not tracking the files locally but identical files are tracked remotely so in order to "pull" your system would be forced to overwrite the local files which are not version controlled.

Try running

git add * 
git stash
git pull

This will track all files, remove all of your local changes to those files, and then get the files from the server.

Solution 2

You can try command to clear the untracked files from the local

Git 2.11 and newer versions:

git clean  -d  -f .

Older versions of Git:

git clean  -d  -f ""

Where -d can be replaced with the following:

  • -x ignored files are also removed as well as files unknown to Git.

  • -d remove untracked directories in addition to untracked files.

  • -f is required to force it to run.

Here is the link that can be helpful as well.

Solution 3

The only commands that worked for me were: (Please be careful this deletes all the local files)

git fetch --all
git reset --hard origin/{{your branch name}}

Solution 4

tl;dr

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

Explanations below, including for when you want to git pull instead of git merge.


How to git merge while overwriting untracked files

The files of interest (FOI) that we are going to remove:

  • exist in the donor branch,
  • do not exist in the receiving branch,
  • and are blocking the merge because they are present and untracked in your working directory.
git checkout -f donor-branch   # replace FOI with tracked `donor` versions
git checkout receiving-branch  # FOI are not in `receiving`, so they disapppear
git merge donor-branch  # now the merge works

How to git pull while overwriting untracked files

The files of interest (FOI) that we are going to remove:

  • exist in the upstream repository,
  • do not exist in the current branch,
  • and are blocking the merge because they are present and untracked in your working directory.

pull = fetch + merge, so we do git fetch followed by the git checkout -f, git checkout, git merge trick above.

git fetch origin  # fetch remote commits
git checkout -f origin/mybranch  # replace FOI with tracked upstream versions
git checkout mybranch  # FOI are not in mybranch, so they disapppear
git merge origin/mybranch  # Now the merge works. We have pulled by fetching + merging.

Detailed explanation

git merge -f does not exist, but git checkout -f does.

We will use git checkout -f + git checkout to remove the Files Of Interest (see above), and then your merge can proceed normally.

Step 1. This step forcibly replaces untracked FOI 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 FOI 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 FOI are absent, merging in the donor branch will not overwrite any untracked files, so we get no errors.

git merge donor-branch

Solution 5

Remove all untracked files:

git clean  -d  -fx .

Caution: this will delete IDE files and any useful files as long as you donot track the files. Use this command with care

Share:
862,352
CQM
Author by

CQM

Updated on February 08, 2022

Comments

  • CQM
    CQM about 2 years

    On my branch I had some files in .gitignore

    On a different branch those files are not.

    I want to merge the different branch into mine, and I don't care if those files are no longer ignored or not.

    Unfortunately I get this:

    The following untracked working tree files would be overwritten by merge

    How would I modify my pull command to overwrite those files, without me having to find, move or delete those files myself?