Loading
Current section: Commands 17 exercises
lesson

Interactive Git Staging

Before we learn a new command, let's work through a quick exercise to practice the git subcommands we've covered so far.

Check git status

Running git status shows that we have several files that are not yet staged for commit:


$ git status
On branch main
Untracked files:
(use "

Loading lesson

Transcript

00:00 To show off the next command, I need more than one commit. So let's take a break and run a quick workflow exercise. Running git status, we can see all of the new files that I've added for the git subcommands that we've covered. Add, commit, config, log, reset, and show. Let's commit add and reset.

00:17 Git add the git add text file and git reset text file. Run git status again and see that just these two files are staged for commit. That's perfect. Now we can git commit those with a message add staging subcommand notes. Now my message actually has an error in it.

00:37 So let me show you a quick freebie. Run git commit amend to change our last commit message. This will open in your terminal text editor, which is typically vim on most systems. In this case, I can use the arrow keys to navigate to that character and hit x to delete it.

00:54 colon wq for write quit to quit vim. Run git show to show our last commit and see that the message is in fact fixed. We can use git show with a relative reference to head. And now that we have two commits, use the caret to reference previous commits relative to that head.

01:14 This was our first commit where we added help init and status notes. Hit q to escape and to see what's left to commit. Use git add dot to commit all of these. But config is distinctly different from these other commands which operate on commits.

01:31 So run git reset the opposite command of git add and pass the files that we'd like to remove. Commit git log, etc. That's a lot to type, so it may be easier to use the shortcut dot to reset all of it. And add back the git config file.

01:48 If we get tired of typing this git prefix with the dot txt suffix, instead I could use a string glob to use an identifying substring surrounded by any number of other characters. In this case, I know that con is unique to the config file. Of course, I need to use the add subcommand here.

02:08 And git status reveals that we added only this git config text file. Let's commit this with the message add username and email config. And make one last commit for the remaining files. This time, let's use git add interactive, which can be summoned with i for short.

02:26 Git add interactive allows us to interact with the staging area without having to prefix all of our commands with git. In the what now space, I'll type a letter of the command that I want to use. Here I'll use add untracked. Hit a and enter. We see the remaining untracked files, and we can use our numbers to select them.

02:46 One, two, and three. Once we're done, hit enter again. S for status. See that these three files are now staged. We can revert any changes if we want to with r. But this is looking good, so hit enter to get out of that. And q to quit this interactive mode.

03:03 Now git status will show us everything that we committed with git add interactive, which we can commit. Add commit subcommands. Now with multiple commits, you can learn the next command.