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.
- Remote repository
- Local repository
Lets start, Once Git Bash is installed in our local machine we can go ahead with the below commands in configuring the Git repository.
$ git config --global user.name "John Doe" $ git config --global user.email "firstname.lastname@example.org"
This command is to set up aliases.
$ git config --global alias.co commit
//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
$ git clone LINK $ git clone https://github.com/Deepak607398/behindjava.git
//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
git pull [<options>] [<repository> [<refspec>…]]
$ 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
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.
$ git commit -m "The message you want to write to describe this commit"
The -m flag helps write a message that describes the commit.
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.
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.
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.
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
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
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
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