This is a guide to using Git for absolute beginners to version control for development projects, using the GitHub for Windows program and just the basics needed to integrate Git into your everyday workflow. Git is a very powerful system but even the fundamental concepts can be overwhelming at first, especially when your only question is “How do I start using version control with my current workflow?” This series of Beginner’s Guide to Git tutorials will help answer that question.
So far we’ve learned how to create a local repository and clone a remote repository. Then we started creating and modifying files in our local repository, aka the ‘Working Repository’, and perfected the art of the saving a set of changes as one unit known as a Commit.
When Last We Left Our Heroes
We’re going to pick up right where we left off on the last post, with two commits to our local repository, named ‘SharedRepository’. Here’s what our repository looks like right now:
Quite honestly, you could move on to the more advanced topics of Git like Syncing and Branching with just a knowledge of Commits. But first, let’s cover a couple handy functions GitHub for Windows has made available to you: Revert and Roll Back.
Once again, I’ll first demonstrate the concept of Revert, and then I’ll explain what it is. I’ve made a couple changes in my repository and a new commit. As you can see, I modified both files. In ‘page2.html’, I added a new line (hence the green highlight). For ‘index.html’, I modified Line 10. The red highlight with the minus sign – on the far left shows the previous version and can also represent deleted lines. The green highlight shows the new version of that line.
You can probably tell that the modified link in ‘index.html’ is not going to work anymore. When you commit several changes and then realize one of those changes broke what was previously working code, don’t you wish you had some kind of “Undo” button? Enter Revert, the “Undo” button of Git. First select the commit you want to undo, then click the Revert button located in the upper right corner. This is what happens when I click on it.
If you compare this image to the previous image, they are like mirror opposites – any line added in the previous commit is deleted. Any line deleted in the previous commit is added back. And finally a new commit, titled ‘Revert’ + the name of the reverted commit, is automatically performed.
So you can see, Revert isn’t really like an Undo button – the commit that you selected to revert doesn’t disappear into thin air. Revert performs a mirror opposite step for every change done in the selected commit. Then it creates a whole new commit chronologically listed after the last commit in the list. There is basically an audit trail of the reverts in the commit list. This is handy because you can go back and view when you did a revert.
Note that you can Revert any previously performed Commit, not just the most recent one in the list. Simply select any commit from the list before you click the button. Finally, you can revert a Revert, which basically means restore the changes that you reverted in the first place.
The other option for going “back in time” is Roll Back. There’s quite a difference between Revert and Roll Back. When you select a commit in the chronological list and Roll Back, essentially all of the changes in all of the commits made since the selected commit inclusive, are collapsed into one set of uncommitted changes on the Stage. Sound confusing? Yes. Let’s take a look.
I am going to select the second commit I made, the second one from the bottom of the chronological list.
When I click the Roll Back button in the upper right, this is the screen I see:
If you take a look at the bottom left under ‘Unsynced commits’, you will see that three commits, including the one I had selected, have disappeared; only the very first commit – the only one performed before the commit I had selected – remains. On the right-hand side, all of the changes in all three commits are listed as ‘Uncommitted changes’. Notice how unlike Revert, which creates a whole new commit but leaves the original commit in its place, Roll Back actually deletes the record of previous commits, and you lose the historical tracking for all of them. It’s like pretending those commits never happened.
So you see, although Roll Back deletes the record of commits happening, it doesn’t delete the changes from those commits. Rather, Git collects them all on the Stage, where you may discard and keep the changes as you see fit. Then you would perform a commit to solidify those choices. So you see, Roll Back can be used to gather up all the changes from several commits, fix them up, and then do one clean Commit.
(“Wait -” you may say, “I see the changes from the commit called ‘Added page 2 and link to it’, but where are the changes from the other two commits?” The third commit is just a revert of the second. So in the end, with all the commits ‘added’ together, it looks like changes from just one commit.)
If you feel that’s a lot to swallow, don’t be discouraged – really, the essential piece to Git is the Commit. The Revert and Roll Back functions simply allow you to take advantage of having a version history, or a timeline of “snapshots” of your project throughout its life. They are not too far from the ‘Undo’ or ‘Roll Back’ features in other version tracking programs, but it’s a good idea to understand exactly how these functions work in GitHub for Windows, so that you don’t get unexpected results when you click those buttons.
If you’ve fiddled with Git through the command line a bit, you might be wondering if Revert and Roll Back are commands you can execute via command line as well. Revert – yes. Roll Back – no. Roll Back is a feature in GitHub for Windows that is actually performing the native ‘reset’ command in Git. The Git Basics tutorial by Atlassian on the reset command is very helpful.
I’ve constantly been repeating this, but so far we’ve only been working in a local repository. All of our work and changes to files have been happening on our local computer. Now we’re ready to share it with the world, or at least the other members of the team. If you’ve cloned your local repository from a remote repository, you can move on to learning about Publish & Sync.