This error occurs when Git detects uncommitted changes in your working directory that conflict with incoming changes from a merge. You need to either commit, stash, or discard your local changes before Git can proceed with the merge.
When you run `git pull` or `git merge`, Git attempts to combine the remote branch's changes with your local branch. However, if you have uncommitted modifications to files that are also modified in the incoming changes, Git cannot safely complete the merge without potentially losing your work. Git is being protective hereβit refuses to overwrite your uncommitted changes because there's no way to recover them if they're lost. This is intentional behavior, not a bug. The error message lists exactly which files have conflicting local changes. This situation commonly occurs when: - You've made local edits but haven't committed them yet - A teammate pushed changes to the same files you're working on - You're switching branches or pulling updates in the middle of active development - CI/CD pipelines pull changes into a working directory that has been modified
First, understand what files Git is protecting:
git statusThis shows all modified, staged, and untracked files. The files listed in the error message will appear here as "modified" or "staged".
If you want to keep your local changes and apply them after the merge, use git stash:
# Save your changes temporarily
git stash
# Now pull/merge will work
git pull
# Restore your changes
git stash popIf there are conflicts when popping the stash, Git will notify you and you can resolve them manually. Your stashed changes are still safe in the stash list even if pop fails.
To see what's in your stash:
git stash list
git stash show -p stash@{0}If your changes are ready to be saved permanently, commit them before merging:
git add .
git commit -m "WIP: Save local changes before merge"
git pullIf there are conflicts after pulling, Git will enter merge conflict resolution mode where you can address each conflict individually. This is often cleaner than resolving stash conflicts.
If you don't need your local changes, you can discard them:
Discard changes to specific files:
git checkout -- path/to/file.txtDiscard all uncommitted changes:
git checkout -- .Or using the newer restore command (Git 2.23+):
git restore path/to/file.txt
git restore .After discarding, you can proceed with your merge or pull.
For a streamlined workflow, use autostash which automatically stashes before rebasing and pops after:
git pull --rebase --autostashThis is equivalent to:
git stash
git pull --rebase
git stash popTo make this the default behavior:
git config --global rebase.autoStash trueAs a last resort, if you want to completely discard local changes and match the remote:
git reset --hard HEAD
git pullWarning: This permanently deletes all uncommitted changes. Only use this if you're certain you don't need your local modifications.
For an even more aggressive reset to match remote exactly:
git fetch origin
git reset --hard origin/main### Dealing with Generated Files
If the conflict is in files like package-lock.json, .idea/, or other generated files that you don't manually edit:
# Discard only the generated files
git checkout -- package-lock.json
git pull
npm install # Regenerate lock fileConsider adding generated files to .gitignore if they shouldn't be tracked.
### Partial Stash
If you only want to stash specific files:
git stash push -m "Stashing specific files" -- path/to/file1.txt path/to/file2.txt
git pull
git stash pop### Stash Including Untracked Files
By default, git stash only saves tracked files. To include untracked files:
git stash -u # Include untracked files
git stash -a # Include all files (untracked + ignored)### Viewing Diff Before Deciding
Before choosing how to handle your changes, review what you've modified:
# See what you changed locally
git diff
# See what's coming from remote
git fetch origin
git diff HEAD..origin/main### CI/CD Pipelines
In CI/CD environments, this error often occurs when the pipeline workspace isn't clean. Add a cleanup step:
git clean -fd # Remove untracked files
git checkout -- . # Discard modifications
git pullOr configure your CI to always start with a fresh clone.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git