Module 5 – Storing Additional Information in Your Repository
In the vast world of Git, we’re not limited to merely tracking code changes. Git also provides features that enable us to store and manage additional information to enhance our development workflow. These could be temporary changes, tags for specific commits, annotations to commits, or files that we prefer Git to ignore.
In this module, we will explore these powerful tools that can streamline your workflow and increase your productivity as a developer. These tools not only save you from potential mess but also equip you with effective practices to keep your repository clean and understandable.
We will cover:
- 5.1 Git Stashing
- 5.2 Git Ignore
- 5.3 Git Tags
- 5.4 Git Notes
By the end of this module, you’ll be able to effectively manage your work using stashes, mark specific points in your project with tags, control what Git should ignore via
.gitignore, and annotate your commits using notes. Let’s delve deeper into how you can enhance your Git experience by storing additional information in your repository.
Git stash is a powerful command that allows you to temporarily save changes that you don’t want to commit immediately.
Creating a Stash: The
git stashcommand takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.
Applying a Stash: The
git stash applycommand re-apply the changes recorded in the stash to your current working directory.
Deleting a Stash: The
git stash dropcommand discards the most recent stash.
Stashing Untracked Files: The
git stash -uor
git stash --include-untrackedcommand stashes the untracked files.
.gitignore file is a text file that tells Git which files or folders to ignore in a project.
Creating .gitignore File: You can create a
.gitignorefile in your project’s root directory where you list all the files or directories you want Git to ignore.
Common Ignored Files: Generally, you would want to ignore files that are autogenerated by your system like log files, .pyc files, .DS_Store files etc.
Tags in Git are a way to bookmark specific commits. This is particularly useful for marking release points.
Creating Tags: The
git tag [tag-name]command creates a lightweight tag that just points to a certain commit.
Annotated Tags: The
git tag -a [tag-name]command creates an annotated tag which is stored as a full object in the Git database containing the tagger name, email, and date.
Listing Tags: The
git tagcommand, when used without any arguments, lists all the tags in your repository.
Git Notes is a way of adding additional information to a commit without altering the commit itself.
Adding Notes: The
git notes addcommand adds a note to the current commit.
Showing Notes: The
git notes showcommand displays the note attached to the current commit.
Exercises and Practice
Exercise 1: Make changes to a Git repository, then stash the changes. Verify the changes were stashed, then apply the stash and verify the changes have been re-applied.
Exercise 2: Create a
.gitignore file and add some entries. Make changes to the ignored files and verify that Git does not track the changes.
Exercise 3: Create several tags in a Git repository, both lightweight and annotated. View the tag information with
Exercise 4: Add a note to a commit, then display the note with
git notes show.
Exercise 5: Practice managing multiple stashes. Make changes and create a stash. Then, make different changes and create a second stash. Use
git stash list to view your stashes. Apply each stash and observe the changes.
Exercise 6: Test the
git stash pop command. Make some changes and stash them. Then, use
git stash pop to apply the stash and remove it from the stash list. Verify the changes and the stash list.
Exercise 7: Create a
.gitignore file with several entries. Then, deliberately violate your own rules—create files that should be ignored—and observe how Git respects the
Exercise 8: Create a lightweight tag, then turn it into an annotated tag using the
git tag -a [tag-name] command with the same tag name.
Exercise 9: Add a note to a commit, then amend the note using
git notes edit.
Exercise 10: Experiment with showing notes automatically in
git log. Use
git config --global notes.displayRef "refs/notes/commits" to configure Git to show notes in the log, then view the log.
These exercises will provide additional experience with stashing, ignoring, tagging, and annotating commits. They will help solidify your understanding and command of Git, making it an even more effective tool for your software development projects.
Rebase Regularly and Interactively