Wednesday 13 March 2024

The Git & Github Bootcamp Part 3- Master on essentials and the tricky bits: rebasing, squashing, stashing, reflogs, blobs, trees, & more!


Comparing Changes with Git Diff

1. Introducing The Git Diff Command

git diff shows the differences between various commits, the staging area, and the working directory. It’s particularly useful for seeing what has changed in your code before committing.

2. A Guide To Reading Diffs

A diff output shows additions and deletions between two states. Additions are prefixed with a + and highlighted in green, while deletions are prefixed with a - and highlighted in red.


3. Viewing Unstaged Changes

To see what changes you’ve made that are not yet staged for commit:

git diff

This command compares your working directory with the staged area (index) and shows the differences.

4. Viewing Working Directory Changes

The same git diff command shows changes in your working directory that you haven’t staged yet. If you want to compare your working directory against the last commit, use:

git diff HEAD

5. Viewing Staged Changes

To see the changes you’ve staged that are waiting to be committed, compared to your last commit:

git diff --staged

or

git diff --cached

Both commands do the same thing, showing what’s different in your staged changes from the last commit.

6. Diffing Specific Files

If you’re only interested in how a specific file has changed, you can specify that file:

git diff <file>

For staged changes in a specific file:

git diff --staged <file>

7. Comparing Changes Across Branches

To compare the current branch with another branch:

git diff <other-branch>

This shows the differences between the tip of the current branch and the tip of <other-branch>.

8. Comparing Changes Across Commits

You can compare the changes between any two commits:

git diff <commit1> <commit2>

Replace <commit1> and <commit2> with the commit hashes or references you’re interested in comparing.

9. Visualizing Diffs With GUIs

Many GUI tools for Git, like GitKraken or Sourcetree, provide visual diff features. These can make it easier to understand the changes, especially for those who prefer a graphical interface over command-line text.

10. Diff Exercise

Let’s do a quick exercise with git diff:

  • Make a change in README.md but don’t stage it. Then run git diff to see your unstaged changes.
  • Now, stage the change (git add README.md) and run git diff --staged to view the staged changes.
  • Finally, make another edit in README.md (without staging) and use git diff HEAD to see all the changes in your working directory compared to the last commit.

This exercise demonstrates how git diff can be used to review changes at various stages of development, helping you maintain a clear understanding of your project’s evolution.

In our ongoing exploration of “SampleProject,” let’s delve into the functionality of Git stash. Stashing is a handy feature that allows you to temporarily save your work-in-progress changes without committing them, enabling you to switch contexts quickly.

The Ins and Outs of Stashing

1. Why We Need Git Stash

There are times when you’re in the middle of coding something and you need to switch branches to work on something else, but you’re not ready to commit your current changes. git stash allows you to save your modifications temporarily so you can return to a clean working directory.

2. Stashing Basics: Git Stash Save & Pop

To stash your changes:

git stash push -m "Your stash message"

This command temporarily shelves changes so you can work on a different task. To reapply the most recently stashed changes and remove them from the stash stack, use:

git stash pop

3. Practicing With Git Stash

Imagine you’re working on a new feature in feature-x branch and need to switch to main branch to fix a bug.

  • First, stash your current changes in feature-x: git stash push -m "Feature X work"
  • Switch to main branch: git switch main
    After fixing the bug in main, you want to return to your work on feature-x.
  • Switch back to feature-x: git switch feature-x
  • Reapply your stashed changes: git stash pop

4. Git Stash Apply

If you want to reapply stashed changes without removing them from the stash stack, use:

git stash apply

This is useful if you want to apply the same stashed changes to multiple branches.

5. Working With Multiple Stashes

You can have multiple stashed entries. To view all your stashes:

git stash list

To apply a specific stash (e.g., the second one in the list):

git stash apply stash@{1}

6. Dropping & Clearing The Stash

To remove a specific stash from the list:

git stash drop stash@{1}

To clear all stashed entries:

git stash clear

7. Stashing Exercise

Let’s practice managing multiple stashes.

  • Make changes in two different files and stash each set of changes separately with meaningful messages.
  • List your stashes to see them indexed.
  • Try applying the second stash you made to see it in action.
  • Drop the first stash you made.
  • Finally, clear all remaining stashes.

This exercise helps you understand how to manage your work-in-progress effectively using Git stash, ensuring you can switch tasks without losing any changes.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home