Git tags to the rescue!Git tags are a great way to version your coding tutorials to make your life, and your readers’ lives, easier.
They allow you to tag specific points in a repository’s history and return to them at later points in time.
First, let’s check out the basics of creating, listing, checking out, and deleting tags.
List Existing TagsListing your Git tags is as simple as typing git tag followed by an optional -l or –list parameter.
This command will list all tags in alphabetical order.
Additionally, you can list all tags which match a certain structure.
For example, if I want to list all tags that begin with v12, I can type git tag -l "v12.
*" and this might output something like this:$ git tag -l "v12.
3Note: If you want to use the wildcard (*) to match specific tag patterns, you must include the –list (or -l) parameter.
Creating A TagYou can create two types of tags: lightweight and annotated.
Lightweight TagsA lightweight tag is similar to a branch; it’s just a pointer to a specific commit.
To create a lightweight tag, all you need to provide is a tag name.
You don’t need to include any of the flags you would see with annotated tags (see below).
$ git tag v2.
4-lightweight$ git tagv0.
4-lightweightIf we run git show on this tag, we simply see the commit hash, the author, the date, and the commit message.
$ git show v2.
4-lightweightcommit ca82a6dff817ec66f44342007202690a93763949Author: Emma Wedekind <wedekind.
com>Date: Sat Mar 16 09:48:93 2918 -0700 changed the version numberAnnotated TagsAn annotated tag, in contrast, is stored as a full object in the Git database.
These tags are checksummed; they contain the name of the person who created the tag, email, and date.
Annotated tags include a message and can be verified with a GNU Privacy Guard (GPG).
To create an annotated flag, you can use the -a flag.
$ git tag -a v2.
4 -m "This is my 2.
4 version"$ git tagv0.
4The -m flag is followed by the tagging message you want to include.
If you don’t include this flag, Git will launch your code editor for you to include it.
We can use the git show command to view the tag data.
$ git show v2.
4Tagger: Emma Wedekind <wedekind.
com>Date: Sat Mar 16 09:48:93 2918 -0700This is my 2.
4 versioncommit ca82a6dff817ec66f44342007202690a93763949Author: Kai Wedekind <wedekind.
com>Date: Thursday Mar 14 19:38:11 2918 -0700 changed the version numberPushing Tags To Remote ServersUsing the git push command won’t push the tags you’ve created; you must explicitly push each tag after they’ve been created.
This process is similar to sharing remote branches.
$ git push origin v2.
4Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.
05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)To git@github.
git * [new tag] v2.
4 -> v2.
4To push multiple tags, you can use the –tags option to the push command.
$ git push origin –tagsDeleting TagsYou can use the git tag -d <tagname> command to delete a tag from your local repository.
$ git tag -d v2.
4-lightweightDeleted tag 'v2.
4-leightweight' (was e7d6add)This command, however, doesn’t remove the tag from your remote servers.
To complete this action you can either use the git push <remote> :refs/tags/<tagname> command:$ git push origin :refs/tags/v2.
git – [deleted] v2.
4-lightweightYou can additionally use the git push origin –delete <tagname> command:$ git push origin –delete v2.
4-lightweightChecking Out TagsTo check out a tag, you can use the git checkout command.
$ git checkout v2.
4-lightweightNote: checking out 'v2.
You are in 'detached HEAD' state.
You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again.
Example: git checkout -b <new-branch>HEAD is now at 99ada87.
Merge pull request #89 from emmawedekind/readme-addition$ git checkout 2.
1Previous HEAD position was 99ada87.
Merge pull request #89 from emmawedekind/readme-additionHEAD is now at df3f601.
add getting started infoGreat!.Now that we know the basics of tagging, let’s use it to version one of our coding tutorials.
Project StructureLet’s suppose I’m creating a tutorial called “Web Development For Beginners.
In order to preserve my project history at each milestone, and be able to quickly switch between the different histories, I want to create three tags: html, css, js.
Milestone 1: HTMLCreate a GitHub repository where you will keep your coding tutorial code.
You can find my code here.
Clone the repository.
Add an HTML template with some elements.
Now, let’s add and commit our HTML code.
$ git add index.
html$ git commit -m "Adding the HTML structure"5.
Next, let’s add a tag to our HTML milestone so we can re-visit this code in the future.
In the terminal, type the following command:git tag -a html -m "The HTML code for this project"Note: You must commit your code before creating the tag.
Now, when you run git tag, you should see the name of your tag.
We need to push the code and the tag to the remote server.
Remember that tags aren’t included in the git push command; we have to use the git push <origin> <tagname> command.
Let’s push our code first.
$ git pushGreat!.If we head back to GitHub, we can see our code has been pushed to master.
Now, let’s push our tag name to GitHub.
$ git push origin htmlOnce this completes, let’s refresh GitHub once more.
Now, we should see the tag appear under the Releases tab.
So for brevity’s sake, I’ll quickly run through the process.
Create a stylesheet.
I’ll call mine styles.
Add some styling to the page.
You can use class names, but to keep my tutorial short, I will just style the base HTML elements.
Additionally, I added a <section> element to wrap the main content so I could style it appropriately.
You can view the full code here.
Link the stylesheet in the head of the index.
html file<link rel="stylesheet" href=".
Add and commit your code.
You can refer to step four in milestone one for more details.
Now it’s time to tag!git tag -a css -m "The CSS code for this project"Now when we run git tag, we should see two tags: html and css.
Push your code and your tag to GitHub$ git push$ git push origin css7.
Double check that your tag and your code have been successfully pushed to the repository.
For the sake of not repeating myself, I’m going to skip the steps we followed in milestones one and two.
But here’s a quick overview of the coded changes I made:Create an index.
Link the index.
js file in my index.
Add a <button> element to the template.
Attach an event listener to the button which will trigger an alert when clicked.
Add some styling to the button.
Add and commit your code.
Create the js tag.
Push all code and the tag to the GitHub repository.
And just to double check everything was pushed to the repository…Great!.Now let’s learn how to check out the different branches to view the milestone code.
Checking Out MilestonesDuring your coding tutorial, you may want to reference specific snapshots of code.
For example, if I’m referencing the code I wrote in the HTML milestone, I want to instruct students to check out the html tag.
You can do this in the following way:$ git checkout htmlYour terminal output should look something like the following.
And when we refresh the browser, you should see the code without any styling or behavior.
Now, let’s check out the CSS tag with git checkout css.
Refreshing the browser, we see the following.
Great!.It’s all working as expected!Viewing Milestones In GitHubIt’s also possible to view the milestones within GitHub.
Simply click the Switch Branches drop down, and select tags.
Now, you can select the tag you want to view the code for.
It’s as simple as that!.I hope this tutorial proved useful and gave you the skills you need to create tags in your coding tutorials.