Git CLI Cheatsheet

Git CLI Cheatsheet

This Git cheat sheet saves you time when you just can't remember what a command is or don't want to use git help in the command line.

I was tired of looking up some of the most commonly used git commands so I thought I'd put them all in one place! Use this handy git cheat sheet guide to enhance your workflow.

imageonline-co-hueshifted.png

What is Git?

Git is an Open Source Distributed Version Control System.

Let's break that down and explain the wording:

  • Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.
  • Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel. So Version Control System helps in handling this by maintaining a history of what changes have happened. Also, Git provides features like branches and merges, which I will be covering later.
  • Distributed Version Control System: Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer. I will explain the concept of remote and local repositories later in this article.

Some of the most commonly used Git commands

Git Configure

git config --global user.email "johndoe@example.com"

Sets the email address to be used for your commits.

git config --global user.name "FirstName LastName"

Sets the name of the author.

git config --list

This command lists all the settings Git can find at that given point.

git config --global color.ui true

Git automatically adds color to it's output.

Git: Committing to a Repository

git commit -m "Description of file changes"

This command records or snapshots the file permanently in the version history.

git commit --amend -m <enter new message>

This command allows you to change your commit message.

Git: Branching

git branch

This command lists all the local branches in the current repository.

git branch <branch-name>

This command creates a new branch.

git checkout <branch-name>

This command is used to alternate from one branch to another.

git merge <branch-name>

This command merges the specified branch’s history into the current branch.

git checkout -b <branch-name>

This command creates a new branch and also switches into it.

Initiating a New Repository

git init

This command is used to create a new repository.

git status

This command lists all the statuses of the files that have to be committed.

Git: Pulling & Pushing from and to Repositories

git remote add origin <link-to-repo>

This command is used to connect your local repository to the remote server.

git push -u origin main

This command sends the committed changes of main branch to your remote repository.

git clone <link-to-clone-repo>

This command is used to obtain a repository from an existing URL.

git pull

This command fetches and merges changes on the remote server to your working directory.

Git: Staging

git add <file-name>

This command adds a file to the staging area.

git add <file-name> <second-file-name> <third-file-name>

This command adds one or more files to the staging area.

git add .

This command adds all files under the current directory to the staging area.

git add --all

This command finds all new and updated files everywhere throughout the project and add them to the staging area.

git rm --cached <file-name>

Removes specific file.

git reset <file-name>

This command removes the file from the staging environment, but it preserves the file contents.

Conclusion

I hope you found this page full of the most commonly used Git commands as useful as I did. If there are any other popular commands that I may have missed, I would love to read them in the comments. Thank you!