Start Using Git on the Command Line Like a Pro in Five Minutes

A quick tour of basic Git commands

Wiktor Malinowski
Dec 2 · 6 min read

You write code every day, right? Why not write Git commands like a code instead of clicking over to your GUI tool?

OK, you need some time to familiarize yourself, but once it happens, you’ll be more efficient than ever.

I’m going to show you some basic Git commands and their features. This isn’t another Git documentation, though.


Git Status

The git status command shows what the full status is, where the HEAD is, and what the changes are (staged, not staged, untracked files).

Once you’re familiar with the long output, you might use the git status --short to see the minified result:


Manipulate the Changes

Here’s a list of some commands that allow you to manipulate the changes:

  • git add file— adds the file file to the stage section
  • git restore file— discards changes to the file
  • git restore --staged file— removes the file from the stage section

The file may be replaced with any file or directory path you want to modify.


Inspecting the diffs

  • git diff — displays the not-staged changes in your working tree
  • git diff --staged — displays the staged changes
  • git diff dev — displays changes between the working tree and the dev branch. You might use a commit hash or tag instead.
  • git diff master..dev — displays changes between the branches master and dev

You might pass a path to a file or directory as an additional argument to filter the diffs to a specified path.

If you only want to inspect the file names, use the --name-only switch. If you need to know which files were modified, added, or deleted, use the--name-status switch.


Committing

Once you have some staged changes, use git commit -m 'Commit message'.
If you don’t use the -m switch, it opens the default text editor. Don’t be surprised if it’s VIM. Save the file, and close the window in order to complete the commit.

You can modify the default editor in the config like:


Use Wildcards for the add/restore/switch/diff Commands

For example, you only want to add all of the TypeScript files from the src dir to be staged for commit:

Discard all unstaged changes from the test directory:

Discard all of the staged changes (move to the unstaged section) starting from the current directory:

It supports a glob pattern, so you have great control over choosing the elements. For the testing purposes, you can use an -n argument also to dry-run the command.


Interactively Choose Hunks

Use the -p switch (or --patch) to interactively choose hunks of code you want to patch using the add (reset or restore, as well) command. You decide which hunk (or an entire file) to add. The -p switch turns the output into:


Switch

The switch command can be used to switch between branches.

  • git switch branch-name — switches to a branch-name branch
  • git switch -b abc — creates branch abc and switches to it
  • git switch - — switches you to the previous branch
  • git checkout [args]— works as a switch but can also move HEAD to any commit/tag as well*

* The switch and restore are quite recent commands meant to provide a better interface for the well-known multi-purpose checkout.

Here is some article with details if you want to dive in:


Branches

  • git branch — returns a list of the local branches.
    Use -r to list only remote branches or -a to get them all
  • git branch abc — creates abc branch
  • git branch -d abc — deletes abc if already merged.
    If not, use -D to force deletion.

Log Results

Use the git log to see the commits’ history.

There are a lot of options here, like:

  • Display the changes alongside the commit description:
    git log -p
  • Return n latest commits from your current position:
    git log -2 # display two recent commits
  • Display log for a specific branch/revision:
    git log branch-name
  • Filter log for a specific file(s) only (glob pattern applies here):
    git log *.js # or git log branch-name:*.js
  • Filter commits by the author (accepts partial name):
    git log --author wiktor
  • Filter by date:
    git log --since 2019-10-01
  • Filter by date using dynamic date ranges (days, weeks, months, years):
    git log --since 2months --until 1week

Setup Aliases

You don’t need to type the entire Git command every time. There’s some basic setup for the aliases. Just paste it into your .gitconfig file.

Now, you can simply use the git st instead of git status, etc.

It’s possible to add alias from the command line as well. For example setting l alias for the log command: git config --global alias l log.

Aliases in action:


Merge

git merge abc — merges the abc branch into yours.

Sometimes you don’t want to include the commits’ history when merging the changes. There’s an option to squash all of the changes into one commit.

git merge --squash your-branch

It results in staged changes in the target branch so you can still modify it before merging.


Setup SSH Keys

To access/modify your repository from the external server, you have to be authorized.

You can either use your credentials over HTTPS or set up your SSH keys that provide a typing-less password. SSH keys have numerous advantages over passwords like:

  • Better security, as it’s extremely unlikely to brute force or guess
  • Provides a typing-less password (until the key is created without a passphrase)

In order to create the SSH key pair, use the ssh-keygen command:

ssh-keygen -t rsa -b 4096

It creates an RSA key with a key size of 4096 bits for better security. It generates the keys in ~/.ssh directory by default. Please remark the private key id_rsa is secret and must not be shared.

Then, send the public key ~/.ssh/id_rsa.pub to your GitHub account.

There’s an official tutorial for that:

From now on, you can access and write to the repository without a password.

Please note it applies for GitLab, Bitbucket, or anything else also. The process should look pretty similar.


Still Here?

You definitely must try a text-mode Git tool called tig. It facilitates browsing changes in the repository. Look at the screenshot:


That’s it!

This was just a simple example of using the basic Git commands. Each of the commands is so comprehensive you’d have to write an article (or series) for each of them separately. The manual of the log command only consists of 2300+ lines.

I hope you enjoyed reading!

Better Programming

Advice for programmers.

Wiktor Malinowski

Written by

Father, Husband, Software Engineer.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade