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.
git branch --track <branch-name> origin/<branch-name>
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.
git add -A git stash git pull
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.
git fetch --all git reset --hard origin/<branch-name>
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/<branch-name>, 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.
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.
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.