Git merge error the following untracked working tree files would be overwritten by merge

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.

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's user avatar

asked Jul 1, 2013 at 12:16

CQM's user avatar

4

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.

answered Oct 29, 2014 at 19:44

userFog's user avatar

userFoguserFog

10.3k1 gold badge14 silver badges7 bronze badges

21

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's user avatar

double-beep

4,85916 gold badges32 silver badges41 bronze badges

answered Nov 19, 2015 at 12:29

sKhan's user avatar

sKhansKhan

9,34416 gold badges54 silver badges53 bronze badges

10

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's user avatar

user3303020

8332 gold badges10 silver badges25 bronze badges

answered Apr 24, 2016 at 14:36

Asaf Manassen's user avatar

Asaf ManassenAsaf Manassen

3,7052 gold badges20 silver badges19 bronze badges

10

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

answered Jul 4, 2018 at 16:26

Esteis's user avatar

EsteisEsteis

4,4592 gold badges29 silver badges45 bronze badges

2

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

leeCoder's user avatar

leeCoder

1,01312 silver badges20 bronze badges

answered Sep 29, 2016 at 8:36

Abhishek Goel's user avatar

Abhishek GoelAbhishek Goel

18k10 gold badges86 silver badges65 bronze badges

7

You can try that command

git clean -df

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

answered Jan 28, 2016 at 16:49

Amr Mohammed's user avatar

3

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's user avatar

answered Jul 1, 2013 at 12:26

mnagel's user avatar

mnagelmnagel

6,4744 gold badges30 silver badges66 bronze badges

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.

answered Sep 10, 2018 at 9:54

matt's user avatar

mattmatt

4,5941 gold badge28 silver badges31 bronze badges

2

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)

answered Oct 14, 2019 at 9:25

Michał Szkudlarek's user avatar

1

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

answered Jun 17, 2020 at 17:42

Zahin's user avatar

ZahinZahin

1792 silver badges10 bronze badges

2

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

answered Sep 8, 2022 at 12:26

Ashish Chaugule's user avatar

1

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.

answered Aug 9, 2016 at 13:31

Maarten's user avatar

MaartenMaarten

1011 silver badge2 bronze badges

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.

answered Mar 29, 2017 at 2:01

MMM's user avatar

MMMMMM

1251 silver badge6 bronze badges

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

answered May 7, 2019 at 14:06

Forhadul Islam's user avatar

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.

answered Jul 10, 2019 at 4:56

A. Davidson's user avatar

A. DavidsonA. Davidson

3771 gold badge3 silver badges12 bronze badges

3

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:

  • git switch to switch branches
  • git restore to restore files (illustrated here)

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.

answered Jul 21, 2022 at 21:22

VonC's user avatar

VonCVonC

1.2m508 gold badges4248 silver badges5069 bronze badges

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

answered Jan 3, 2020 at 9:00

Tom Bush's user avatar

Tom BushTom Bush

7597 silver badges11 bronze badges

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

answered Mar 25, 2020 at 13:47

uko akanowo's user avatar

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)

answered May 20, 2021 at 12:36

Ammar Mujeeb's user avatar

Ammar MujeebAmmar Mujeeb

1,20218 silver badges21 bronze badges

0

Содержание

  1. Error: the Following Untracked Working Tree Files Would Be Overwritten by Merge
  2. Tell the Local Branch to Track the Remote Branch
  3. Find Your Bootcamp Match
  4. Staging and Stashing
  5. Fetching and Resetting
  6. Conclusion
  7. What’s Next?
  8. Kelly M.
  9. git pull error: The following untracked working tree files would be overwritten by merge #92
  10. Comments
  11. Simone Web Design
  12. Why am I seeing this error?
  13. How do I fix it?
  14. Wait — that’s it?

Error: the Following Untracked Working Tree Files Would Be Overwritten by Merge

Kelly M. — November 10, 2020

The error above is often triggered when we do not clone the repository we are trying to pull from. The projects may be identical, but we may be working on one locally while trying to pull it from the repo on Github because it may have other files or features we’d like to incorporate on our local version.

There are several ways to fix this.

Tell the Local Branch to Track the Remote Branch

We can do this by using the command below.

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

By doing this, the remote branch becomes a counterpart to the remote server. After this, do a git status to see the difference between the two repos.

Staging and Stashing

To pull so local files are not overwritten from version control, we can also stage and then stash by using the commands below.

With git add -A, we are staging all changes. It is the same as git add in that it looks at all

working trees and adds all working paths to stages changes, changed, new, or not ignored. In addition to this, it also acts like git add -u in that it looks at the files already being tracked, and stages the changes to those files if they have been removed or if they differ.

With git stash we are taking staged and unstaged uncommitted changes, stashing them away for future use, then reverting them from a working copy. After this, we are free to make changes like pulling new files.

Fetching and Resetting

If neither of the above has worked for you yet, try fetching and resetting. Because we will be using –hard on this option, it is important to at least attempt the two above as –hard is a potentially dangerous command which throws away all uncommitted changes.

Please do a git status before attempting the fix below to make sure your output is empty.

With git fetch –all, we can fetch all remote branches. Fetch will update local copies of remote branches, but will not update local branches that track remote branches. To achieve this, we need to do a git pull –all.

With git reset –hard origin/
, we are essentially saying “throw everything away that is in my local branch, and make it the same as my remote branch”. It throws away all staged and unstaged changes.

Conclusion

The error: the following untracked working tree files would be overwritten by merge is triggered when we are trying to pull a remote branch while on a local one. The projects may be identical, but the local one needs to be able to track the remote for it to pull successfully.

This error is often triggered when the developer forgets to clone a repo. Other ways to fix this error is by staging and stashing or fetching and resetting, which should be attempted only if the first two methods were not successful.

«Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!»

Venus, Software Engineer at Rockbot

Find Your Bootcamp Match

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What’s Next?

Want to take action?

Get matched with top bootcamps

Kelly M.

About the author: Kelly is a technical writer at Career Karma, where she writes tutorials on a variety of topics. She attended the University of Central Florida, earning a BS in Business Administration. Shortly after, she attended Lambda School, specializing in full stack web development and computer science. Before joining Career Karma in September 2020, Kelly worked as a Developer Advocate at Dwolla and as a team lead at Lambda School. Her technical writing can be found on Codecademy, gitConnected, and JavaScript in Plain English.

Источник

git pull error: The following untracked working tree files would be overwritten by merge #92

Pulling a new master ends with the following error:

The text was updated successfully, but these errors were encountered:

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):
  1. Or, fix the existing repository:

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.

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):
  1. Or, fix the existing repository:

git fetch —all
git reset —hard origin/<>

This worked for me, May be helpful for other.

Yes this works..thanks

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

git clone «_repo_path_»

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?

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?

@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?

@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?

$ git add *
$ git stash
$ git pull origin master/

You will lose the local changes however.

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

Источник

Simone Web Design

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:

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:

If you try to git pull origin
, 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:

For example, if your branch is named 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.

Источник

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

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!

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Git lfs smudge error
  • Git lfs error downloading object
  • Git ignore certificate error
  • Git fatal unable to access the requested url returned error 403
  • Git failed with a fatal error unable to access error setting certificate verify locations

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии