Reader small image

You're reading from  Git for Programmers

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781801075732
Edition1st Edition
Right arrow
Author (1)
Jesse Liberty
Jesse Liberty
author image
Jesse Liberty

Jesse Liberty is a full-time hands-on programmer, specializing in C#, git and .NET MAUI. He hosts the popular Yet Another Podcast and is the author of more than a dozen best-selling programming books. Liberty is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP. He was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University. Jesse is a recognized expert and has spoken at conferences world-wide.
Read more about Jesse Liberty

Right arrow

Important Git Commands and Metadata

There are hundreds of Git commands and flags. In this chapter, we'll endeavor to review a few of the most important that we have not looked at so far. These include:

  • Creating the stash
  • Listing what's in the stash
  • Retrieving from the stash
  • The clean command for getting rid of unwanted untracked files
  • How to see metadata and choose which data you want to see

Let's start by digging into the stash.

You can find the complete list at http://git-scm.com/docs.

Stash

When we reviewed the five areas of Git, we included an area called the stash, but we did not delve into what the stash is. In short, the stash is a place where you can hold (stash) files you've modified but not yet committed:

Figure 10.1: The five areas of Git

The stash can be pretty important. Let's say you are working on a feature and suddenly you are asked to work on a very important bug. You are not ready to commit the code you have, but you can't switch branches with uncommitted files in the work area.

To solve this, you could just make a backup of your directory, and then delete the uncommitted files, but that is very slow and error-prone. Instead, you want to stash them away somewhere that you can get them back when you are ready, which of course is the purpose of the stash.

To see this at work, we need a repository with some commits. Let's quickly make a mirror of the RockyHorror2 repo. To do so, we'll start by making...

Clean

From time to time, you'll find that there are untracked files listed in your status. 99% of the time these will be files you created and you'll want them to be tracked, which you do by adding them to the index (as shown previously). There are times, however, when you may find untracked files that you don't want:

Figure 10.15: Untracked files

In this case, we have a couple choices. We can add Untracked.cs to the index or we can get rid of it. To do so, we try git clean:

Figure 10.16: Using clean to remove untracked files (fails)

Because git clean is one of the few truly destructive commands—once called, the untracked files are gone, never to be seen again—Git comes back with the snarky reply that it is "refusing to clean." To actually clean, Git requires that you tell it you really mean it by using the -f (force) flag:

Figure 10.17: Using clean as above, but with the force flag (succeeds)

The -f flag...

Metadata

Every commit, merge, and so on, is accompanied by metadata. You can get at a lot of the metadata by using the log, but sometimes you just want to extract a few pieces of important metadata. For that you can use the show command:

Figure 10.18: Using show to see metadata

In this example, we use show to find the name and email of the author, along with the ID and the metadata telling us where the tip of main is. Let's break it down:

  • git show—the show command.
  • -s—silent (or quiet), which suppresses the difference output (try the command without it to see).
  • HEAD tells show which commit you are interested in.
  • %an is the author's name.
  • %ae is the author's email address.

We put this code into a string and assign it to the format flag.

Let's look at the log and see what else we can do with showing metadata:

Figure 10.19: Looking at the log

Let's zero in on the metadata...

Summary

In this chapter, we reviewed some of the most important commands that we had not yet looked at. These include:

  • Creating the stash
  • Listing what's in the stash
  • Retrieving from the stash
  • The clean command for getting rid of unwanted untracked files
  • How to see metadata and choose which data you want to see

Challenge

Mirror a repo, or use one you already have if you don't mind changing it. Examine the list of commits. Start work on some changes but don't commit your changes. Switch to working on a different repo. Create or modify some files in the second repo but don't commit them. Start work on a third repo. Abandon that work and go back to the first repository. Examine the stash and retrieve the stash you need to keep working.

Answer

Once again, there are many ways to answer this. I'll start by mirroring the RockyHorrorStash repo to RockyHorrorStash2, and then I will immediately clone it to my local repo.

I'll do the same thing with Panofy (to PanofyStash) and musicHandler2 (creating musicHandler2Stash). Now we have three repos we can work on:

  • musicHandler2Stash
  • PanofyStash
  • RockyHorrorStash

Let's begin with musicHandler2Stash by changing directory and getting a log of what is already there. Then let's open it in Visual Studio and do some work:

Figure 10.22: Log of MusicHandler2Stash

Let's make two changes by opening Visual Studio in that directory. We need changes in a couple of files, so let's just add comments. When we take a status, we see that there are two modified files:

Figure 10.23: Status of MusicHandler files after changes and before commit

Right in the middle of our work, we're asked to work on a...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Git for Programmers
Published in: Jun 2021Publisher: PacktISBN-13: 9781801075732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jesse Liberty

Jesse Liberty is a full-time hands-on programmer, specializing in C#, git and .NET MAUI. He hosts the popular Yet Another Podcast and is the author of more than a dozen best-selling programming books. Liberty is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP. He was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University. Jesse is a recognized expert and has spoken at conferences world-wide.
Read more about Jesse Liberty