Oscar the owl and Vershd logo
Overwrite local files but keep un-pushed commits

Overwrite Local Files but Keep Un-pushed Commits

6th April 2020

This is part of a Git Tips series. For the complete set for beginner and intermediate Git users, see our white paper.

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

The Quickest & Easiest Solution

Do you need a Git GUI that's totally free for personal use? Vershd boosts your software development productivity by making Git simple with a unique UI/UX. It's a Git GUI that prevents errors and creates clarity. This Git client soars over your repository and then swoops down to quickly tackle branches, commits and files. It deals with Git, so you can concentrate on coding.

Get screenshots and a 60 second video of what Vershd, the effortless Git GUI can do for you.
Vershd is the effortless Git GUI for Windows, Mac, & Linux
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