Getting Started with Git

Aug 14 2023 · git 2.28, console

Part 1: Getting Started with Git

09. Using the Command Line

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 08. Organizing Your Commit Next episode: 10. Ignoring Files

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 09. Using the Command Line

Cover the same basic concepts that are used with the GH desktop app and how they can be done with the command line.

Transcript: 09. Using the Command Line

You don’t need to use the command line to be effective with Git, but this episode is to show you some basic Git commands so you can see what the GitHub Desktop app is doing behind the scenes as well as an alternative to using the GitHub desktop app if you prefer.

Open up a terminal and navigate to your working directory of the recipes-book repo. Depending on what branch you were on, this might be different. This states that I’m on the main branch. If you are not on the main branch, you can do git checkout main and hit enter. Since I’m already on the main branch, I won’t hit enter.

One of the most important commands that you’ll learn is git status. As you can imagine by the name, status tells you the status of your Git repo. It tells you you are on branch main, and that your working directory is clean. This is because in the last episode, we pushed all of our changes to the remote and don’t have any changes.

As you did previously, when you’re ready to start a new feature, you’ll want to create a branch.

You can create a branch and switch to that branch just like you did with the GitHub app by using

git checkout -b for branch, followed by the branch name.

In terminal, type git checkout -b jambalaya.

Another handy command is git branch. This shows the branches you have on your local repo. Since we haven’t deleted any branches from when we started this project, you can see all the branches here. You can type Q to quit this view.

Open your text editor and open recipes.txt and add a new entry for jambalaya. Now go back to the command line and run git status again. You can see Git knows that you have changes, but your change is not staged to be committed. Your terminal might have different color coding than mine, but typically files that are not staged to be committed are in red.

In order to add your change to the staging area, you can do

git add .

This will add all of the changes in your working directory to the staging area.

Run git status, and see now, that change is in green denoting that it’s staged to be committed.

If you wanted to remove all the files from the staging area, you can do git reset. This doesn’t reset the changes to your files. You can see here in your text editor, your recipes.txt still has the jambalaya recipe. Switch back to terminal.

I’ll clear the terminal to give myself more space.

If you run git status again, you’ll see we are back where we left off with the changes unstaged.

Alternatively, if you had modified multiple files and you wanted to just add changes from a single file, you can do git add and the name of the file.

git add recipes.txt

Now run git status. As you can see, the changes are in green. Now your changes are ready to be committed.

To make a commit, run git commit -m. We are using the -m so we can add the commit message with the commit. This makes it easy to do it in one step. Now add the commit message, “added jambalaya recipe to recipes list”.

Run git status again. You can see that our working directory is clean again. But if you remember, these changes are still in our local repo only. We need to push those changes to the remote repo.

To push your new branch, run git push -u origin and the name of the branch, which in our case is jambalaya. The -u option is used here to tell Git that the jambalaya branch locally will track the remote branch jambalaya.

Now if you go to a web browser, you can see your new branch jambalaya and the change you made to the recipes file.

The last command you’ll look at is git diff. Open your text editor and add another recipes to the list. Add brownies.

Run git status. It shows we again have made changes to the recipes.txt file, but we don’t know what those changes are. You can see those changes by running git diff. This is similar to what you saw in the GitHub desktop app. It has one new line added denoted by the green plus with the text Brownies.

These are some of the basic commands that you did while using the GitHub app. And since you know the Git terminology, if you want to learn more about the command line, you can easily search for the Git command online and give it a whirl.