A set of handy git commands for beginners to make your life easier!
Tags: git, beginners, productivity, webdev
Published: 2022-04-28 12:52:46

Starting out with version control is exciting however keeping track of all the commands can be difficult.

This is a simplified cheat sheet with short explanations to guide you on which command does what and if the changes made are locally, remotely or both.

<table>
<tr><td id='toc'><b>Table of Content</b></td></tr>
<tr><th><a href='#setup'>First-Time Git Setup</th></tr>
<tr><th><a href='#create'>Creating A Repo</a></th></tr>
<tr><th><a href='#local-changes'>Managing Local Changes (including undos)</a></th></tr>
<tr><th><a href='#branch'>Managing Branches</a></th></tr>
<tr><th><a href='#pp'>Pushing And Pulling</a></th></tr>
<tr><th><a href='#fetch'>Fetching</a></th></tr>
<tr><th><a href='#merge'>Merging</a></th></tr>
</table>


## 1. <span id='setup'>First-Time Git Setup</span> &#9874;

Once git is installed on your system you have to customize your git environment. This can be done using the `git config` tool

### i. To setup your Git username

```
git config --global user.name <username>
```
**Example**
```
git config --global user.name janedoe
```

### ii. To setup your Git username

```
git config --global user.email <emailAddress>
```
**Example**
```
git config --global user.email janedoe@gmail.com
```

### iii. To check Git configuration

```
git config -l
```
<br>
<a href='#toc'>&#129093; back to TOC</a>
<br>

## 2. <span id='create'>Creating A Repo</span> &#128394; 

These are probably the first command every git enthusiast learns. 

### i. To create a local repo

```
git init
```

You should run this command in the folder you want to become the root(base) folder for your repo

### ii. To clone an existing repo

```
git clone <repo-clone-link>
```

**Example:**

```
git clone https://github.com/user/repo.git
```

Ensure that the link is copied from the **green clone button** NOT the browser tab

![clone button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7c1bpgnrrtj2vy5qwcu.png) ![open button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvhtkqlgpwvohbngygm4.png)
<br>

### iii. Linking to a remote repo

```
git remote add origin <link-to-github-remote-repo>
```

**Example:**

```
git clone https://github.com/user/repo.git
```
<br>
---
<br>
<a href='#toc'>&#129093; back to TOC</a>
<br>

## 3. <span id='local-changes'>Managing Local Changes (including undos)</span> &#128736; 
<br>
### i. Checking files you've worked on without committing (unstaged files)

```
git status
```

### ii. To stage files<br>

- To stage particular files, you'll have to specify the filenames (including the extensions)

```
git add <filename>
```

**Example:**

```
git add index.html
git add index.html styles.css app.js
```
To check which files to add run `git status` 

- To stage all files at once

```
git add .
```

 **Note:** Do no forget the decimal point

### iii. To unstage (unadd or uncommit :smile:) files

These can be used for commits that have **not** been pushed

To unstage a  single file

```
git reset HEAD <filename>
```

**Example:**

```
git reset HEAD index.html
```

To unstage **all** stage files

```
git reset HEAD
```

To unstage the last file you staged

```
git reset HEAD~1
```

**Note:** Be sure that you don't have any additional changes to the files<br>

### iv. To commit staged files<br>

Once you've stage a file you can then commit it

```
git commit -m "<commit-message>"
```

**Example:**

```
git commit -m "initial commit"
```

**Note:** Do not forget the commit message

To commit means to capture a snapshot of the project's currently staged changes and "commit it to git memory"

### v. To stage and commit all changes at once

```
git commit --am "<commit-message>"
```

**Example:**

```
git commit -a -m "styles and design done"
```
<br>

### vi. To correct your last commit message

```
git commit --amend -m "<commit-message>"
```

**Example:**

```
git commit --amend -m "Corrected: first commit"
```

**Note:** Don't amend published(pushed) commits!

###vii. To rollback commits

**_These are for commits that have been pushed_**

To rollback the **latest** commit
```
git revert HEAD
```

To rollback to a specific commit 
```
git revert <commitID>
```
<br>
---
<br>
<a href='#toc'>&#129093; back to TOC</a>
<br>

## 4. <span id="branch">Managing Branches</span>
<br>

### i. To list all existing local branches

```
git branch 
```

**Example:**

![git branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oufakbjnj81jm6xlga7u.png)
<br>
### ii. To list all branches (remote and local)

```
git branch -a
```
<br>
### iii. To create a new branch on the current head <br>

You can think of the HEAD as the "current branch". When you switch branches with git <a href="#checkout">checkout</a> (switch branches), the HEAD revision changes to point to the tip of the new branch

```
git branch <branch-name>
```

**Example:**

```
git branch develop
```
<br>
### iv. <span id="checkout">To switch to an existing branch an update current working directory</span><br>

 ```
git checkout <branch-name>
```

**Example:**

```
git checkout develop
```

### v. git checkout -b <branch-name>

Creates new branch from current branch(the branch you are currently working on) and checkout out to it. 

**_This move can be done even when you have unstage files. The untracked files will now be in the new branch_**
<br>
### vi. To delete a branch

To delete a merged branch locally. Do not be on the branch to be deleted.

```
git branch -d <branch>
```

To delete a branch locally whether merged or not. Do not be on the branch to be deleted.

```
git branch -D <branch>
``` 
**Note:** These delete the local branch **only**
<br>
To delete a **remote** branch from terminal.

```
git push origin --delete <branch>
```
**Note:** This deletes the remote branch **only**

### vi. To rename a branch

To rename a branch while on another branch

```
git branch -m <oldName> <newName>
```

To rename your current working branch

```
git branch -m <newName>
```

If you want upstream to reflect your changes, that is the branch upstream to have the same naming as your local branch

```
git push origin -u <newName>
```

### vii. To set local branch to track remote branch

```
 git branch --set-upstream-to=origin/<remoteBranchName> <localBranchName>
```
### vii. Pushing a branch for the first time

If you clone a project, created a branch, worked on it and want to push it to upstream for the very first time

```
git push --set-upstream origin <branchName>
```

After this is done, just do `git push` whenever you wish to push that branch to the one upstream

<br>
<a href='#toc'>&#129093; back to TOC</a>
<br>

## 5. <span id="pp">Pushing And Pulling</span>

### i. <span id='track'>To push local branch for the first time</span>

This is to push a branch that was created locally for the very first time. That is, it does not exist remotely.

```
 git push --set-upstream origin <branchName>
```
 **OR** 

For short

```
git push -u origin <branchname>
```

### ii. To push

To push a branch which already exists and has already been set to an upstream
```
git push
```
**Note:** The local branch must be set to track to remote branch. Use <a href='#track'>this command</a> to set the branch to upstream.


### iii. To push to a specific origin

```
git push origin <remoteBranchName>
```

### iv. To pull 

To pull from the remote branch and directly merge/integrate into HEAD recursively

```
git pull
```

Use <a href='#track'>this command</a> to set the branch to upstream.

### v. To pull from a specific origin

To pull from the specific remote branch and directly merge/integrate into HEAD

```
git pull origin <branchName>
```
<br>
<a href='#toc'>&#129093; back to TOC</a>
<br>
## 6. <span id='fetch'>To fetch </span>

### i. Synchronizing to the central repository

To synchronize your local **repository** to that of the central repository's main branch

```
git fetch origin
```
Any branch that was behind the central repository's main branch will be downloaded.

### ii. <span id='sub-fetch'>Fetching a remote branch</span>

To fetch and update a branch exists locally and remotely or to fetch a remote branch locally. Checkout to that branch

```
git fetch <remote> <branch>
```

<br>
<a href='#toc'>&#129093; back to TOC</a>
<br>
## 7. <span id="merge">Merging</span>

Merging is simply combining (or merging) branches
As a beginner with git, when working in a team with others, I merge my branches on Github (after creating pull requests) so I could compare changes and easily resolve any conflicts. Then pull the work down to my local branch

### i. <span id='local-merge'>Merging two local branches</span>

On the terminal, to merge a branch into another, you have to be in the branch you want to merge into. Then run the command

```
git merge <branch-to-be-merged>
```

### ii. Merging a remote branch into a local branch 

First <a href='#sub-fetch'>fetch</a> the branch, then <a href='#local-merge'>merge</a> using the merge command <a href='#local-merge'>above</a>