Day 6: Stashing and Tagging in Git

Day 6: Stashing and Tagging in Git

Brief Overview

On Day 6, we will cover:

  1. Understanding Git stashing.

  2. Stashing changes.

  3. Applying and dropping stashes.

  4. Understanding Git tagging.

  5. Creating and listing tags.

  6. Pushing tags to a remote repository.

Detailed Explanation with Code

  1. 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.

  2. 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
      
  3. 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}
      
  4. 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).

  5. 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
      
  6. 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

  1. Initialize a New Git Repository:

     mkdir my-git-project
     cd my-git-project
     git init
    
  2. Create and Commit a File:

     echo "Hello, Git!" > file.txt
     git add file.txt
     git commit -m "Initial commit"
    
  3. Make Changes and Stash Them:

     echo "Temporary change" >> file.txt
     git stash
    
  4. List Stashes:

     git stash list
    
  5. Apply and Drop a Stash:

     git stash apply stash@{0}
     git stash drop stash@{0}
    
  6. Create and List Tags:

     git tag v1.0
     git tag -a v1.0 -m "Initial release"
     git tag
    
  7. 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.