Skip to content

Git Basics: Diff and Stash

This article was written over 18 months ago and may contain information that is out of date. Some content may be relevant but please refer to the relevant official documentation or available resources for the latest information.

Getting started with Git

Today’s article will cover some of the basics of Git. This article is written under the assumption that you have already made a GitHub repository or have access to one, and that you also have a basic understanding of the command line.

If you haven’t opened up a GitHub account, I recommend going to GitHub, creating an account, setting up a repository and following this guide before continuing on.

Now, we’ll move on to a brief rundown of the Git commands that will be used in this article, and then follow it up with how to use each of them.

The Rundown

Git diff

  • This command is used to show changes between commits and the working tree. Git stash
  • This command is used to stash or remove the changes made to your working directory (no worries these haven’t gone up in smoke) Git stash pop
  • This command is used to retrieve your most recent stash made by popping it from your stash stack Git stash list
  • This command is used to display a list of your current stash entries. Git stash apply
  • This command is used to reapply a git stash, but also keep it in your stash

Git Diff

Alright, now we’re going to move on to how to do a git diff. I’m going to be going to my console, and heading over to the blog repo I used last time. From here, I’m going to open up my README file with nano and edit it. After saving, I’ll use a git status to verify that the changes are showing up. Now, we can see that the file is edited, but say we don’t know or remember what was changed. In this instance, we can use a git diff, and it’ll show us the changes that were made.

Git status latest git blog 1

Git diff latest git blog 2

Git Stash

Say we decide we don’t want or need those README changes at the moment we can use a git stash. With that done, we’ll use git status, and we can see that those changes are gone. While the change do appear to be gone, we can easily retrieve it by doing git stash pop. Once again, we’ll use git status, and verify that the changes are back.

Git stash latest git blog 3

Git status latest git blog 4

Git stash pop latest git blog 5

Git status again latest git blog 1

Git stash list & apply

Alright, so we’re going to do a git stash again to get rid of our current changes. We’re going to edit the README with nano again. We’ll run another git status to verify the changes were made. Then, we’re going to do another git stash to get rid of those changes. Now, with a couple of changes stashed, we’re going to do a git stash list to see our list of stashed changes.

Git stash latest git blog 3

Nano changes and git status latest git blog 6

Git stash again latest git blog 3

Git stash list latest git blog 7

Now we want our initial changes. In order to get those, we’ll use a git stash apply 1. This will keep it in your git stash and it is useful if you want to apply the same changes in multiple branches.

Git stash apply n (in our case n === 1) latest git blog 8

Conclusion

We made it to the end! I hope this article was helpful, and that you were able to learn about, and be more comfortable with Git and GitHub. This article covered some of the basics of Git to try and help people starting out, or for those who might need a refresher.

This Dot is a consultancy dedicated to guiding companies through their modernization and digital transformation journeys. Specializing in replatforming, modernizing, and launching new initiatives, we stand out by taking true ownership of your engineering projects.

We love helping teams with projects that have missed their deadlines or helping keep your strategic digital initiatives on course. Check out our case studies and our clients that trust us with their engineering.

Let's innovate together!

We're ready to be your trusted technical partners in your digital innovation journey.

Whether it's modernization or custom software solutions, our team of experts can guide you through best practices and how to build scalable, performant software that lasts.

Prefer email? hi@thisdot.co