How to git on the command line

In this article, we’ll run through some simple scenarios that may apply to your work at some point. We will:

  • Initialize (create) a new repository,
  • Add a file,
  • Commit our work

This is, by no means, a comprehensive guide to git’ing on the command line. I’d encourage you to read the git documentation for a ton of additional reference.

Let’s Do This!

First, we need a place to work. I’ll be working out of a folder called src, but you’re welcome to work anywhere. Let’s:

  • Create a directory
  • Change into said directory
  • Initialize a git repository
  • Review what happened
Yup…all of that
  • mkdir git-demo
    • Created a folder
  • cd git-demo
    • Changed paths into the new folder
  • git init
    • Started a git repo
  • ls -Hidden
    • Show the contents of the directory (we’re in PowerShell Core, but in bash you could do ls -al)

The last command shows the .git folder. This contains the metadata of your repository. We won’t go into detail in this article–perhaps in the future. Just know it’s there.

A note about the [master] notation: I have installed posh-git. It’s a great module that enhances your prompt to describe the state of a git repository. It’s not required, but if you’d like to install it, check this out.

The Project

Now we have a repository, so let’s pretend we’re importing some super important project with tons of files to this directory, when in reality, we’re going to create a really simple file (work with me!) Lets:

  • Add a file with some text in it
  • Stage the new file
  • Commit our work
  • Review
Add a file with some text in it

Don’t get too hung up on the PowerShell syntax above. The first line simple says, “Put this text into a file called awesomeprogram.txt” and then ls shows us the file is there. The more command displays the contents of the file, so you know there’s no funny business.

Notice the posh-git prompt changed. It’s trying to tell us, “you have a dirty working directory” or unstaged files. The last icon ! is the indicator for unstaged files.

git status shows us what’s up:

git status

There is a notion of staging in git that serves as a holding area for files about to be committed. If we attempted to commit our work right now, nothing would happen, since the stage is empty. Let’s stage our new file with git add .:

Add the new file, and run git status to check on things

Now we see the file is ready to be committed. The ~ is the indicator for staged, but uncommitted files. So, let’s commit our work.

You guessed it–we use git commit, and the -m switch specifies that we want to dictate the commit message right in the same command.

The posh-git prompt changes back to simply [master] and our work is now part of the repository history!

Summary

This may not seem like a lot, but we accomplished a ton! You can apply this work to a new project, or an existing project. To elaborate on the latter, copy your project contents into the repository folder and follow the instructions starting with git add . to stage all your files. Boom! Existing project into version control.

Leave a Reply