> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Basic Git Commands You Must Know [Download Cheat Sheet]
- URL: https://itsfoss.com/basic-git-commands-cheat-sheet/
- Published: 2016-06-06T22:59:50.000Z
- Updated: 2024-08-27T09:07:26.000Z
- Description: This quick guide shows you all the basic Git commands and their usage. You can also download these commands for quick reference.
- Author: Abhishek Prakash
- Tags: Coding

You have seen the quick guide and [Vi cheat sheet download](https://itsfoss.com/download-vi-cheat-sheet/) in an earlier post. In this article, we’ll see all the basic Git commands you need to get started with it.

## Basic git commands at a glance

Here are the essential git commands you must now:

### Create Directory (Local System)

| Purpose                   | Command               |
| ------------------------- | --------------------- |
| New Directory inside Home | cd \~/directory\_name |
| New Git Repository        | git init              |
| Add a single file         | git add <file\_name>  |
| Add all files             | git add --all         |
| Clone into a remote       | git clone <link>      |

### Check Status

| Purpose                          | Command    |
| -------------------------------- | ---------- |
| Show changed files               | git status |
| Show difference in changed files | git diff   |
| Show the log files               | git log    |

### Dealing With Branches

| Purpose                                 | Command                  |
| --------------------------------------- | ------------------------ |
| Create a new branch and stay in current | git branch <brname>      |
| Create a new branch and switch          | git checkout -b <brname> |
| Switch to an existing branch            | git checkout <brname>    |
| Delete and existing branch              | git branch -d <brname>   |

### Revert Changes

| Purpose                              | Command                            |
| ------------------------------------ | ---------------------------------- |
| Revert a particular commit by its ID | git revert <commit id>             |
| Revert all commits                   | git reset --hard <first-commit-id> |
| Revert the last change               | git revert HEAD                    |
| Fix the last commit                  | git commit --amend                 |

### Publish Your Changes

| Purpose                                           | Command                     |
| ------------------------------------------------- | --------------------------- |
| Commit your local changes with a commit message   | git commit -m “Update”      |
| Commit your local changes (Edit Separate Message) | git commit -a               |
| Push changes to your repo                         | git push -u origin <brname> |
| Create patch files for commits in current branch  | git format patch origin     |
| For any help                                      | git --help                  |

## Download free git cheatsheet

If you like, you can download these basic Git commands as a quick reference guide from the link below:

[Git CheatsheetBasic Git commands for quick referencegit-cheatsheet-itsfoss.pdf88 KBdownload-circle](https://itsfoss.com/content/files/2022/12/git-cheatsheet-itsfoss.pdf "Download")

In the next section, I'll show examples of the most common git commands.

## Basic git commands with example

Till now, you saw the basic git commands and learned their purpose. It's time to see them in action.

### Setup Git

After Git is installed, whether from apt or from the source, you need to copy your username and email in the gitconfig file. You can later access this file at \~/.gitconfig.

You can use the follow commands to add in the required information. Replace ‘User’ with your username and ‘user@example.com’ with your email.

```
git config --global user.name "User"
git config --global user.email user@example.com
```

Now you can open it using vim editor or any of your [favourite text editors.](https://itsfoss.com/command-line-text-editors-linux/)

```
sudo vim ~/.gitconfig
```

And you are done with setting up. Now let’s get started with Git.

### Repository

Create a new directory, open it and run this command:

```
git init
```

![create a new git repository with git init](https://itsfoss.com/content/images/wordpress/2022/07/Create-a-New-Git-Repository-with-git-init.png)

Create a New Git Repository With git init

This will create a new git repository. Your local repository consists of three “trees” maintained by git.

First one is your **Working Directory** which holds the actual files. Second one is the **Index** which acts as a staging area and finally the **HEAD** which points to the last commit you’ve made.

Checkout your repository (repository you just created or an existing repository on a server) using:

```
git clone /path/to/repository
```

### Add files and commit

You can propose changes using:

```
git add <filename>
```

Where “filename” is an existing file. This will add a new file for the commit. If you want to add every new file, then just do:

```
git add --all
```

Your files are added check your status using

```
git status
```

![add files for commit and check status](https://itsfoss.com/content/images/wordpress/2022/07/Add-files-for-commit-and-check-status.png)

Add Files for Commit and Check Status

As you can see, there are changes but they are not committed. Now you need to commit these changes, use:

```
git commit -m "Commit message"
```

![commit changes with inline commit message](https://itsfoss.com/content/images/wordpress/2022/07/Commit-changes-with-inline-commit-message.png)

Commit Changes With Inline Commit Message

You can also do (preferred):

```
git commit -a
```

![add commit message in nano editor](https://itsfoss.com/content/images/wordpress/2022/07/Add-Commit-Message-in-nano-editor.png)

Add Commit Message in nano Editor

And then write your commit message. Now the file is committed to the HEAD, but not in your remote repository yet.

### Push your changes

Your changes are in the HEAD of your local working copy. If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it first with:

```
git remote add origin <serveraddress>
```

Now you are able to push your changes to the selected remote server. To send those changes to your remote repository, run:

```
git push -u origin master
```

### Branching

Branches are used to develop features which are isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

Create a new branch named “mybranch” and switch to it using:

```
git checkout -b mybranch
```

![create a new branch and switch](https://itsfoss.com/content/images/wordpress/2022/07/Create-a-New-Branch-and-Switch.png)

Create a New Branch and Switch

You can switch back to master by running:

```
git checkout master
```

If you want to delete the branch use:

```
git branch -d mybranch
```

![switching to master and delete an existing branch](https://itsfoss.com/content/images/wordpress/2022/07/Switching-to-master-and-Delete-an-existing-branch.png)

Switching to master and Delete an Existing Branch

A branch is not available to others unless you push the branch to your remote repository, so what are you thinking about just push it:

```
git push origin <branchname>
```

### Update and Merge

To update your local repository to the newest commit, run:

```
git pull
```

In your working directory to fetch and merge remote changes.

To merge another branch into your active branch (e.g. master), use :

```
git merge <branch>
```

In both cases, git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible for merging those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with:

```
git add <filename>
```

Before merging changes, you can also preview them by using

```
git diff <sourcebranch> <targetbranch>
```

### Git log

You can see the repository history using:

```
git log
```

To see a log where each commit is one line you can use:

```
git log --pretty=oneline
```

![see log where each commit is one line](https://itsfoss.com/content/images/wordpress/2022/07/see-log-where-each-commit-is-one-line.png)

See Log Where Each Commit is One Line

Or maybe you want to see an ASCII art tree of all the branches, decorated with the names of tags and branches:

```
git log --graph --oneline --decorate --all
```

![log decorated with the names of tags and branches](https://itsfoss.com/content/images/wordpress/2022/07/log-decorated-with-the-names-of-tags-and-branches.png)

Log Decorated With the Names of Tags and Branches

If you want to see only which files have changed:

```
git log --name-status
```

And for any help during the entire process, you can use` git --help`

Congratulations, you are done with the basics of git. Isn’t Git awesome!!

*🗨️ Questions? Suggestions? The comment section is yours.*