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

GitMergeGit MergeGit Fetch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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 - Git

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

Solution 5 - Git

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

Solution 6 - Git

You can try that command

git clean -df

EDIT: Be aware this will delete the untracked files which can be valuable. thanks to @zhekaus

Solution 7 - Git

If this is a one-time operation, you could just remove all untracked files from the working directory before doing the pull. Read https://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy for information on how to remove all untracked files.

Be sure to not accidentally remove untracked file that you still need ;)

Solution 8 - Git

Update - a better version

This tool (https://github.com/mklepaczewski/git-clean-before-merge) will:

  • delete untracked files that are identical to their git pull equivalents,
  • revert changes to modified files who's modified version is identical to their git pull equivalents,
  • report modified/untracked files that differ from their git pull version,
  • the tool has the --pretend option that will not modify any files.

Old version

How this answer differ from other answers?

The method presented here removes only files that would be overwritten by merge. If you have other untracked (possibly ignored) files in the directory this method won't remove them.

The solution

This snippet will extract all untracked files that would be overwritten by git pull and delete them.

git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} rm -rf "{}"

and then just do:

git pull

This is not git porcelain command so always double check what it would do with:

git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} echo "{}"

Explanation - because one liners are scary:

Here's a breakdown of what it does:

  1. git pull 2>&1 - capture git pull output and redirect it all to stdout so we can easily capture it with grep.
  2. grep -E '^\s - the intent is to capture the list of the untracked files that would be overwritten by git pull. The filenames have a bunch of whitespace characters in front of them so we utilize it to get them.
  3. cut -f2- - remove whitespace from the beginning of each line captured in 2.
  4. xargs -I {} rm -rf "{}" - us xargs to iterate over all files, save their name in "{}" and call rm for each of them. We use -rf to force delete and remove untracked directories.

It would be great to replace steps 1-3 with porcelain command, but I'm not aware of any equivalent.

Solution 9 - Git

Neither clean/reset/hard checkout/rebase worked for me.

So I just removed files that git complained about*

rm /path/to/files/that/git/complained/about

*I checked if this files can be removed by checking out a brand new repo in a separate folder (files were not there)

Solution 10 - Git

The problem is when we have incoming changes that will merge untracked file, git complains. These commands helped me:

git clean -dxf
git pull origin master

Solution 11 - Git

If you consider using the -f flag you might first run it as a dry-run. Just that you know upfront what kind of interesting situation you will end up next ;-P

-n 
--dry-run 
    Don’t actually remove anything, just show what would be done.

Solution 12 - Git

In addition to the accepted answer you can of course remove the files if they are no longer needed by specifying the file:

git clean -f '/path/to/file/'

Remember to run it with the -n flag first if you would like to see which files git clean will remove. Note that these files will be deleted. In my case I didn't care about them anyway, so that was a better solution for me.

Solution 13 - Git

One way to do this is by stashing you local changes and pulling from the remote repo. In this way, you will not lose your local files as the files will go to the stash.

git add -A
git stash
git pull

You can check your local stashed files using this command - git stash list

Solution 14 - Git

For those who don't know, git ignores uppercase/lowercase name differences in files and folders. This turns out to be a nightmare when you rename them to the exact same name with a different case.

I encountered this issue when I renamed a folder from "Petstore" to "petstore" (uppercase to lowercase). I had edited my .git/config file to stop ignoring case, made changes, squashed my commits, and stashed my changes to move to a different branch. I could not apply my stashed changes to this other branch.

The fix that I found that worked was to temporarily edit my .git/config file to temporarily ignore case again. This caused git stash apply to succeed. Then, I changed ignoreCase back to false. I then added everything except for the new files in the petstore folder which git oddly claimed were deleted, for whatever reason. I committed my changes, then ran git reset --hard HEAD to get rid of those untracked new files. My commit appeared exactly as expected: the files in the folder were renamed.

I hope that this helps you avoid my same nightmare.

Solution 15 - Git

In my case when I had this problem. I had a local file that I had renamed on the remote.

When trying to git pull Git told me the new filename was not tracked -- which it was on the remote although it didn't yet exist on local.

Because there was no instance of it locally I couldn't do git pull until I did git rm on the old filename (which wasn't obvious at first because of my stupid idea of renaming it).

Solution 16 - Git

If you have the files written under .gitignore, remove the files and run git pull again. That helped me out.

Solution 17 - Git

I am having same issue, whenever I try to merge master in my local branch it says

"The following untracked working tree files would be overwritten by merge M.xcworkspace/xcshareddata/swiftpm/Package.resolved"

But following any of the above answer didn't worked for me. As when I do git status theres no untracked file, so when I do git add . no file get staged hence stashing doesn't solve the problem nor force checkout as answered above.

I was able to solve by running following commands as mentioned above but more importantly I had to close the Xcode (as may be even after running the clean command it was creating the file that was causing issue for me.)

git clean -dfxn (to check what can be removed)

git clean -d -fx . (remove above listed files)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCQMView Question on Stackoverflow
Solution 1 - GituserFogView Answer on Stackoverflow
Solution 2 - GitsKhanView Answer on Stackoverflow
Solution 3 - GitAsaf ManassenView Answer on Stackoverflow
Solution 4 - GitEsteisView Answer on Stackoverflow
Solution 5 - GitAbhishek GoelView Answer on Stackoverflow
Solution 6 - GitAmr MohammedView Answer on Stackoverflow
Solution 7 - GitmnagelView Answer on Stackoverflow
Solution 8 - GitmattView Answer on Stackoverflow
Solution 9 - GitMichał SzkudlarekView Answer on Stackoverflow
Solution 10 - GitZahinView Answer on Stackoverflow
Solution 11 - GitMaartenView Answer on Stackoverflow
Solution 12 - GitMMMView Answer on Stackoverflow
Solution 13 - GitForhadul IslamView Answer on Stackoverflow
Solution 14 - GitA. DavidsonView Answer on Stackoverflow
Solution 15 - GitTom BushView Answer on Stackoverflow
Solution 16 - Gituko akanowoView Answer on Stackoverflow
Solution 17 - GitAmmar MujeebView Answer on Stackoverflow