Using Simple Git Commands to Tame Website Updates
In my last post, I talked in general about how I use Git to provide version control for my website. In this post, I’ll describe the simple Git commands I use to manage changes to my website.
Simple Example: Edit a File
I have a master version of my website that I push via FTP to a web server. I also have a development version of my website that I use to test changes before deciding to take them live. Git manages those versions seamlessly so I can concentrate on the changes. Here is a typical exchange:
$ git checkout development ⇒ Tells Git I want to work on the development version of my website
Next I make changes to my files. For example, I fixed a typo in image017.jpg. Git now recognizes image017.jpg has changed. NOTE: This change is not yet associated with the development version of my website. See git add command.
$ git add image017.jpg ⇒ Git gets ready to add the updated image017 file to the development version of my website by adding it to a staging area. You can collect several related changes in the staging area before committing them as a change Git will track in the log file.
git commit –m “Fixed Typo in image017.jpg Image Process.” ⇒ Takes all changes in the staging area and commits them, making them part of the development version of my website. In this case, it’s just one file.
Now, I can test the changes. If I’m happy with them, the next step is to move the change to my master copy.
$ git checkout master ⇒ This command tells Git I now want to work with my master copy. (The one I use to update my live website.)
$ git merge development ⇒ Tells Git to take all the development version changes and add them to the master version.
Done! I’m ready to push the changes via FTP to my live website.
$ git log –oneline ⇒ The log file shows the changes I’ve made to the master version of my website. Each committed change has an alphanumeric identifier. Now the log file includes this message:
4fd9061 Fixes typo in image017.jpg Import Process
More Complex Example: Switching Between Versions
The example I used at the end of my last post (stop working on the development copy to fix an issue with the live site) would look like this:
$ git checkout master ⇒ Switches to my master copy.
$ git branch fix_issue ⇒ Creates a new copy of master (AKA, a new branch) called fix_issue.
$ git checkout fix_issue ⇒Switches to my fix_issue copy.
Make the file changes in the fix_issue copy.
$ git add <changed files> ⇒Adds the changes to the fix_issue staging area.
$ git commit -m "Write a good commit message here" ⇒ Makes changes part of the fix_issue copy.
$ git checkout master ⇒ Switches to the master copy.
$ git merge fix_issue ⇒ Adds the changes from fix_issue to the master copy.
Final test and push changes via FTP to web server.
$ git checkout development ⇒ Switches back to development branch, so I can continue working.
Although I decided on Git website version control primarily to become more comfortable with Git, it didn’t take long for me to see the benefits, even with a local files. (I haven’t even touched on the benefits of version control when your are collaborating on a project.) Of course, people most often use Git to contribute to open-source projects by interacting with remote repositories, and you can even manage your website that way. But, for now, I’ll continue updating my website using the approach I outlined above. Using Git for version control frees me up to make changes without fear, while improving my Git skills.