Throwing away local changes in Git workspace, allowing 'git pull' to proceed

By: (plus.google.com) +David Herron; Date: August 8, 2017

Tags: Git

Git is a powerful source code revision control system, letting teams large and small track changes between team members. It is widely used and very flexible. However, it's easy to get into an inscrutable state where it's not clear how to proceed.

Just now I found myself wishing to update my local repository with changes from co-workers. The repository contains UML models designed with Enterprise Architect, hence the primary thing in the repository is a large binary file. I'd been running Enterprise Architect on my local file, and there must have been some changes made to that file. As such Git told me there were local changes which I must merge before I could fetch the upstream changes. (specifically: Your local changes to the following files would be overwritten by merge) But I knew any change that had been made was unimportant, unneeded, and should be tossed away. Leaving me wondering how to throw away changes to a local Git repository.

Here's the problem:

$ git pull
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 2), reused 6 (delta 2), pack-reused 0
Unpacking objects: 100% (6/6), done.
From github.com:ORGANIZATION/REPOSITORY
   add0c66..2d8a40d  master     -> origin/master
Updating add0c66..2d8a40d
error: Your local changes to the following files would be overwritten by merge:
	MODEL_FILE_NAME.eap
Please commit your changes or stash them before you merge.
Aborting

I have used Enterprise Architect on a local .eap file to browse the UML models being designed by co-workers. I am not the person to be making changes, that is the responsibility of those co-workers, and therefore any change I make should be thrown away. But I do need to use Enterprise Architect to browse the UML models.

As is common with desktop applications, simple use of the application to READ the file will often cause the application to make changes to the file it is READING. That's crazy but there are plenty of applications where this is true.

That must be what has happened this time -- somehow clicking on something, even though I didn't change anything, must have caused the file to register some kind of change.

Whatever the reason, Git is believing the file has changes. Git is treating those changes as if they're important and giving me valuable advice on how to preserve those changes. Indeed there are many situations where I WOULD want to save local changes. What I would do in such a case is to commit local changes, for example, so that a git merge command could work.

Instead, in this case I do not want to preserve local changes.

My first thought was to use git revert. While that's a useful command to learn, its effect is to undo certain commits in your working tree. That isn't what is needed in this case.

Instead what's required is the git reset command. The documentation isn't all that clear, but this command does the trick:

$ git reset --hard HEAD
HEAD is now at add0c66 Minor changes to modules properties

What this does is reset the working tree to match the named commit. HEAD is a magic value that refers to the most recent commit.

Once you've run git reset, you can now do:

$ git pull
Updating add0c66..2d8a40d
Fast-forward
 MODEL_FILE_NAME.eap | Bin 2297856 -> 2885632 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)

Because this is a "Fast-Forward" action it demonstrates that the local file had zero changes from the repository and Git could do a fast merge.

« What does TARDIS stand for? Five years of Mars exploration with NASA's Curiosity Rover »
2016 Election Acer C720 Ad block AkashaCMS Amazon Amazon Kindle Amiga Android Anti-Fascism AntiVirus Software Apple Apple Hardware History Apple iPhone Apple iPhone Hardware April 1st Arduino ARM Compilation Astronomy Asynchronous Programming Authoritarianism Automated Social Posting Ayo.JS Bells Law Big Brother Big Finish Bitcoin Mining Black Holes Blade Runner Blockchain Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks Chromebox ChromeOS CIA CitiCards Citizen Journalism Civil Liberties Clinton Cluster Computing Command Line Tools Comment Systems Computer Hardware Computer Repair Computers Cross Compilation Crouton Cryptocurrency Curiosity Rover Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Developers Editors Digital Photography Diskless Booting Disqus DIY DIY Repair DNP3 Do it yourself Docker Docker MAMP Docker Swarm Doctor Who Doctor Who Paradox Drobo Drupal Drupal Themes DVD E-Books E-Readers Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Encabulators Energy Efficiency Enterprise Node EPUB ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Facebook Fake News Fedora VirtualBox File transfer without iTunes FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Heat Loss Hibernate Hoax Science Home Automation HTTPS Human ID I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick In-memory Computing InfluxDB Infrared Thermometers Insulation Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man Iternet of Things iTunes Java JavaScript JavaScript Injection JDBC John Simms Journalism Joyent Kaspersky Labs Kindle Kindle Marketplace Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac OS Mac OS X Machine Readable ID macOS MacOS X setup Make Money Online MariaDB Mars Matt Lucas MEADS Anti-Missile Mercurial Michele Gomez Micro Apartments Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications MODBUS Mondas MongoDB Mongoose Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole NASA Net Neutrality Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea npm NY Times Online advertising Online Community Online Fraud Online Journalism Online Video Open Media Vault Open Source Governance Open Source Licenses Open Source Software OpenAPI OpenVPN Paywalls Personal Flight Peter Capaldi Photography PHP Plex Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Refurbished Computers Remote Desktop Republicans Retro-Technology Reviews Right to Repair River Song Robotics Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Russian Hacking SCADA Scheme Science Fiction Search Engine Ranking Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Shell Scripts Silence Simsimi Skype Social Media Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spear Phishing Spring Spring Boot SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Trump Campaign Ubuntu UDOO Virtual Private Networks VirtualBox VLC VNC VOIP Web Applications Web Developer Resources Web Development Web Development Tools Web Marketing Website Advertising Weeping Angels WhatsApp Window Insulation Wordpress YouTube YouTube Monetization