How to recover an older version of your code with Git and Beyond Compare
Last updated
Last updated
Git is a safety net. If you commit frequently you can also commit fearlessly: anything you break in your code can be rolled back to a recent commit.
Git tags each old commit with a long hash number. To look at any old commit, you just copy and paste the first dozen digits into your command line syntax.
We are going to open up a window that shows my current file on one side and an older commit on the other side.
I'm going to do it wrong twice and then do it correctly.
Wrong. You can leave the file unspecified in your command line arguments. Git may open up the wrong file. Or may open up multiple files.
Better. I have opened up the plain old diff of just my broken code. If your change is more than a line or two, this will be hard to read.
Correct. I have used the syntax git difftool -- filename
. Instead of diff we say difftool. Assuming we have previously specified that we want Git to use Beyond Compare, this will open our files up in BC. Also in Git, the double dash syntax is like a colon, it emphasizes 'do-the-previous-verb-to-the-following-noun'.
You have now opened up a TextCompare session. Use your mouse and instincts to edit your current file on the tree. In this screenshot, it is the panel on the right (notice its address in the top white address bar). Don't edit the old commit on the other side! (The potency of Beyond Compare unfortunately allows you to go back and rewrite your saved snapshots -- very Orwellian!).
Once your editing is finished and you are satisfied, press the save button here and then in the terminal, do a commit, e.g.
git add myfile.txt
git commit -m "Fix bug in code.