by BehindJava

Git cheatsheet that all coders need to know, Ever accidentally deleted files or necessary code? Or do you wish to look back at an older version of your code?

Home » interview » Git cheatsheet that all coders need to know, Ever accidentally deleted files or necessary code? Or do you wish to look back at an older version of your code?

In this tutorial we are going to see important Git commands that one needs to know while using Git.

Firstly we will know the terms used in Git.

  • Repository
  • Remote repository
  • Local repository
  • Commits
  • Branches

Lets start, Once Git Bash is installed in our local machine we can go ahead with the below commands in configuring the Git repository.

1) git config

$ git config --global user.name "John Doe"
$ git config --global user.email "johndoe@email.com"

This command is to set up aliases.

$ git config --global alias.co commit

2) git init

//Run the following command inside the folder
$ git init
//Run the following command to create a new directory that is a git repo
$ git init DIRECTORY_PATH/DIRECTORY_NAME

3) git clone

$ git clone LINK
$ git clone https://github.com/Deepak607398/behindjava.git

4) git fetch

//download the state for all the branches
$ git fetch 
//fetch for just one branch
$ git fetch <remote> <local> 
//<remote> is the name of the remote branch
//<local> is the name of the local branch
//an example of it is 
$ git fetch origin master

5) git pull

git pull [<options>] [<repository> [<refspec>…​]]

6) git status

$ git status //a sample output of this command is as follows
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	README.txt
	lab1

7) git add .

Once all the changes are made in the project or folder in which the Git is configured. We can use the the git add . which adds all the latest changes.

$ git add .

Now when you run the git status command, you will see this file name is written in green because Git is up to date with this file.

If you need to delete a file or folder, you can check out the git rm command.

8) git commit

$ git commit -m "The message you want to write to describe this commit"

The -m flag helps write a message that describes the commit.

9) git push

Once our code is ready and we want to have it in our Git repository we can use git push command.

$ git push <remote> <local> 
//<remote> is the name of the remote branch
//<local> is the name of the local branch
//an example of it is
$ git push origin master

The above command pushes our local commits to the master branch.

10) git log

In the development environment there will be multiple people performing multiple commits and we need to know the detailed commit history.

$ git log --graph --oneline --decorate
* 345e553 (HEAD -> master, origin/master) mutation testing
* 749c511 Banking Use
* a6b6fc0 Junit test case
* 98ccd11 profile swagger validations

Here, the alphanumeric codes we see at the start of each line represent each commit and will be used if we want to revert or perform other functions. Also, see git shortlog.

11) git revert

For git revert, we will need the commit codes that we saw earlier in the log.

$ git log --oneline
345e553 (HEAD -> master, origin/master) mutation testing
749c511 Banking Use
a6b6fc0 Junit test case
98ccd11 profile swagger validations

$ git reset --hard c14809fa
//this command will not changes files that you have not git added 

There are many other ways to revert, so do check those out once, in case you need them.

12) git branch

This command lets us create, list, rename, and delete branches. Let’s look at a few examples.

//this lists the name of the branches present
$ git branch 
main 
another_branch 
feature_inprogress_branch
//delete a branch safely
$ git branch -d <branch>
$ git branch -d another_branch

13) git checkout

The git checkout command lets you navigate between the branches created by git branch.

//switch to a different branch
$ git checkout <branch_name>
$ git checkout another_branch
//create a new branch
$ git checkout -b <new_branch_name>
$ git checkout -b new_feature_branch

14) git diff

There are times when we will need to compare the code between versions or between branches; that is when we use git diff.

//print any uncommitted changes since the last commit.
$ git diff
//compare code between two branches
$ git diff branch1 branch2
//print the uncommitted changes made in one file
$ git diff /filepath/filename

15) git rebase

Rebase is one of two Git utilities that specializes in integrating changes from one branch onto another. The other change integration utility is git merge. Merge is always a forward-moving change record. Alternatively, rebase has powerful history rewriting features.

Let’s see a visual of what git rebase does.

    B -- C (another_branch)
   /      
  A--------D (master)
Rebasing another_branch onto master
             
             B -- C (another_branch)
            /      
  A--------D (master)
The corresponding code
$ git pull origin master
$ git checkout another_branch
$ git rebase master