Git CheatSheet
Tags: devops, github, automationtesting
Published: 2021-08-28 07:07:18

This guide includes an introduction on Git, a definition of terms, and lists of commonly used Git commands. If you're having trouble getting started with Git, or if you're an experienced tester looking for a refresher, this cheat sheet can help.

#What is GIT
Git is a free, open source distributed version control system tool designed to handle everything from small to very large projects with speed and efficiency. It was created by Linus Torvalds in 2005 to develop Linux Kernel. Git has the functionality, performance, security and flexibility that most teams and individual developers and testers need. It also serves as an important distributed version-control DevOps tool. 

#Git Terminologies:
###Repositories
Repositories can be compared to folders on your computer. A Github repository contains all of the project's files, including the documentation and all revisions. It is the most basic element of 
Github.

###Branch:
Branch is a version of the repository that diverges from the main working project. It is an essential feature available in most modern version control systems. A Git project can have more than one branch. We can perform many operations on Git branch-like rename, list, delete, etc

###Remote:
Remote is the term used to refer to the repository hosted in Github. A company's local Git repository hosted in Github or any other Git host platform for sharing and collaboration is generally called Remote repository.

###Master
The master is the primary branch of all your repositories. It should include the most recent changes and commits.

###Checkout
Use the git checkout command to switch between branches. Just enter git checkout followed by the name of the branch you wish to move to, or enter git checkout master to return to the master branch.

###Commit
Commit is one the most frequently used feature of Git, it’s like saving the updated file to its original folder and overwrite on already existing.A commit is a collection of content, a message about how you got there, and the commits that came before it.

###Fetch
Fetching refers to getting the latest changes from an online repository (like GitHub.com) without merging them in. Once these changes are fetched you can compare them to your local branches (the code residing on your local machine).

###Fork
A 'fork' is a personal copy of another user's repository on your GitHub account. Forks allow you to freely modify a project without affecting the original, providing limitless opportunities for experimentation and learning from the work of others.

###Head
A HEAD is a snapshot of the most recent commit on each branch. On each branch, it is a short name or a pointer reference to a SHA-1 of a commit ID.A HEAD always points to the branch's most recent commit (code) and moves forward with each commit to point to the most recent commit ID.

###Index
When you add, delete, or modify a file, it is retained in the index until you are ready to commit the changes. Think of it as Git's staging area. To view the contents of your index, use the git status command. Changes highlighted in green are ready to commit, while those highlighted in red must still be added to staging.

###Stash
Git stash removes changes from your index and "stashes" them for later use. It's useful if you want to put something on hold and work on something else for a while. You can only stash one set of changes at a time.

###Tags
Tags provide a way to keep track of important commits. Lightweight tags simply serve as pointers while annotated tags get stored as full objects.

###Upstream
In the context of Git, upstream refers to where you push your changes, which is typically the master branch.

###Merge
To add changes from one branch to another, use the git merge command in conjunction with pull requests.

###Origin
The default version of a repository is referred to as the origin. Origin can also be used as a system alias to communicate with the master branch. To push local changes to the master branch, use the command git push origin master.

###Pull
Pull requests represent suggestions for changes to the master branch. If you're working with a team, you can create pull requests to tell the repository maintainer to review the changes and merge them upstream. The git pull command is used to add changes to the master branch.

###Push
Pushing refers to sending your committed changes to a remote repository such as GitHub.com. For instance, if you change something locally, you'd want to then push those changes so that others may access them

#GIT Commands
Here are some basic GIT commands you need to know

###How to setup your Git username:
With the command below you can configure your user name:

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxqehkh2zgws87a4idgw.png)

###How to setup your Git user email:
This command lets you setup the user email address you'll use in your commits.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zrcec3vmxyzrgacnlq7.png)
 
###How to Initialize a Git repo:
Everything begins here. The first step is to create a new Git repo in your project's root directory. You can do so with the command below:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dopv8yzlxepr68k35hx.png)
 
###How to clone a Repository:
Git clone is a Git command line utility which is used to target an existing repository and create a clone, or copy of the target repository

####Cloning to a specific folder 
Clone the repository located at ＜repo＞ into the folder called ~＜directory＞! on the local machine.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dba2w4mhqpdz5nypedw8.png)
 
###How to Create a New Branch in Git:
By default, there is only one branch, the main branch. You can use this command to create a new branch. Git will not switch to it automatically; you must do so manually with the following command.

####To create a new branch use:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1u2lcimfylxe6e5lcwj.png)
 
###How to List Branches in Git:
Using the git branch command, you can see all of the branches that have been created. It will display a list of all branches, with the current branch marked with an asterisk and highlighted in green.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmm583z1ip1cre44vpfz.png)
 
###How to Switch Branches in Git:
When you create a new branch, Git will automatically switch to it.
If you have multiple branches, you can easily switch between them by running git checkout:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdw9neczu5lasm45zlum.png)
 
###How to delete a branch in Git:
You can delete the branch using below command

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymcyb4g6v6z4jt69kleq.png)
 
###How to add a file to the staging area in Git:
The command below will add a file to the staging area. Simply replace filename here with the name of the file you want to include in the staging area.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0wr8531ndth20apihfi.png)
 
###How to add all files in the staging area in Git:
You can use a wildcard to add all files in your project to the staging area.

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8jr6ydusb0p8u4s43m1.png)
 
###How to add only certain files to the staging area in Git:
You can add all files beginning with 'str' to the staging area by using the command below.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbfekf0gy5csuhsamzgk.png)
 
###How to Check repository's status in Git:
This command displays the current repository's status, including staged, unstaged, and untracked files.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkapfkb9olqghk50rgpa.png)
 
###How to Commit changes with a message:
This command allows you to specify a brief summary of your commit message.

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wjemui8pxtjbzg5wqqe.png)
 
###How to Commit changes (and skip the staging area) in Git:
Using the -a and -m options, you can add and commit tracked files with a single command.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tflvmugbcca870in7enx.png)
 
###How to View your commit history in Git:
This command displays the commit history for the current repository:

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i239opy48993hy6stnfv.png)
 
###How to view commit history including changes in Git:
This command displays the current repository's commit history:

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3zv0juyei52u78r6bb3.png)

###How to view log stats in Git:
This command causes the Git log to display some statistics about the changes in each commit, such as the number of lines changed and file names.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdg9jr2glcog3oyc1vex.png)
 
###How to Amend the most recent commit in Git:
You can use git commit —amend to modify and add changes to the most recent commit.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcxc6jis8dunz4lpc2ff.png)
 
###How to view a specific commit in Git
Replace commit-id with the id of the commit found in the commit log.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rqlr7hoba014w4rg656.png)
  
###How to view changes made before committing them using "diff" in Git:
You can specify a file as a parameter to only see changes to that file. By default, git diff displays only unstaged changes.

####To see any staged changes, we can use diff with the —staged flag.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehqgrseiiq0kpedao1jt.png)
 
###How to revert staged changes in Git:
If you want to remove a certain file from the stage:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q95ptbdbj7wtc5faww94.png)
  
####Remove all staged files:
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vuyh9i2cqig50sr69c8.png)
 
###How to Rename files in Git:
This command stages the changes, then it expects a commit message.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qp5svj9zuyuzvf6ogpl.png)
 
###How to push changes to a remote repo in Git:

When all of your work is complete and ready to be saved on a remote repository, use the commands below to push all changes:

####First Push
Push a local branch for the first time:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jal86r9cs86jvs0oc4fn.png)
  
####After that, then you can just use:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7nc8ghrjef6to3m7dd9.png)
  
####Push local branch to different remote branch
To push a local branch to a different remote branch, you can use:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5x5chogvmu813tg86wv.png)
  
###How to Undo Last Push
If you have to undo your last push, you can use below command.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grr0f43rp5oa99kitvia.png)
 
###How to fetch remote repo changes in Git:
This command will download changes from a remote repository but will not merge them into your local branch (use git pull to do that).

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vv6zagquwrw2hg06qdy2.png)
 
 
###How to pull changes from a remote repo in Git:
If other team members are working on your repository, you can use the command below to retrieve the most recent changes made to the remote repository:

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uputg5ykeo8843y21tr.png)
 

####If you have a branch called my_feature and you want to pull that branch, you can use:

 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlxktsjpxl1hay4xzh5q.png)
 

###How to merge two branches in Git or Rebase:
When you run git merge, your HEAD branch will generate a new commit, preserving the ancestry of each commit history.
The rebase re-writes the changes of one branch onto another without creating a new commit.

####Merge Master Branch to Feature Branch:
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpyvmkrxsoprgb2kmlrn.png)

####Or with rebase option, use below command:
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2417c0ekg8ohsmbk8on.png)
 
####Merge Feature Branch to Master Branch:
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7qd6ahnj7dxqmjlfdsg.png)
 
###How to use GIT STASH:
Git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. 

####To stash changes use below command

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks8irhxs1nuyr5exai2l.png)
 
####If you want to unstash those changes and bring them back into your working directory use:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1u11mvsmup5h0f4ce84.png)