Skip to main content

Git for Authors

Section 7.1 That is So Messed Up

Suppose you have really made a mess of some file, and you do not even really know just how it happened. No problem. With a clean working directory, you can git checkout <ref> where ref is any reference you can think of to refer to a certain commit anywhere in your repository. This reference could be a branch pointer, like master or heroine, or it could be the leading digits of a commit hash that you locate in the output of git log, like 39b56cc9. This will convert the working directory to some previous state, where your file has what you want.
Now copy the acceptable version of the file in question to someplace outside of your repository. Then something like git checkout giraffes will bring you back to where you were. Now copy the file from outside the repository on top of the messed-up one. To git the changes will look indistinguisable to you typing at the keyboard to fix everything. Commit these changes in the usual way as a single changeset and get back to your writing.
Your history will contain a record of whatever mistake you made, but at least the collection of commits you had created in git allowed you to go back and re-create something acceptably close to your original work. Note that commiting frequently gives you greater latitude to pick just the right commit for the recovery.
Of course, if the damage is extensive and several files are affected, you can do the copy and replace with each one.