How do I “git pull” and overwrite my local changes?

Aram Koukia
Koukia
Published in
2 min readFeb 17, 2020

--

image from jlord.us

Has it happened to you that you have a really old git repository, that is not in sync with the remote repository and you also may or may not have some local commits, but now you want to “pull” remote changes and don’t care about your local changes?

well, one way is to delete the repo and clone again but the problem with this is, you will also lose any untracked files that exists in your git repo folder! So here is what you can do about it:

WARNING: If you have any local commits or uncommitted changes, they will be gone by doing this!

First you start with a “fetch — all” like the following

git fetch --all

Then, if you are on the “master” branch you can do the following:

git reset --hard origin/master

OR If you are on any other branch you can use the branch name like below:

git reset --hard origin/<branch_name>

How does all this work?

Well first of all 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.

--

--

Software Engineer, Engineering Leader and Manager @ AWS. Living my dream life. http://koukia.ca/