Day 6: Stashing and Tagging in Git

Brief Overview
On Day 6, we will cover:
Understanding Git stashing.
Stashing changes.
Applying and dropping stashes.
Understanding Git tagging.
Creating and listing tags.
Pushing tags to a remote repository.
Detailed Explanation with Code
Understanding Git Stashing:
Stashing in Git allows you to temporarily save changes that are not ready to be committed. It is useful when you need to switch branches or work on something else without committing unfinished work.
Stashing Changes:
Stash Uncommitted Changes: Use the
git stashcommand to save uncommitted changes.git stashExample:
echo "Temporary change" >> file.txt git stashOutput:
Saved working directory and index state WIP on main: a1b2c3d Initial commitList Stashes: Use the
git stash listcommand to view all stashes.git stash listExample:
git stash listOutput:
stash@{0}: WIP on main: a1b2c3d Initial commit
Applying and Dropping Stashes:
Apply a Stash: Use the
git stash applycommand to reapply stashed changes. This command keeps the stash in the list.git stash applyExample:
git stash apply stash@{0}Pop a Stash: Use the
git stash popcommand to reapply stashed changes and remove the stash from the list.git stash popExample:
git stash pop stash@{0}Drop a Stash: Use the
git stash dropcommand to remove a stash from the list.git stash dropExample:
git stash drop stash@{0}
Understanding Git Tagging:
Tags in Git are used to mark specific points in history as important. Typically, tags are used to mark release points (e.g.,
v1.0,v2.0).Creating and Listing Tags:
Creating a Lightweight Tag: A lightweight tag is simply a named pointer to a commit.
git tag <tag-name>Example:
git tag v1.0Creating an Annotated Tag: An annotated tag is stored as a full object in the Git database and contains additional metadata, such as the tagger's name, email, date, and a tagging message.
git tag -a <tag-name> -m "Tag message"Example:
git tag -a v1.0 -m "Initial release"Listing Tags: Use the
git tagcommand to list all tags.git tagExample:
git tagOutput:
v1.0
Pushing Tags to a Remote Repository:
Push a Specific Tag: Use the
git push origin <tag-name>command to push a specific tag to the remote repository.git push origin <tag-name>Example:
git push origin v1.0Push All Tags: Use the
git push origin --tagscommand to push all tags to the remote repository.git push origin --tags
Example Workflow
Initialize a New Git Repository:
mkdir my-git-project cd my-git-project git initCreate and Commit a File:
echo "Hello, Git!" > file.txt git add file.txt git commit -m "Initial commit"Make Changes and Stash Them:
echo "Temporary change" >> file.txt git stashList Stashes:
git stash listApply and Drop a Stash:
git stash apply stash@{0} git stash drop stash@{0}Create and List Tags:
git tag v1.0 git tag -a v1.0 -m "Initial release" git tagPush Tags to a Remote Repository:
git remote add origin https://github.com/username/repository-name.git git push origin v1.0 git push origin --tags
By the end of Day 6, we are able to understand how to stash changes temporarily and create, list, and push tags in Git. So stay tuned for further topics on Git & Github.




