Git & GitHub Tutorial

Rahul's Profile

Git Tag

In Git, a tag acts as a reference point for a specific commit, similar to a bookmark or label. Tags are primarily used to identify key moments in a project's timeline, such as version releases (e.g., v1.0 or v2.0). They provide a straightforward method for tracking different versions and making them easily accessible to your team or users.

Lightweight Tag

A lightweight tag in Git is essentially a label assigned to a specific commit, serving as a quick reference without any additional metadata. It’s straightforward and efficient but does not retain extra details beyond the commit itself. You can create a lightweight tag as follows:


                        git tag v1.0
                    

Annotated Tag

An annotated tag in Git is a more detailed reference to a specific commit, containing additional metadata beyond just the commit itself. Unlike lightweight tags, annotated tags store extra information such as the tagger's name, email, date, and an optional message, making them useful for version tracking and release management.

  • Metadata Storage: Includes details about the tag creator, timestamp, and an associated message.
  • Permanent Record: Stored in Git's database as a separate object, ensuring long-term traceability.
  • Verification Support: Can be signed with GPG keys for authentication and security.
  • Useful for Releases: Commonly used to tag official versions of a project, making it easy to reference specific releases

                        git tag -a v1.0 -m "Release Version v1.0"
                    

Since annotated tags provide richer context and verification, they are ideal for maintaining a structured versioning system within a Git repository.

Tag Using Hash

Git allows tagging a commit directly using its hash, which is helpful when you want to retroactively mark a specific commit as important or as a release point. This is especially useful when the commit is not the latest one.

  • Direct Tagging: You can create a tag on any commit using its SHA-1 hash value.
  • Flexibility: Ideal for marking historical commits or specific milestones.
  • Annotated or Lightweight: Both types of tags can be used with hashes.
  • Useful in CI/CD: Commonly used when referencing build-specific commits.

    git tag -a v1.1 abc1234 -m "Tagging specific commit"
  

Tagging using a hash gives you more control over version marking, especially in scenarios where a tag needs to reference past work.

List Tags

Listing tags in a Git repository is useful for viewing all available versions or checkpoints marked within your project. It helps you understand the progression and history of your releases or important states.

  • All Tags: The basic command lists every tag created in the repository.
  • Filter Support: You can use patterns to limit the output, such as listing only version 2 tags.
  • Helpful in Releases: Useful in pipelines and release documentation to identify available versions.
  • Easy Review: Gives a quick snapshot of major checkpoints.

    git tag
  

Use listing to review and manage all tags, ensuring that important stages in your project are properly recorded and visible.

Delete Tags

Sometimes you may need to delete a tag either due to incorrect versioning or obsolete checkpoints. Git makes it easy to remove tags locally and remotely when needed.

  • Local Deletion: Removes the tag from your local repository.
  • Remote Cleanup: Tags pushed to remotes need separate deletion.
  • Version Correction: Helpful when mistakenly tagging the wrong commit.
  • Safe Operation: Deletion does not affect commit history.

    git tag -d v1.0
  

Deleting tags ensures that your versioning stays accurate and relevant, avoiding confusion in shared repositories.

Push Tags

After creating tags locally, you may want to share them with collaborators or CI/CD systems. Git supports pushing individual tags or all tags to a remote repository.

  • Single Tag Push: Allows targeted tag publishing to remotes.
  • All Tags: Useful for bulk operations and version publishing.
  • Version Sharing: Makes tags visible to all contributors.
  • CI/CD Integration: Often used to trigger builds or releases in pipelines.

    git push origin v1.0
  

Pushing tags ensures synchronization across all team members and environments, making collaboration and version control more reliable.

Signed Tag

A signed tag in Git is similar to an annotated tag but includes a GPG or SSH signature for verification. This ensures that the tag was created by a trusted source, adding security and authenticity to version tracking.

  • Cryptographic Signature: Uses GPG keys to verify the identity of the tag creator.
  • Secure Releases: Essential for open-source projects and official versioning.
  • Traceable: Combines metadata with digital signature for strong audit trails.
  • Requires Configuration: You must set up and configure GPG keys locally before use.

    git tag -s v2.0 -m "Signed release version v2.0"
  

Signed tags provide an extra layer of trust, especially when publishing code to public repositories or handling sensitive release workflows.

Git Stash

What is Git Stash?

Git Stash helps you temporarily set aside uncommitted changes in your working directory, so you can switch tasks or branches without committing half-done work. This gives you a clean slate to handle urgent issues or explore other features without losing progress.

  • Switch Branches Safely: Store your work-in-progress before moving to another branch.
  • Handle Critical Fixes: Pause your current task and return to it later.
  • Avoid Incomplete Commits: Keeps your commit history clean and meaningful.

Stash Your Changes

You can stash your current modifications—both staged and unstaged tracked files—using the basic stash command. This action clears your workspace, allowing for safe context-switching.

  • Tracked Files Only: By default, Git stashes only tracked files.
  • Exclude Untracked Files: New files not yet added with git add are not included unless specified.
  • Include Untracked: Use -u or --include-untracked to stash them too.

    git stash           # Stash tracked changes
    git stash -u        # Include untracked files
    

This clears your working directory and places your changes onto a stack, ready to be reapplied later.

Stash Stack

Every time you stash changes, Git adds them to a stack. The latest stash sits on top, and you can retrieve or discard any stash by referencing its position in the list.

  • LIFO System: The most recent stash is always on top.
  • Indexed: Each stash is tagged as stash@{0}, stash@{1}, etc.

Stash with a Message

You can add a custom message to your stash to identify what changes it contains. This helps when you're juggling multiple work streams.


    git stash push -m "WIP: homepage redesign"
    

This message appears in your stash list, making it easier to manage your stashes later.

List All Stashes

View all the stashed changes in your project using the following command:


    git stash list
    

Git will display each stash with an index and the message you added, if any.

Show Stash Details

To see a quick summary of what was changed in your most recent stash, use:


    git stash show
    

For a complete view with code differences, add the -p flag:


    git stash show -p
    

Apply the Latest Stash

Reapply the most recent stash without removing it from the stash list:


    git stash apply
    

This brings back your saved changes, allowing you to continue where you left off.

Apply a Specific Stash

You can apply any stash from the list by referring to its index:


    git stash apply stash@{1}
    

Pop the Stash

This command reapplies the most recent stash and deletes it from the stash stack:


    git stash pop
    

Ideal when you want to restore changes and clear them from your list in one go.

Drop a Stash

Remove a specific stash from the list using its index:


    git stash drop stash@{0}
    

Clear All Stashes

Delete every saved stash in your project:


    git stash clear
    

Use this with caution—cleared stashes cannot be recovered.

Branch from a Stash

You can directly create a new branch from a stash if your saved work deserves to be its own feature:


    git stash branch new-feature stash@{0}
    

This creates a branch and applies the stash there, letting you develop your work in isolation.

Git History

Understanding Git History

Git maintains a complete record of every change in your repository. Using history commands, you can check what was modified, when it happened, and who made the change. This is incredibly helpful for tracking development, identifying bugs, and reviewing the project's journey.

Essential Commands for Viewing History

  • git log – Display complete commit history
  • git log --oneline – View a concise version of commit history
  • git show <commit> – See details of a particular commit
  • git diff – Show unstaged file changes
  • git diff --staged – Show staged file changes

Tips for Managing History Effectively

  • Commit regularly and keep messages meaningful and relevant.
  • Write clear commit messages for better collaboration and understanding.
  • Use git log --oneline to get a fast snapshot of commit history.
  • Use git diff to review your changes before committing.

Viewing Complete Commit Log

To check all commits in your repo:

git log
commit 09f4acd3f8836b7f...
Author: Dev Team
Date:   Fri Mar 26 09:35:54 2021 +0100

    Modified index.html with new line

Inspecting a Specific Commit

git show 09f4acd
commit 09f4acd3f88...
Author: Dev Team
Date:   Fri Mar 26 09:35:54 2021 +0100

    Modified index.html

diff --git a/index.html b/index.html
@@ ...
+Updated Title

Checking Local Modifications

git diff
diff --git a/index.html b/index.html
@@ ...
-Old Title
+Updated Title

Reviewing Staged Changes

git diff --staged
diff --git a/index.html b/index.html
@@ ...
-Old Title
+Updated Title

Comparing Two Commits

git diff abc123 def456

Quick Overview of Commits

git log --oneline
09f4acd Updated homepage
8e7b2c1 Added About page
1a2b3c4 Initial structure

Filter Commits by Author

git log --author="Alice"

Show Commits From a Specific Timeframe

git log --since="2 weeks ago"

Display Changed Files Per Commit

git log --stat
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

Visualize Commit Tree with Graph

git log --graph --oneline
* 09f4acd Updated homepage
* 8e7b2c1 Added About page
|\
| * aabbccd Merged feature-x
|/

Common Issues and Solutions

  • Not seeing your updates? Make sure they’ve been committed.
  • Too many logs to handle? Use --oneline or --since options for simplicity.
  • Stuck inside the log view? Hit q to exit.

Git Help

Git offers a vast set of commands, and it’s common to need assistance remembering their syntax or available options. Fortunately, Git includes a built-in help system that lets you access command manuals directly from your terminal—quickly and without leaving your workflow.

When Should You Use Git Help?

Whether you're exploring a new command or just forgot a flag, Git's help commands are the fastest way to get reliable answers. They're especially useful when you want to stay inside the terminal.

Useful Help Commands

  • git help <command> – Opens the manual for the specified command
  • git <command> --help – Alternative syntax for the same manual
  • git <command> -h – Displays a concise summary of command options
  • git help --all – Lists all Git commands on your system
  • git help -g – Shows concept guides and useful documentation topics

Full Manual Page: git help <command>

This opens the detailed manual of any Git command, including description, usage, and options.

Example:

git help commit

This will launch the manual for git commit, showing its usage and available flags.

Alternative Way: git <command> --help

Works exactly like git help <command>. Many users find this format more intuitive.

Example:

git status --help

Launches the help for git status.

Quick Overview: git <command> -h

Need just the options without the full manual? Use -h to get a summary right in the terminal.

Example:

git add -h

Outputs a list of common flags like --interactive, --patch, etc.

See All Commands: git help --all

Displays every Git command available on your system, categorized by their purpose. Note: It’s a long list!

git help --all

Use Shift + G to jump to the end and q to exit the viewer.

Explore Git Guides: git help -g

This command shows in-depth learning topics like revisions, everyday Git usage, and attributes. Ideal for diving deeper into Git’s concepts.

git help -g

Example guides include:

  • everyday – Practical Git with 20 common commands
  • revisions – Understanding revision ranges
  • glossary – Definitions of key Git terms

Tips for Navigating Git Help Viewer

  • Use Space or arrow keys to scroll
  • Press / to search, then type your keyword
  • Press n for the next match
  • Press q to quit

Troubleshooting Help Viewer

  • If the help doesn’t open, try git <command> -h instead
  • To search inside help, press / followed by your term
  • Can't exit? Press q to quit the viewer