[git] .gitignore and "The following untracked working tree files would be overwritten by checkout"

So I added a folder to my .gitignore file.

Once I do a git status it tells me

# On branch latest
nothing to commit (working directory clean)

However, when I try to change branches I get the following:

My-MacBook-Pro:webapp marcamillion$ git checkout develop
error: The following untracked working tree files would be overwritten by checkout:
    public/system/images/9/thumb/red-stripe.jpg
    public/system/images/9/original/red-stripe.jpg
    public/system/images/8/thumb/red-stripe-red.jpg
    public/system/images/8/original/red-stripe-red.jpg
    public/system/images/8/original/00-louis_c.k.-chewed_up-cover-2008.jpg
    public/system/images/7/thumb/red-stripe-dark.jpg
    public/system/images/7/original/red-stripe-dark.jpg
    public/system/images/7/original/DSC07833.JPG
    public/system/images/6/thumb/red-stripe-bw.jpg
    public/system/images/6/original/website-logo.png
    public/system/images/6/original/red-stripe-bw.jpg
    public/system/images/5/thumb/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/5/original/logocompv-colored-squares-100px.png
    public/system/images/5/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/4/thumb/DSC_0001.JPG
    public/system/images/4/original/logo.png
    public/system/images/4/original/DSC_0001.JPG
    public/system/images/4/original/2-up.jpg
    public/system/images/3/thumb/logo2.gif
    public/system/images/3/original/logo2.gif
    public/system/images/3/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/3/original/11002000962.jpg
    public/system/images/2/thumb/Profile Pic.jpg
    public/system/images/2/original/Profile Pic.jpg
    public/system/images/2/original/02 Login Screen.jpg
    public/system/images/1/original/Argentina-2010-World-Cup.jpg
Please move or remove them before you can switch branches.
Aborting

This is what my .gitignore file looks like:

.bundle
.DS_Store
db/*.sqlite3
log/*.log
tmp/**/*
public/system/images/*
public/system/avatars/*

How do I get this working so I can switch branches without deleting those files?

If I make a change, will it affect those files? In other words, if I came back to this branch afterwards would everything be perfect as up to my latest commit?

I don't want to lose those files, I just don't want them tracked.

This question is related to git git-merge gitignore

The answer is


In my case, I was seeing this error because I am using a popular open source CMS and the directory which was causing issues was the uploads directory which the CMS writes to.

So what it was saying is that there are files which you don't have, but which you can't get from versioning.

I'm grabbing all the files from the live site to my local, then I'll check this into the repo in the hope that this fixes the issue.


I just went to the file system and deleted the file directly, then continued with git checkout and it worked.

I've had the problem occur several times and it may be related to developers doing delete, push, re-add, push or some such thing.


If you want to quickly resolve this question,You can use this command:

git checkout -f dev

Delete .gitignore file from appname/gen/ to solve this issue.


that's easy to solve, git is saying that you have the same files in both branches, therefore you have to delete the specific files from master branch and then you will be able to merge:

git merge "your branch"

I hope it works for you, I just solved my error. my error was:

error: The following untracked working tree files would be overwritten by merge:
        .vs/slnx.sqlite
Please move or remove them before you merge.
Aborting

Now it is working! In my case .vs/slnx.sqlite was generated by visual studio, I needed to close it before delete it.


In my case, the problem was with the submodules. master was merged with another branch which added a new submodule to the project. The branch I was trying to checkout didn't have it, that's why git was complaining about untracked files and none of the other suggested solutions worked for me. I forced the checkout to my new branch, and pulled master.

  • git checkout -f my_branch
  • git pull origin master
  • git submodule update --init

Warning: This will delete the local files that are not indexed

Just force it : git checkout -f another-branch


Move files, instead of delete

One way of avoiding deleting files is to move them instead. For example:

cd "`git rev-parse --show-toplevel`"
git checkout 2>&1 | while read f; do [ ! -e "$f" ] || mv "$f" "$f".bak; done

I had the same problem when checking out to a branch based on an earlier commit. Git refused to checkout because of untracked files.

I've found a solution and I hope it will help you too.

Adding the affected directories to .gitignore and issuing $ git rm -r --cached on them is apparently not enough.

Assume you want to make a branch based an earlier commit K to test some stuff and come back to the current version. I would do it in the following steps:

  1. Setup the untracked files: edit the .gitignore and apply $ git rm -r --cached on the files and directories you want the git to ignore. Add also the file .gitignore itself to .gitignoreand don't forget to issue $ git rm -r --cached .gitignore. This will ensure the the ignore behavior of git leaves the same in the earlier commits.

  2. Commit the changes you just made:

    $ git add -A
    $ git commit
    
  3. Save the current log, otherwise you may get problems coming back to the current version

    $ git log > ../git.log

  4. Hard reset to the commit K

    $ git reset --hard version_k

  5. Create a branch based on the commit K

    $ git branch commit_k_branch

  6. Checkout into that branch

    $ git checkout commit_k_branch

  7. Do your stuff and commit it

  8. Checkout back into master again

    $ git checkout master

  9. Reset to the current Version again

    $ git reset current_version or $ git reset ORIG_HEAD

  10. Now you can reset hard to the HEAD

    git reset --hard HEAD

NOTE! Do not skip the next-to-last step (like e. g. $ git reset --hard ORIG_HEAD ) otherwise the untracked files git complained above will get lost.

I also made sure the files git complained about were not deleted. I copied them to a text-file and issued the command $ for i in $(cat ../test.txt); do ls -ahl $i; done

If you checkout to the branch mentioned above again, do not forget to issue $ git status to ensure no unwanted changes appear.


For those who need something less far-reaching than Scott Schafer’s answer,

git clean -f

will likely work. I highly suggest running

git clean --dry-run

first. That command will output a list of files that Git will remove if you run git clean -f, and might save you the pain of inadvertently removing something you didn’t want to.

See this Stack Oveflow answer or the docs for more information on git clean.


In order to save the modified files and to use the modified content later. I found this error while i try checking out a branch and when trying to rebase. Try Git stash

git stash


Just delete the files or rename them.

e.g.

$ git pull
Enter passphrase for key '/c/Users/PC983/.ssh/id_rsa':
error: Your local changes to the following files would be overwritten by merge:
        ajax/productPrice.php
Please commit your changes or stash them before you merge.
error: The following untracked working tree files would be overwritten by merge:
        ajax/product.php
Please move or remove them before you merge.
Aborting
Updating a04cbe7a..6aa8ead5

I had to rename/delete ajax/product.php and ajax/produtPrice.php.

Don't worry, git pull will bring them back. I suggest you to rename them instead of deleting, because you might loose some changes.

If this does not help, then you have to delete the whole Branch and create it again and then do git pull origin remotebranch


These two functions

  1. git rm --cached
  2. git checkout -f another-branch

did NOT work for me.

Instead, I physically removed the file (in eclipse) as what Git tells you to do;

Please move or remove them before you can switch branches.

and then I add/committed it.

and then I pulled and it worked!


Unfortunately neither git rm --cached or git clean -d -fx "" did it for me.

My solution ended up being pushing my branch to remote, cloning a new repo, then doing my merge in the new repo. Other people accessing the repo had to do the same.

Moral of the story: use a .gitignore file from inception.


WARNING: it will delete untracked files, so it's not a great answer to the question being posed.

I hit this message as well. In my case, I didn't want to keep the files, so this worked for me:

git 2.11 and newer

git clean  -d  -f .

older git

git clean  -d  -f ""

If you also want to remove files ignored by git, then execute the following command.

BE WARNED!!! THIS MOST PROBABLY DESTROYS YOUR PROJECT, USE ONLY IF YOU KNOW 100% WHAT YOU ARE DOING

git 2.11 and newer

git clean  -d  -fx .

older git

git clean  -d  -fx ""

http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

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

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

  • -f is required to force it to run.


There is a command for this delicate task (permanently deleting untracked files)

git clean -i

Then git pull will do.


This could be a permission issue,

change the ownership,

sudo chown -v -R usr-name:group-name folder-name

This worked for me.

 1. git fetch --all
 2. git reset --hard origin/{branch_name}

2 files with the same name but different case might be the issue.

You can Delete one on these files or rename it. Ex:

Pdf.html.twig (The GOOD one)

pdf.html.twig (The one I deleted)

Most of the answers consider deleting or removing the files, which is the easy way. But sometimes you don't want to get rid of the local files. But merge with a strategy, so git has solution for this too ;

git merge --strategy=ours master 

I was also facing a similar issue and i tried all the solutions posted above but it didn't work

The issue was caused when i renamed my onMusicUpdateListener.java to OnMusicUpdateListener.java in develop branch.

Now master had onMusicUpdateListener.java and develop had the same file as OnMusicUpdateListener.java

Now whenever i switched to master it gave me an error

The following untracked working tree files would be overwritten by checkout

and then it aborted.

In order to solve this, i forcefully checked out master branch and then renamed my onMusicUpdateListener.java to OnMusicUpdateListener.java, committed it and then merged it with develop branch.

Then i updated my develop branch by merging it into master and now everything is back to normal and problem is solved.


If you have renamed a file locally and then do a pull, it will display that error message.


Check if any folder name having '/' or any special symbol then rename that folders. Then you just clone the repository to another location.


A simple solution might be: Just make sure that you are in the correct working directory in GitBash. That Message occure almost every time if a User tries to merge a directory too high in his folder hierarchy.

Example:

/workspace/git/myBashSourceFolder/myProjectSourcefolder

Scenario: User cloned repo in git-folder he created a new Java Project in Eclipse, imported the cloned repo. Eclipse set myProjectSourceFolder as Source Folder in his local Project. therefore the User entered it in git bash and pushed, pulled and commited his project from there. git syncs therefore myProjectSourceFolder - but has no record in his history for myBashSourceFolder. Therefore a push / pull /merge from myBashSourceFolder will produce the given output, if User tries to sync from there next time, instead of the folder he worked before.

Solution: Enter correct Folder and try a pull again. In almost every time I encountered, this solution worked fine :)


Git is telling you that it wants to create files (named public/system/images/9/... etc), but you already have existing files in that directory that aren't tracked by Git. Perhaps somebody else added those files to the Git repository, and this is the first time you have switched to that branch?

There's probably a reason why those files in your develop branch but not in your current branch. You may have to ask your collaborators why that is.

how do I get this working so I can switch branches without deleting those files?

You can't do it without making the files disappear somehow. You could rename public to my_public or something for now.

if I came back to this branch afterwards would everything be perfect as up to my latest commit?

If you commit your changes, Git won't lose them. If you don't commit your changes, then Git will try really hard not to overwrite work that you have done. That's what Git is warning you about in the first instance here (when you tried to switch branches).


If you're on OS X, it may be because a file's name has had certain characters change case. Try setting the following config option:

git config core.ignorecase true

In my case git rm --cached didn't work. But i got it with a git rebase


This happened to me on a Windows 8 system, using Git from the command prompt. The rest of my team uses TFS, and I use Microsoft's git-tf to push/pull between TFS and my local Git repository.

The problem arose due to some files that had been renamed only to change their case. What appears to have happened was this:

  • The files were checked in with mixed casing in their names.
  • In a later commit, the file names were changed to all lower-case.
  • git-tf initially got the files in mixed case.
  • When the files were renamed to lower-case, git-tf didn't get the files because to Windows 8 those file names are equivalent.
  • Since Git is case-sensitive, it complained that I had the mixed-case files that weren't in source control. But using git status, I couldn't see any changes, since in the Windows command prompt those file names are equivalent.

The simplest solution for me was:

  • git checkout a previous version of the project, well before those files were ever added.
  • Then git checkout the latest version of the project, with the correct file casing.

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-merge

Abort a Git Merge Git pull - Please move or remove them before you can merge Git: How configure KDiff3 as merge tool and diff tool Git: How to pull a single file from a server repository in Git? How to resolve git error: "Updates were rejected because the tip of your current branch is behind" error: Your local changes to the following files would be overwritten by checkout Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch How to merge specific files from Git branches git remove merge commit from history The following untracked working tree files would be overwritten by merge, but I don't care

Examples related to gitignore

Gitignore not working How to add files/folders to .gitignore in IntelliJ IDEA? Apply .gitignore on an existing repository already tracking large number of files Ignore .classpath and .project from Git .gitignore all the .DS_Store files in every folder and subfolder Correctly ignore all files recursively under a specific folder except for a specific file type What should be in my .gitignore for an Android Studio project? Commit empty folder structure (with git) How to remove files that are listed in the .gitignore but still on the repository? What to gitignore from the .idea folder?