Oscar the owl and Vershd logo

How to Overwrite Local Files Not Git Commits Effortlessly

The Problem

Cuthbert made some commits, but instead of committing the relevant diagrams into the commit, he sent them to you by carrier pigeon. What a character.

You then copied them to where they’re supposed to be in the repository and committed them. But when he went to pull your commit down, Git refused, saying that untracked working files (his original diagrams) would be overwritten by the merge. He’s stuck, and spends his time gazing out the window instead of working.

In this example, you have committed the diagrams to the fun-times branch, and Cuthbert needs to store any local un-pushed commits so he doesn't lose them. Then replace his local diagrams with your committed ones.

The Solution

This makes fun-times the current branch:
git checkout fun-times
To maintain any recent commits he has made but not yet pushed, or keep some local files, Cuthbert needs to take further action first of all. Otherwise he will lose all of those local commits.

Option 1: This command replicates fun-times to a new branch, fun-times-local, without changing the current branch. So it is a branch of his local, un-pushed commits. This allows him to change or push them later on:
git branch fun-times-local
Option 2: This keeps any local files by stashing them, so they are not affected by the reset:
git stash
This brings him all the latest data from the remote repository, but makes no alterations to his files:
git fetch --all
This replaces all his local files and index with those from the remote origin/fun-times branch:
git reset --hard origin/fun-times
Option 2 Resolution: This pops (re-instates) the stash of any local files that were stashed before in option 2:
git stash pop

Zero stress Git

Vershd is an effortless Git GUI that boosts software development. It works on Windows, macOS, and Linux. It's free for personal, educational, non-profit and trade union use.

Designed for intermediate and beginner Git users, our unique UI/UX boosts your software development and makes Git simple.

Help & tools

We provide these pages to try to make your programming life easier. Our resources page gives an overview. You can see a full list of our Git tips & tools here.

We have these free tools:

Customers say...

I love any tool that makes my life easier... This is perfect, just the right amount of control. No more, no less. Easy to get started, push and rollback changes... It's a no brainer!
Hayden T.
Oscar the owl and Vershd logo
 © 2021 Blightysoft
Made in England
St. George's flag of England, the Union Jack flag of the United Kingdom, the European Union flag, and the United Nations flag.
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram