- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Git Configuration, Tagging, and Releases
Git is an ubiquitous source code management tool used worldwide. It's likely that, if you're writing code, you're using Git. In this Guided Code Lab, you’ll configure Git to automatically apply your username and email to Git commits, use tagging and releases to mark the latest commit of our repo, and create a new git commit, tag it, then mark that tag as a release. When you're finished, you'll be ready to optimize your Git workflow in order to save time and stay organized every day.
Lab Info
Table of Contents
-
Challenge
Configuring Username and Email
Git is a collaborative tool - it's meant to help large groups of people, possibly separated by large distances or even spans of time, work together to build stable, maintainable, and useful software.
That's why it's essential that all your commits are signed with your name and email. This lets others working on the project know who contributed that amazing code (or, occasionally, that error-prone code!)
Luckily, you don't need to specify this information every time you make a commit. For your first task, you'll configure git to automatically use your preferred username and email.
Configuring Your Username and Email
Using the Terminal, you can configure default values for username and email when creating git commits. This is essential as having these values set is often required before Git will allow you to commit changes. Now when you make changes to your files, you'll see the commit you entered, with the username and email you just entered credited to the changes it contains.
-
Challenge
Initializing the Repo and Adding a File
Before beginning, you'll initialize the Git repository, which will cause Git to recognize your working directory as a Git repo and allow you to use commands such as
git addon the files in the directory.A Git repository isn't much use without any files to track and manage, so after you initialize the Git repository, you will add one file to it.
-
Challenge
Configuring Your Text Editor
Git will often open a text editor to perform common tasks such as merging changes or applying a commit message.
However, you're not limited to using the default text editor provided by Git. You can use whichever text editor best suits your workflow, changing it as often as the task demands.
In short, Git can be customized to use the text editor of your choice. This allows you to use the same editor you use for editing code to edit commit messages.
To change your editor, input the following command in the Terminal:
git config --global core.editor <path-to-my-editor>The value you input can be the alias of an editor, like vi, or a full path.
Commonly used editors include Sublime Text, Vim and Notepad++.
In the following task, you'll set your text editor to Vim.
-
Challenge
Configuring Commit Template
Git provides tools for teams of developers to collaborate in an orderly and efficient manner. One way this is possible is by configuring a commit template.
When a commit template is specified, it will provide a custom structure for commit messages, allowing all developers to communicate with a unified voice.
In the following step, you'll create a custom commit message. Look to see the new template in action. First, add the new file to the git repo:
git add -A .You'll now commit the code, but without specifying a message:
git commitThe text editor Vim will open, displaying the contents of your commit template!
To exit Vim without saving the commit message, press Esc, type
:q!, and press Enter. -
Challenge
Associating Tags with Git Commits and Reviewing Tags
As you've learned, Git is about more than saving your source code. It has many tools to help you stay organized!
One tool for staying organized is a
tag. Tags are special labels that can be applied to commits.Understanding commit hashes
Git identifies commits using a unique hash, commonly called a SHA. By default, the
git logcommand displays the complete commit hash, such as the following:61849e8472b358b801b7e5e92cc8ef627ccb3c04To view an abbreviated commit hash, run:
git log -1 --onelineThis displays the abbreviated hash of the most recent commit along with its commit message. For example:
61849e8 Initial commitThe abbreviated hash may vary in length and can be used to reference a commit, provided it uniquely identifies that commit.
Note how this SHA is not very personalized and can be hard to remember.
However, by using a tag, you can access this commit much more easily.
Lightweight vs. annotated tags
Git supports two main types of tags:
- Lightweight tags: Simple references to a specific commit. They do not store additional information, such as a tag message or tagger details.
- Annotated tags: Store additional information, including the tagger's name, email, date, and a message. They can also be digitally signed.
Annotated tags are generally recommended for releases because they preserve additional information about the tagged version.
In this lab, you'll create annotated tags using the
-aoption and provide a message using the-moption.In the following task, you'll tag a Git commit. Now type
git tag, and you should see your newly created tag.You can now checkout this commit at any time by referencing it by it's tag name. For now, you'll leave the repo checked out to the commit it's currently on.
-
Challenge
Accessing Commits via Tags
You can use tags to conveniently check Git code out to the commit associated with any tag.
This is a great way of resetting the state of a codebase to known, working state in order to troubleshoot bugs, or just get the system running again.
In the following task, you'll check out code by referring to its tag.
-
Challenge
Marking Tags as Releases
Sometimes, it's necessary to mark a Git commit as one that's especially important or stable. For that, you can use a tag to identify a release.
Naming release tags
Git does not require a specific naming convention for release tags. You can choose a naming scheme that works for your project, such as
v1.0.0orrelease-v1.0.0.In this lab, you'll use the
release-prefix to distinguish release tags from other tags. This is a naming convention for the exercise, not a special Git feature.Note: For this lab, use the exact tag name
release-v0.0.1as instructed in the task so your work can be validated.Git tags vs. GitHub Releases
Creating a Git tag does not automatically create a GitHub Release. GitHub Releases are a separate feature that allows you to publish release notes and attach downloadable assets to a specific tag.
In the following task, you'll create an annotated tag to identify a release. Now type
git tag. You should see your new release among the tags listed.Congratulations on completing this lab! Great job! You're now familiar with git tagging and configuration.
When you use Git in your day-to-day workplace tasks, you can complete tasks more confidently, using your text editor of choice, and marking important commits with tags and releases.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.