Forcing Git Pull: Overwriting Local Files Safely

Know before use !

Introduction

Once, while working on a project, I had a lot of file changes in my local repository. My usual routine is to start by pulling from the git repository before diving into work. At the end of the day or before leaving the office, I pull again and merge my changes to commit.

However, on that particular day, I missed taking the initial pull due to a busy schedule of meetings. I started working right after the meetings without realizing this.

Everything seemed fine until I took a tea break and discovered that someone had committed from my system without my knowledge. I noticed that my code editor was not displaying some files I had modified, and when I tried to revert the changes, it caused a complete mess. I encountered tree conflicts, which led me to reset my local changes.

Steps I used follow below:


Fetch all the branches.

git fetch --all
Now you have two options here to reset the branch you want at updated from remote.

git reset --hard origin/master
git reset --hard origin/<branch_name>

Explanation:


git fetch downloads the latest from remote without trying to merge or rebase anything.
Then the  git reset resets the master branch to what you just fetched. 

The  --hard option changes all the files in your working tree to match the files in origin/master


It's important to avoid creating such a mix-up, but if it does happen, this advice will definitely come in handy.

Forcing Git Pull: Overwriting Local Files Safely
Administrator October 9, 2020
Share this post
Our blogs
Sign in to leave a comment
Bun vs Node.js: A Complete Guide
Your All-in-One Solution for Speed and Efficiency