Table of contents
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 stash
command to save uncommitted changes.git stash
Example:
echo "Temporary change" >> file.txt git stash
Output:
Saved working directory and index state WIP on main: a1b2c3d Initial commit
List Stashes: Use the
git stash list
command to view all stashes.git stash list
Example:
git stash list
Output:
stash@{0}: WIP on main: a1b2c3d Initial commit
Applying and Dropping Stashes:
Apply a Stash: Use the
git stash apply
command to reapply stashed changes. This command keeps the stash in the list.git stash apply
Example:
git stash apply stash@{0}
Pop a Stash: Use the
git stash pop
command to reapply stashed changes and remove the stash from the list.git stash pop
Example:
git stash pop stash@{0}
Drop a Stash: Use the
git stash drop
command to remove a stash from the list.git stash drop
Example:
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.0
Creating 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 tag
command to list all tags.git tag
Example:
git tag
Output:
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.0
Push All Tags: Use the
git push origin --tags
command 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 init
Create 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 stash
List Stashes:
git stash list
Apply 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 tag
Push 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.