Git CheatSheet
Tags: github, git, linux, cloud
Published: 2022-10-02 02:02:16

# Git (Version Control)

Git is an essential collaborative part of any programmer/developer/DevOps engineer's daily routine. Git is widely used in the industry with version control platforms such as Github, Bitbucket, etc.

There is a multitude of commands to use for Git; mastering them takes time and effort. In this post, my aim is to share with you some of the more frequently used commands in Git that every developer should know.

## Git Clone 

Git clone is a command that is used for downloading existing source code from a remote repository (Github, Bitbucket). Basically, git clone makes an identical copy of the latest version of a project in a repository and saves it within your local filesystem on your local machine.

Generally, I **clone with https**:

` git clone <https://name-of-the-repository-link>  `


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8m74u96mdxuajtyy9ty.png)

This will make a copy of the repository to your local machine so you can start working with it, make changes, etc.

## Git branch

By making use of branches in Git, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing, and deleting branches.

### Creating a new branch:

This command will create a branch locally. 

`git branch <branch-name>`

To push the new branch into the remote repository, here is the command to use:

`git push -u <remote> <branch-name>`

### Viewing branches:

`git branch or git branch --list`

### Delete a branch:

`git branch -d <branch-name>`


## Git checkout

This is also one of the most used Git commands. In order to work in a branch, we first need to switch to that branch. We use **git checkout** mainly for switching from one branch to another. It can also be used for checking out files and commits.

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

- The changes in your current branch must be committed or stashed before you switch

- The branch you want to checkout should already exist in your local directory.

Here is a shortcut command that will allow you to create and switch to a branch at the same time:

`git checkout -b <name-of-your-branch>`

This command creates a new branch in your local (-b means for branch) and checks the branch out to new right after it has been created.


## Git status

The Git status command gives all the necessary information about the current branch.

`git status`

This information is very important, it provides:

- Info on whether the current branch is up to date
- Info on anything to commit, push or pull
- Info on files stages, unstaged or untracked.
- Info on files created, modified, or deleted.

## Git add

Whenever we create, modify, or delete a file, these changes will happen in our local and will not be included in the enxt commit.

we need to use the git add command to include the changes of a file(s) into our next commit.

### Add a single file:

`git add <file>`

### Add everything at once:

`git add -A`

The **git add** command doesn't change the repository and the changes are not saved until we make use of **git commit**


## Git commit

The famous **Git commit** command is probably the most used command of Git. To save our changes we make use of git commit.

Git commit is like setting a checkpoint in the development process which you can go back to later if needed.

We are able to write a short message explaining what you have developed or changes in the source code.

`git commit -m "commit message`

**PS: Git commit saves your changes only locally**


## Git push

Making use of **Git push** is for sending your changes to the remote server (remote repository) after committing your changes. 

`git push <remote> <branch-name>`

If your branch is newly created, please upload the branch with the following command:

`git push --set-upstream <remote> <name-of-your-branch>`

**or**

`git push -u origin >branch-name>`

**PS: Git push only uploads changes that have been committed**

## Git pull

The **git pull** command is used to get updates from the remote repository. This command is basically a combination of **git fetch** and **git merge** which means that, when we use git pull, it gets the updates from the remote repository (git fetch) and immediately applies the latest changes in your local (git merge).

`git pull <remote>`

## Git revert

The **git revert** command is used to undo the changes that we've made. 

To see your commit history, make use of this command: **git log --oneline** 

Specify the hash code next to your commit that you would like to undo:

Example: 4442389

`git revert 4442389`

The Git revert command will undo the given commit, but will create a new commit without deleting the older one.

The advantage of using **git revert** is that it doesn't do anything to the commit history. Also, everything happens in our local system unless we push it to the remote repository.

## Git merge

Once you have completed development in your branch and everything works as intended, the final step is to merge the local branch with the parent branch (remote branch). This can be accomplished with the **git merge** command.

### Before merging, you should update your local development branch:

`git fetch`

### Then, we can merge your feature branch into the remote branch:

`git merge <branch-name>`

**PS: Please ensure your local development branch has the latest version before you merge your branches, otherwise, you may face conflicts or other unwanted issues with versioning and state control**

I hope these frequently used Git commands will help all developers in their daily tasks. There are many more things to learn about Git and complex ways of versioning, therefore, I encourage you to continue learning Git and take things future.

Thank you @CemEygi