How to Ignore Local Changes When Pulling From Remote Repository

John Wachira Feb 02, 2024
How to Ignore Local Changes When Pulling From Remote Repository

This article will discuss how you can force a git pull command to override our local files. This action comes in handy when several people are working on the same file, and we want to update our files based on the remote repository.

Let’s jump right in.

Force Git Pull to Override Local Files

Matching our local repository to the remote repository starts with fetching.

Fetch Branches

We use the git fetch command to fetch commits, files, and references from our remote repository. Note this command only downloads the files, commits and references to our local repository without merging anything.

You can fetch from the remote repo in the context below.

git fetch --all

The command above fetches from all the branches in our remote repository.

Reset Changes

The next step is to rest our local repository to match our origin/master. We will use the git rest command with the --hard flag to delete unpublished commits and our local changes.

Our local repository will match the remote repository (origin/master).

git rest --hard origin/<branch-name>

In some cases, we might want to keep our local changes. How do we go about this?

Keep Current Local Changes

We can maintain our current local commits and changes by creating a new branch before resetting the local repository.

git checkout <branch-name>
git branch <create-branch-to-save-current-changes>

The command above will create a new branch where we will save all current changes in our local repository. We can now fetch and rest.

git fetch --all
git rest --hard origin/<branch-name>

Our old commits will be in the <create-branch-to-save-current-changes> branch.

Uncommitted Changes

The git rest command will delete all uncommitted changes in our local repository. We can save them and apply the commits later.

Follow these steps:

  1. Run the git stash command. The command will keep all commits elsewhere and clean your workspace.

    git stash
    
  2. You can follow the steps mentioned earlier to rest your local repository and apply the stashed commits using the git stash pop command.

    git stash pop
    

Git Pull

The git pull command fetches and merges changes from our remote repositories to the local repository. It combines the git fetch and git merge commands.

We can use the command to overwrite our local repository in the context below.

git rest --hard
git pull

We use the git clean command to remove all untracked files from our remote repository.

  1. The git clean -f command will remove untracked files.
  2. The git clean -df command will remove untracked files and folders.
  3. The git clean -xdf command will delete untracked or ignored files and directories.
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Pull