Oscar the owl and Vershd logo

Overwrite Local Files but Keep Un-pushed Commits

6th April 2020

This is part of a Git Tips series. For the complete set for beginners 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

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram