The basic git CLI commands that any developer using it should know

Aaron Moore
4 min readAug 9, 2021

--

Generic git history image
Photo by Yancy Min on Unsplash

There are a few, relatively simple commands you should know in order to be effective with using git in your projects. However, if you look at the documentation for some of these commands it can be a bit overwhelming. This is because git is so powerful and flexible.

Thankfully, the vast majority of the time you don’t need this power and flexibility — you just want to ensure you have the latest changes, or send your latest changes to a remote repository.

This is not intended as an exhaustive list. This is simply an introductory list of the most common commands that it has been my experience to use. These cover the vast majority of the day-to-day and are intended for those new to the git CLI. Don’t expect anything here to be advanced. None of these commands are powerful enough to bork your repo.

Useful Commands

Clone

This will pull down a remote repository. You want to execute this from the parent folder you want to store the repository in. It’s as simple as:

git clone <url> <directory?>

Just replace <url> with the URL of a repository and either leave off <directory> or replace it with the name of the directory. If you leave off the directory name it’ll just use the repo name. Adding the directory is useful if the repo has a really long name that I can locally shorten to an abbreviation or acronym for personal use without affecting the repository.

Checkout, Checkout new branch

The following will switch over to an existing branch:

git checkout <branch>

Adding the -b flag will create the branch before switching over to it:

git checkout -b <branch>

Just replace <branch> with whatever you want to name it.

Pull down remote changes, pull from another branch

In order to pull remote changes, you use the pull command.

git pull

This will pull the latest changes from the repo, and any changes to the same branch you’re on it’ll try and merge.

If you have a branch and there are changes from another branch, you indicate both the remote and the branch name.

git pull origin <branch>

For example, I’ll often git pull origin main into my feature branch before I push my feature branch up. That’ll ensure I don’t have any conflicts.

Push changes to existing or new branch

In order to send changes to the remote repository, you use the push command.

git push

This will send your changes to a remote branch of the same name. If you do this and the branch doesn’t exist, you’ll get an error. But don’t fret! The error (at least by default in the version of git I’m currently using) gives you the exact command to copy/paste in to send your new branch to the remote repo. The command for it is:

git push -u <branch>

The -u flag signifies the upstream branch, or the longhand flag --set-upstream-to. (previously just --set-upstream but that’s been deprecated)

Status

This would be the command I use most often and it’s the simplest. It tells me the current working state of the files I’m working with.

git status

It shows what’s changed/deleted, what’s staged/un-staged, and what is untracked.

Stage and Commit Changes

When you want to take the working-copy of your code and codify or “commit” it to the repo, the command is commit. In order for anything to commit though, it needs to be “staged”. To stage, you use the add command.

git add <path(s)>

You can also use relative paths like . or .. and it’ll work its way through the un-tracked files recursively with that folder as the base folder for recursively checking. Be sure before you use these that any files/folders you don’t want committed are listed appropriately in your .gitignore file.

To commit, you send the commit command along with the -m flag to indicate a description or “message” that is tied to it.

git commit -m "my commit message"

For a shortcut, if you’ve only got file modifications/deletions (nothing “untracked” you can skip the git add command and combine them using the -a flag. Often I commit with:

git commit -am "my commit message"

This will automatically add modified/deleted files as long as they are already tracked. It will not add untracked, you’ll need to use add beforehand.

Bonus commands

I tried to limit the main list to the absolute necessities. The following commands are some that are also good-to-know.

Init

The git init command will initialize a new, empty repository in the directory where you type it.

Diff

This command takes tracked, unstaged files and shows what has been modified. This is not a very good diff tool. However, it’s useful for doing a quick once-over to ensure nothing needs any last-minute changes before being committed.

git diff

Note: If the diff tool has to scroll you have to hit q to exit the diff tool. You can use the down arrow or page-down on your keyboard to scroll.

Branch

Typing git branch -l will list all your local branches.

--

--

Aaron Moore
Aaron Moore

Written by Aaron Moore

Full-stack software engineer/architect.

No responses yet