HOWTO · Git

How to Ignore Local Changes When Pulling From Remote Repository

In this article, discover effective methods to ignore local changes when pulling from a remote Git repository. Learn how to stash, reset, or create new branches to manage your local modifications seamlessly. Enhance your Git skills and streamline your workflow with these practical solutions.

On this page

When local edits prevent an update, first decide whether to keep them. Use git stash to set work aside temporarily, or commit it before switching branches. If you intend to discard tracked edits and make the current branch point at the fetched remote commit, use git fetch followed by git reset --hard <remote>/<branch>. The reset discards staged and unstaged changes to tracked files; it does not generally remove unrelated untracked files. Preview git clean separately before using it to delete untracked files.

Choose whether to keep or discard local changes

Check git status --short before choosing a workflow. A git pull fetches remote commits and integrates them into the current branch; it is not a command that blindly overwrites every local file. Git may stop when local edits would be overwritten or when integration produces conflicts.

If the edits matter, commit or stash them. If you have verified they are disposable, reset the current branch to the intended remote-tracking branch. These choices affect different state: a stash holds uncommitted work, a commit records work in history, and a hard reset moves the current branch and resets tracked files.

Temporarily save changes with git stash

A stash is useful when you want a clean working tree for the update and plan to restore your edits afterward. Include untracked files with -u; without it, ordinary untracked files are left in the working tree.

Run this local example to save a tracked edit before updating the working copy. It creates a temporary repository and removes it when finished. The example is unexecuted in this article-authoring stage.

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
cd "$tmp"
git init -q
git config user.name Example
git config user.email example@example.com
printf 'base\n' > note.txt
git add note.txt
git commit -qm 'base'
printf 'local edit\n' > note.txt
git stash push -u -m 'save local edit' -q
printf 'working copy: '
cat note.txt
git stash pop -q
printf 'restored edit: '
cat note.txt

For a real repository, stash with git stash push -u -m "before pull", then run git pull. Restore the work with git stash apply or git stash pop. Applying keeps the stash entry; popping removes it after a successful application. If restored edits conflict with incoming changes, resolve the files and check the stash list before dropping anything.

Discard tracked changes and match a remote branch

Use this only after confirming that tracked edits can be discarded. Fetch the remote-tracking references, then reset to the specific remote branch, for example git fetch origin followed by git reset --hard origin/main. Replace origin and main with the remote and branch you intend to follow. Fetch updates the reference; reset moves the current branch to that commit and replaces the index and working-tree contents of tracked files.

A hard reset does not clean all untracked or ignored files. An untracked file that blocks writing a tracked path can be removed as part of the reset, so inspect git status --short first. Local commits no longer pointed to by the branch are not part of the new branch history; do not use this workflow if you need to keep them.

Keep a copy on a new branch before resetting

A branch points to a commit, so it protects committed history at the current HEAD; it does not save uncommitted edits. To preserve the current commit, create a branch such as git branch backup-before-reset before resetting. If there are uncommitted edits to retain, commit or stash them first. Then switch to the intended branch and perform the fetch-and-reset workflow. You can later inspect or merge commits from the backup branch as needed.

Remove untracked files only when needed

git clean deletes untracked files and is separate from git reset --hard. Preview the proposed file removal with git clean -n, or include directories in the preview with git clean -nd. After reviewing that list, git clean -f removes untracked files and git clean -fd also removes untracked directories. Ignored files require an additional option: -X targets ignored files, while -x includes ignored files along with other untracked files. Review a dry run before either option; ignored files may contain local configuration or generated data you still need.

How git pull handles local changes

git pull fetches and then integrates the selected upstream into the current branch, using the configured merge or rebase behavior. It may succeed when local edits do not overlap incoming changes, or stop for conflicts or edits that would be overwritten. It does not promise to replace the working tree with the remote version.

To keep local work, stash or commit it before pulling, then restore or continue from that commit. To discard tracked changes and match one remote branch, fetch that branch and use git reset --hard <remote>/<branch>. Clean untracked files only when you have reviewed the separate git clean preview.