Error untracked working tree file gitignore would be overwritten by merge

A common Git error, the fix is explained in this blog post. It happens because files aren't tracked locally, but the same files are present remotely.

Let’s say you have two Git repositories: one on GitHub, and one on your computer.
They contain identical (or very similar) files, and what you want to do is “synchronize” them (i.e. make them look exactly the same). Maybe all you need is to download a few missing files from GitHub to your computer, or simply push some changes from your machine to GitHub.

You have tried git pull, but you’re getting this error:

error: Untracked working tree file * would be overwritten by merge.
fatal: read-tree failed

You need the changes, but obviously you don’t want to overwrite or lose any files. Don’t worry, the fix is actually straightforward!

Why am I seeing this error?

The reason is probably because you didn’t clone the repository. In my case, I already had some local files, so instead of running git clone, here’s what I did:

git init
git remote add origin git@github.com:<username>/<reponame>.git

If you try to git pull origin <branch-name>, you might get the “untracked working tree” error.

How do I fix it?

If you have already tried pulling from the remote and it didn’t work, here’s the fix:

git branch --track <branch-name> origin/<branch-name>

For example, if your branch is named main:

git branch --track main origin/main

What this does is simply tell Git that these two branches, main and origin/main, are related to each other, and that it should keep track of the changes between them. Turns out it also fixes the error, since Git can now see that nothing would be overwritten.

Wait — that’s it?

Yes! After running the command above, git status will indeed reveal the differences between the two repositories: your untracked files (i.e. extra files that you only have on your PC) will still be there, and some other files may have been automatically staged for deletion: these are files that are present in the remote repo, but you don’t have locally.

At this point you’ll want to double-check that everything is the way it should be. You may also want to run:

To get a clean state. Don’t worry, this won’t delete anything at all, it will simply unstage any modification that was applied automatically by Git. You can stage back the changes you care about using git add . — once you are happy, you can finally make a commit and run:

Note there’s no need to specify the origin and the branch name anymore, since the two branches (the local and the remote) are now tracked.


Hopefully this article helped you fix your issue; either way, feel free to ask for help by leaving a comment below.

Happy hacking!

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account


Closed

PeterMatula opened this issue

Jan 15, 2018

· 17 comments

Comments

@PeterMatula

Pulling a new master ends with the following error:

$ git pull
error: The following untracked working tree files would be overwritten by merge:
        deps/googletest/CMakeLists.txt
        deps/libdwarf/CMakeLists.txt
        deps/llvm/CMakeLists.txt
Please move or remove them before you can merge.
Aborting

@PeterMatula



Copy link


Collaborator

Author

This is caused by changing the basic structure of the retdec repository. We got rid of all git submodules. See https://github.com/avast-tl/retdec/wiki/Project-Repository-Structure for more details.

The are two possible solutions (pick only one):

  1. Remove the old repository and clone a new one (simple & foolproof):
rm -rf retdec
git clone https://github.com/avast-tl/retdec.git
  1. Or, fix the existing repository:
git submodule deinit --all
git pull
rm -rf build
rm -rf $RETDEC_INSTALL_DIR

@PeterMatula



Copy link


Collaborator

Author

This is not really an issue that could be solved. Everyone that had retdec cloned before the change will have to manually fix the repository according to the instructions above.

I will keep this open for now, since this way, it is more visible for anyone searching for the reason why git pull is not working.

@MisterAwesome23

This is caused by changing the basic structure of the retdec repository. We got rid of all git submodules. See https://github.com/avast-tl/retdec/wiki/Project-Repository-Structure for more details.

The are two possible solutions (pick only one):

  1. Remove the old repository and clone a new one (simple & foolproof):
rm -rf retdec
git clone https://github.com/avast-tl/retdec.git
  1. Or, fix the existing repository:
git submodule deinit --all
git pull
rm -rf build
rm -rf $RETDEC_INSTALL_DIR

Thanks :)

@mtkumar82

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

This worked for me, May be helpful for other.

mohashan, lg906321, juliofihdeldev, hktalent, FelhiAbdelhafidh, grant, estra99, rafaelmarinsGL, praveencanva, anacarmen90, and 24 more reacted with thumbs up emoji
vladimir-lugin, DaneShrewsbury2288, and Nelxio reacted with laugh emoji
anacarmen90, innerpeacez, vladimir-lugin, Jack-Barry, DaneShrewsbury2288, cljk, and wedsonwebdesigner reacted with hooray emoji
bilsak18, vladimir-lugin, dimitris23bp, Ezinnem, nitiratjames, SabaTass, DaneShrewsbury2288, Danielkaas94, and alpizano reacted with heart emoji

@muriminyaga

@Shravankumarhiregoudar

Even after performing the above steps my issue was not solved.
So, unfortunately, I created a new folder and cloned.

git clone "_repo_path_"

@vidven09

take this example:
$ git pull
error: The following untracked working tree files would be overwritten by merge:
deps/googletest/CMakeLists.txt
deps/libdwarf/CMakeLists.txt
deps/llvm/CMakeLists.txt
Please move or remove them before you can merge.
Aborting

What if I am OK to overwrite one file due to merge but want to retain my version for 2 other files?

@auxiliaryjoel

I’m having this same problem, I tried:
git stash
git stash drop
git pull origin master
but it still results in the same warning:
error: The following untracked working tree files would be overwritten by merge:
web/themes/contrib/bootstrap_barrio/css/components/progress.css
web/themes/contrib/bootstrap_barrio/js/toast.js
web/themes/contrib/bootstrap_barrio/scripts/create_subtheme.sh
web/themes/contrib/bootstrap_barrio/templates/layout/maintenance-page.html.twig
Please move or remove them before you merge.
Aborting

How do I get around this?

@s3rvac

@auxiliaryjoel I think that you are in the wrong project (we have never had any of the mentioned files in our repository). Are you sure that you are in a RetDec repository?

@auxiliaryjoel

@s3rvac oh sorry for confusion I noticed the last person to comment on this was having the same problem (different files but the same problem). I was wondering if @vidven09 was able to resolve their issue. If they have, I think what worked for them may answer my issue?

@subhashree-r

$ git add *
$ git stash
$ git pull origin master/{branch_name}

You will lose the local changes however.

@auxiliaryjoel

oh ok so the :
$ git add *
I would do on the remote server that I’m intending to git pull to?

Danielkaas94

referenced
this issue
in Danielkaas94/Xenonauts-Mods

Feb 5, 2020

@Danielkaas94

Added in this commit:
    • New picture
        + Super Shotgun

A big applause and shoutout to mtkumar82:

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

@alpizano

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

This worked for me, May be helpful for other.

omgthank you

@weberthmo

run…
git add *
git pull origin master
git pull

@fisicaetcetera

$ git add *
$ git stash
$ git pull origin master/{branch_name}

You will lose the local changes however.
My change was on remote. Worked fine. Thanks!

@rlynch99

ok.. nothing too tough about it.. i had one file listed.. I deleted it, then did a git pull,
all good.
(if you need to, move the files to a different directory — not under git — and do the pull — you will always be able to move them back afterwards if needed.)

@s3rvac

I am locking the issue as it has been solved and the newly appearing discussions are not related to RetDec.

@avast
avast

locked as resolved and limited conversation to collaborators

Mar 3, 2020

 

  • Игнорируем изменения прав файлов (chmod)
  • error: The following untracked working tree files would be overwritten by merge
  • Локальный git ignore
  • Удаляем тег на удаленном репозитории
  • Слияние ветки в master
  • Прячем правки (сохраняем на потом)
  • Откатываем последний комит в remote
  • Смотрим изменения перед пушем
  • Создание и применение патча

Игнорируем изменения прав файлов (chmod)

git config core.fileMode false

Используем флаг —global чтобы настройка применялась для всех репозиториев(использует ~/.gitconfig вместо .git/config)

git config --global core.fileMode false

error: The following untracked working tree files would be overwritten by merge

git fetch --all
git reset --hard origin/master

Локальный git ignore

.gitignore для всех проектов:

git config --global core.excludesfile ~/.gitexcludes
nano ~/.gitexcludes

.gitignore для конкретного проекта:

nano .git/info/exclude

Удаляем тег на удаленном репозитории

Удаляем тег с remote:

git push --delete origin tagname

Удаляем тег локально:

git tag --delete tagname

Слияние ветки в master

git checkout master
git merge some-branch
git push

Прячем правки (сохраняем на потом)

git stash
git stash list
git checkout -b some-branch
git stash pop (git stash apply stash@{0})

Откатываем последний комит в remote

Ситуация

git add .
git commit -m "comment"
git push origin master

А надо было выполнить:

git push origin some-branch

Решение

Пушим изменения в правильную ветку:

git push origin some-branch

Потом на мастере получим изменения из удалённой репы:

git checkout master
git fetch --all
git rebase origin/master

Сбрасываем изменения на один шаг назад, и принудительно пушим в мастер:

git reset --hard HEAD~1
git push origin master -f 

Смотрим изменения перед пушем

Только файлы:

git diff --stat --cached origin/master
 composer.lock | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

Содержимое:

git diff --cached origin/master

Создание и применение патча

Создание патча

git diff --no-prefix > some-fix.patch

Применение патча:

git apply some-fix.patch

Тоже самое без git:

Создание патча

diff -Naru test.old.txt test.txt > some-fix.patch

Применение патча

patch -p0 < some-fix.patch

Понравилась статья? Поделить с друзьями:
  • Error verifying request error querying service server returns 504 code
  • Error verifying recaptcha please try again
  • Error verifying free blocks on media
  • Error verifying digital signature make sure your systems root certificate are up to date faceit
  • Error verifying digital signature faceit как исправить