731

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
CQM
  • 42,592
  • 75
  • 224
  • 366
  • 3
    possible duplicate of [Force git to overwrite local files on pull](http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull) – Balog Pal Jul 01 '13 at 12:38
  • possible duplicate of [error: The following untracked working tree files would be overwritten by checkout - git](http://stackoverflow.com/questions/4858047/error-the-following-untracked-working-tree-files-would-be-overwritten-by-checko) – Steve Chambers Nov 06 '13 at 08:52
  • 1
    Possible duplicate of [The following untracked working tree files would be overwritten by checkout](https://stackoverflow.com/questions/4858047/the-following-untracked-working-tree-files-would-be-overwritten-by-checkout) – nelaaro Oct 31 '17 at 09:11
  • 8
    git does a good job of forcing you to deal with things you don't care about – micahhoover Dec 11 '20 at 13:49

19 Answers19

1009

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.

userFog
  • 10,685
  • 1
  • 15
  • 7
  • 116
    `git add -A .; git stash` worked for me. The `git add *` variant complained about ignored paths. – imsky Jan 26 '15 at 15:33
  • 15
    I tried git add ., git stash, git pull. It worked, but I still dont get why? – ARK Jun 10 '15 at 20:45
  • 1
    For me, I had a remote file which was tracked and I had a file of the same name in my local repo which was untracked. So, if you add the local file stash the changes and then pull, the remote file should overwrite the local file. – userFog Jun 11 '15 at 14:13
  • 6
    Here's a good link to understand how this works, if anyone is interested. https://git-scm.com/book/en/v1/Git-Tools-Stashing – James M. Lay Aug 30 '15 at 22:59
  • Note that you can then `git stash pop` as long as the files aren't in conflict. – jpmc26 Sep 16 '15 at 23:16
  • 2
    // , This doesn't really go too far into the purpose of this error, really. – Nathan Basanese Sep 28 '15 at 23:12
  • git stash --all Stashes ALL files, the star only takes the current folder. – Entalpi Jan 24 '16 at 14:02
  • I get an error when pulling: **Merge conflict in app/build.gradle Automatic merge failed; fix conflicts and then commit the result.** – IgorGanapolsky Sep 08 '16 at 13:33
  • This answer doesn't work if the files are from submodules that were removed and then readded as normal files. See Asaf's answer in this case – Routhinator Sep 12 '16 at 02:21
  • The reason of such error is that the using of stash applying cause different file structure. The command above solve my problem using stash again. – onebraveman Sep 12 '16 at 07:10
  • I get an error: `There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details` – Tomáš Zato Sep 27 '16 at 14:22
  • what if i do not want the non tracked files to be replaced and i just want to pull everything else? – Neville Nazerane Oct 16 '16 at 07:10
  • I was dealing with this issue on a merge, I assume it would be the same for a pull as well. I only add the files and then merged, not sure why you would stash the files also if you didn't need them as the OP stated. – doz87 Jan 03 '17 at 02:32
  • I got this error when I was changing branches and there were merge conflicts (dont' remember exactly how it happened). git clean and git rm -cached did not help, but this answer did. – Phil Carter May 15 '17 at 15:33
  • 1
    Don't you want to add, at the end, `git stash drop`, i.e. drop the stash just created, since the OP says "but I don't care", i.e. these files are of absolutely no value to her/him? – mike rodent Oct 08 '19 at 16:13
  • git add . git stash git pull – Darren Murphy Jul 29 '20 at 14:38
  • `git stash` results in "You do not have the initial commit yet" if you're trying to get something back into source control. – Matt Arnold Sep 11 '20 at 17:26
  • Then committing said changes and running it again results in "No local changes to save"! – Matt Arnold Sep 11 '20 at 17:30
  • Then trying to pull the changes from the remote results in " refusing to merge unrelated histories"! What makes the histories "unrelated"?! What histories?! They're un-source-controlled files - they have no history! – Matt Arnold Sep 11 '20 at 17:32
  • git add * worked as it reduced the number of files that git complained about but still aborted as two were left. I tried the top comment here by using git add -A . and that worked on the next git pull – DavidG May 10 '23 at 07:03
321

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
sKhan
  • 9,694
  • 16
  • 55
  • 53
265

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}}
user3303020
  • 933
  • 2
  • 12
  • 26
Asaf Manassen
  • 3,893
  • 2
  • 21
  • 19
  • 10
    It should be noted that this answer is needed if you have removed submodules and readded them as libraries in the original repo. I needed an answer and this was all that worked. – Routhinator Sep 12 '16 at 02:18
  • I didn't have submodules, and tried git clean and git stash as suggested in the other answers, but only this helped. – kslstn Apr 03 '19 at 07:53
  • After clearing a variety of submodules this was the only things that worked for me as well. Of special note, I had a case sensitivity issue as well on a file. Ex. XML.php to Xml.php in my vendor folder from composer. – KazaJhodo Aug 01 '19 at 20:34
  • 1
    It worked for me too. Can you please explain what is going on? I don't get it. – Dimitris Karamanis Dec 08 '19 at 15:08
  • 7
    This deleted two of my local commits, take care before using this! – David Cian Jan 09 '20 at 12:46
  • @Asaf, every time I have this issue. This is the only approach that works, while the rest don't, and I don't know why it does :) I have to first zip and backup my work in case I don't loose local commits. I would appreciate it if you or someone threw more light on whats happening with these two commands. – iamcastelli Mar 01 '20 at 11:30
  • This is the correct answer. But only if you do not have local commits in the branch you want to reset. Sometimes it might be needed to add `sudo` in front of the `git reset` command for permission issues. –  Mar 06 '20 at 08:47
  • 4
    Before using --hard, think twice. The default behavior is meant to protect you against your mistakes – Patrick Mutuku Apr 26 '20 at 11:53
  • 2
    This should be the accepted answer :D nothing was working for me but this solution worked perfectly. thanks mate!! – Shehbaz Khan May 20 '20 at 21:01
  • This is the only solution works for me! Thanks....;-) – bostonsqd Mar 25 '21 at 15:44
  • Sure enough, this worked when trying to pull a git tracked project from a remote repository to a newly configured Anaconda project environment in PyCharm on a different machine. Thanks! – Nicholas Stommel May 25 '23 at 02:58
99

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
Esteis
  • 4,669
  • 2
  • 29
  • 45
32

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

CanCoder
  • 1,073
  • 14
  • 20
Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65
  • 20
    deleting possibly used files in a project is shouldn't be the real solution – Erdinç Çorbacı Aug 02 '18 at 15:19
  • 14
    yikes, cleaned up IDE files as well :( – kisna Jan 15 '20 at 23:11
  • This cleaned up my anaconda files in a python project – Jonno_FTW Oct 01 '20 at 01:06
  • Yeah it cleans my bazel build cache as well. – yuqli Mar 15 '21 at 23:56
  • 2
    Please use this command very carefully. It deleted lots of files in my code. From the last 2 days, I am not able to execute my code again. – Abhi May 24 '21 at 17:43
  • @ProsyArceno: Why should it be removed? It offers an answer. It might well be a _bad_ answer, but that's what the downvote and comment options are for. – Jeremy Caney Oct 01 '21 at 08:30
  • I tried to rebase my codes as hard as I can and I kept getting `error: The following untracked working tree files would be overwritten by merge...` but after using this command, even though it deleted so many files, I was able to complete my rebase. That's probably why people are upvoting it. It is helping them to complete the process regardless of how many files it is deleting – Mwibutsa Floribert Feb 09 '22 at 08:38
28

You can try that command

git clean -df

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

Amr Mohammed
  • 573
  • 6
  • 13
16

If this is a one-time operation, you could just remove all untracked files from the working directory before doing the pull. Read How to remove local (untracked) files from the current Git working tree? for information on how to remove all untracked files.

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

Community
  • 1
  • 1
mnagel
  • 6,729
  • 4
  • 31
  • 66
15

Step 1: Cleaning Up the Working Copy

a) Saving Local Changes on a Stash If you want to preserve your local changes, you can safely store them on a Stash. They will be available in case you want them back at a later point.

$ git stash --include-untracked

b) Discarding Local Changes If you are sure that you don't need them anymore, you can discard your local changes completely:

$ git reset --hard

c)If you also have untracked / new files, you will have to use the "git clean" command to get rid of these, too:

$ git clean -fd

Step 2: Pull Again After you have cleaned up any local changes / untracked files that would have been overwritten, the pull will finally work:

$ git pull
Ashish Chaugule
  • 1,526
  • 11
  • 9
  • 2
    `git clean -fd` is a very dangerous operation; one shouldn’t run it without being extra sure of what it does. – bfontaine Dec 05 '22 at 10:06
  • `git reset --hard` is a very dangerous operation also. `git reset --soft` is preferred by me. – caot Apr 07 '23 at 14:10
14

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.

matt
  • 4,614
  • 1
  • 29
  • 32
  • Thank you, I often have untracked files in my working directory that I don't want deleted or committed. In my case I needed to substitute `git pull` with `git checkout ` – mihow Feb 27 '19 at 18:42
  • This was perfect especially when there's numerous files to not have to do it one by one, thank you! – sMyles Apr 09 '21 at 20:15
13

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)

Michał Szkudlarek
  • 1,443
  • 1
  • 21
  • 35
  • 4
    This is the best/simplest answer. Crazy that there isn't an option in git merge to do this automatically. – Samuel Jul 09 '20 at 14:31
12

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
Zahin
  • 307
  • 3
  • 12
8

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.
Maarten
  • 101
  • 1
  • 2
7

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.

A. Davidson
  • 397
  • 1
  • 4
  • 14
  • Didn't work for me. I deleted all files in the folder. Then `git pull -f` then `git checkout .`. What a nightmare. – m.rufca Aug 20 '19 at 13:21
  • Really? It worked fine for me just a few weeks ago. Did you try editing your gitconfig file to start/stop ignoring case as appropriate? – A. Davidson Aug 21 '19 at 22:26
  • Thank you. I needed to merge from master after fixing the casing in the folder path. I was able to merge it with git ignore case on, after that I just re-enable the case sensitivity. – Rubenisme Apr 22 '20 at 15:00
  • 2
    Thanks to your answer, in my case I was able to workaround this problem by using `git config core.ignorecase true` – Lucas Cimon Mar 22 '23 at 09:41
6

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.

MMM
  • 125
  • 1
  • 6
6

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

Forhadul Islam
  • 1,159
  • 11
  • 13
5

The post Git 2.23 (Q3 2019) answer would not use the old and confusing git checkout command as in Esteis's answer.

You would use:

So:

Safely remove/overwrite only bothersome files

When you want to merge:

git switch -f receiving-branch # -f is an alias for --discard-changes.
git merge donor-branch         # merge works

When you want to pull:

git switch -f mybranch    # automatically track origin/mybranch
git pull

It avoids:

  • the git clean dangerous operation
  • the detached head like a git checkout origin/myBranch)
  • the explicit pull, since git switch has a guess mode which is the equivalent of git switch -c <branch> --track <remote>/<branch>. Meaning a simple git pull is then enough.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

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).

Tom Bush
  • 858
  • 8
  • 11
0

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

-1

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)

Ammar Mujeeb
  • 1,222
  • 18
  • 21