Git: Switching to the Command Line

Git: Switching to the Command Line

So you're thinking about giving the Git command line a go. Maybe you're ready for a faster interface than UI clients, or maybe you just want to get closer to the metal. Here are the bare minimums to get started.

Get ready

If you're starting with a fresh, new codebase, run git init.

$ cd myproject
$ git init

Or if you're starting with an existing codebase, run git clone to acquire a local copy from the remote repository.

$ git clone <repository-url> myproject
$ cd myproject

Running git clone will create the project folder for you. The repository URL looks like https://github.com/someone/myproject and varies depending on where the project is hosted.

Get the latest version

Run git pull to get the latest version from the remote repository.

$ git pull

Check the status

Run git status to see your changes and additions compared against the most recent git pull.

$ git status

Remember: these are compared against your most recent pull; not the remote repository.

Stage (queue) your changes and additions

Run git add to stage your changes and additions for the next commit.

$ git add .

The period at the end is important. It means, add any new, modified, or deleted files in the current directory and all subdirectories.

You can also stage individual files.

$ git add path/to/specific/file

Any time you're typing in specific paths and file names, remember to hit that [Tab] key to autocomplete and save some typing.

$ git add pa[Tab]
  git add path/
  git add path/to/sp[Tab]
  git add path/to/specific/

Create a commit

Run git commit to combine your staged changes and additions into a single commit.

$ git commit -m "Fixed layout bug in dashboard"

If you forget to include the -m parameter (for "message"), then Git will attempt to open up a text editor and have you type one there. Use the -m parameter; it's much faster.

Push commits to the remote repository

Run git push to send your commits to the remote repository.

$ git push

If any changes have been made on the remote repository since the last time you ran git pull, then you'll have to run git pull again before you can push.

Summary

  • Get started locally with git init or git clone.
  • Get the latest with git pull.
  • See where you are with git status.
  • Stage changes with git add.
  • Create commits with git commit.
  • Push your commits to the remote repository with git push.

Explore these commands next:

  • git diff to see what has changed
  • git branch and git checkout to create and switch between branches
  • git merge to merge a branch into the current branch

Happy Git command lining!

Photo by Moritz Kindler on Unsplash