Error: the following untracked working tree files would be overwritten by merge (Solved)

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?


How to Solve the untracked working tree files would be overwritten by merge issue?

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

Alternative Method:

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

👋
git clean -d -f . 

Git 2.11 and newer versions:

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

How Gitignore Works?

Gitignore is a new format for ignoring files that was created by Git. It is similar to the old "ignore" option in version control systems, but it has many additional features.

1. Gitignore supports multiple file types, including .gitignore, .svnignore, and .hgignore.

2. The contents of a Gitignore file are committed to the remote branch of your repository when you make a change to that file.

3. If a commit fails because of an ignored file, the branch will be aborted with an error message and no changes will be made to the repository.

4. To use Gitignore in your code, you must add its lines to the "git config" section of your configuration file (e.g., "git config --global gitignore ".gitignore).

How to Use Gitignore to Ignore Files?

  1. In your project’s root directory, create a file called gitignore.
  2. Add the following content to the gitignore file:
  • .gitignore
  • ../../../*
  • test-files
  • README.txt
  1. Save the file and exit your project.

How to Use Gitignore to Avoid getting Unknown Files in Your Project?


To add a Gitignore file to your project, you first need to create an empty Git repository and name it after the file you want to ignore. For example, if you want to avoid any files that may contain sensitive data, you might name your repository " secrets ". Next, install git and gitignore on your computer.

Then, use the following command to add the file secrets/gitignore to your project:
git add secrets/gitignore


If all goes well, your project should now have a new Gitignore file in its contents. You can use this file to exclude specific files from being checked out or worked on by anyone else in your project. To use the FILE list contained in this Gitignore file, run the following command:
git status
You should now see something like this when running git status :


<-- Nothing changed since last time  --- /Users/username/.gitconfig: 11 path HIDDEN:/Users/username/.gitmodules: 7 path secret:/etc/gitlab/config: 6 ---> [new version]
This means that the file secrets/gitignore was added and is being used to ignore files in your project.