Basic Git Commands You Need To Know

To use Git well, you need two things: an understanding of what Git is and knowledge of which commands to use in various circumstances. In this post, we’ll review both.

An Understanding of Git

So first up, what is Git?

Git is a form of version control software. It’s designed to help you keep track of changes in code, documents, images, and other files related to a project. When you use Git, you create a repository and take “snapshots” of the content. Because they’re stored in a central location with a full history that can be used to roll back through changes, check differences, and recover deleted files, these snapshots can then be safely shared between different contributors and physical systems.

A Git repository consists of three primary sections. The first is the main Git directory, which stores the collection of snapshots you “commit” into it. Then there’s the staging area, which temporarily holds files prior to them being committed. Finally, the working directory holds the latest copy of your files— it’s where you create, edit, and delete files.

Basic Git Commands

Here’s a list of basic Git commands that can get you started.

Initialize a project repository:

  • git init [repository name] creates a blank repository
  • cd [project_root] && git init initializes in an existing project

Connect the repository to a remote machine or server:

  • git remote -v lists the available remote repositories
  • git remote add origin [remote_url or host_name] connects your repository to the server at remote_url
  • git remote rm [remote_url or host_name] deletes the connection to the remote machine or server

Copy an existing project repository to a new machine:

  • git clone http://username@host/path/to/repository copies from a remote host
  • cd [new_root] && git clone /path/to/repository copies from another directory on the same machine

Add files to the staging area:

  • git add [file_name] adds file_name to the repository (regular expression wildcards, such as *, can also be used to add multiple files at once)

Work with branches:

  1. git branch lists available branches
  2. git branch [branch_name] creates a new local branch called branch_name
  3. git checkout [branch_name] switches to branch_name (called “checking out”)
  4. git checkout -b [branch_name] creates and checks out the branch in one command
  5. git merge [other_branch] merges other_branch into the checked out branch
  6. git branch -d [branch_name] deletes branch_name

Commit changes:

  1. git commit -m “[Message describing the changes]” creates a snapshot and saves it to the Git repository

Send and receive changes in different branches:

  1. git push origin [branch_name] sends your changes to the branch_name (note that “master” is commonly used as a default name for the main repository, but it is being changed on multiple platforms that implement Git.)
  2. git pull [branch_name] gets all the changes from branch_name and applies them to your local repository

Some Extended Commands

Here are some additional Git commands.

Add your details to the Git configuration:

  1. git config –local user.email you@email.com sets your details for the current repository
  2. git config –global user.email you@email.com sets your details for all repositories

Check on the current situation of the git repository:

  1. git status displays files that have been changed and are yet to be staged or committed
  2. git diff lists all conflicts between files in different branches, stages, or local repositories
  3. git log shows the repository’s history

Want To Learn More?

Git is an incredibly useful tool. However, it’s also quite complex and has a steep learning curve. As I said at the beginning, using it well requires an understanding of what Git is and knowledge of which commands to use in various circumstances. Cheat sheets like this are fantastic for refreshing your command knowledge, but to get the most out of them, you need a more complete knowledge of Git. Cprime has a Git and GitHub course that runs you through everything you need to know. As a result, you can become the resident Git expert in your office.

Git & GitHub Boot Camp

View Course
Michael de Ridder
Michael de Ridder