How to Force Git Pull to Overwrite Local Files

Abdul Jabbar Feb 02, 2024
  1. Keep Local Changes
  2. Force Git Pull to Overwrite Local Files
How to Force Git Pull to Overwrite Local Files

Git is a little bit difficult for the new users if one of the first tasks for you to do some kind of pull from the Git remote repository, then we’re going to discover feasible approaches of having Git overwrite a few local files.

No doubt Git is a very effective tool for managing a team’s work. You need to recognize a few important principles to end up smartly with Git. The good thing is that when you learn them properly, you may rarely run into problems you can not get away from.

We use the local repository, a remote repository with one or multiple branches in a normal Git workflow. Repositories keep all records regarding the project, inclusive of its whole history and all of the branches. A branch is a group of changes leading from an empty project to the present state.

When you are working on a local file and introduce new changes, you need to push local changes to the remote repository, and until local changes are pushed to remote repositories, every work is available on your machine. The problem begins when a team is working on the same thing and want to make changes in the same place.

The pull is based on multiple operations, consisting of fetching data from the remote branch and then merging its changes into the local repository. Above mentioned operations can be performed manually using the following commands.

git fetch
git merge origin/$CURRENT_BRANCH

Here we have two major choices while working on the Git repositories.

Keep Local Changes

In this case, when your uncommitted changes are important for you, there are two ways to perform them:

Firstly, you can commit them and then apply git pull.

Secondly, you can stash them. Precisely, stashing can be explained as that we make a commit, but it’s not visible at that time on your current branch, but we can access it by Git. For bringing back the saved changes (that we did use stash) we will use the command git stash pop. This command will remove the stash commit after putting the stashed changes as we no longer need it.

git fetch (it will fetch the folder for the local machine)
git stash (it will stash the local changes)
git merge origin/$CURRENT_BRANCH (merge the changes from the local folder to workspace folder) 
git stash pop (it will do all stash to latest)

Force Git Pull to Overwrite Local Files

In this situation, you want to release all the uncommitted local changes. Sometimes, you modify a file just for an experiment, but after that, you realize that you don’t want that change. Then, all you want is to update it to upstream.

This will add one more step in this operation between fetching and merging. Hence it will reset the branch to its original state, thus allowing git merge to work.

git fetch (fetch the local machine folder)
git reset --hard HEAD (it will remove the local changes)
git merge origin/$CURRENT_BRANCH (merge the changes from the local folder to workspace folder)
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Pull