Basic Git Commands Every Developer Should Know
Hey, are you a software developer and you don’t know the basic git commands and which git command is used for which purpose? Don’t worry! Here i will explain you the usages of basic git commands that you should know as a software developer. So Let’s get started.
This command is used to set your username and email in the main configuration file. To check your username and email if its already configured, type below command in git bash:
git config –global user.name to know your username
git config –global user.email to know your email
If you wanted to set your username and email in this configuration, then follow the below commands:
git config –global user.name “Nahidur Rahman”to set your username
git config –global user.email “firstname.lastname@example.org”to set your email
This command is used to initialise a git repository for a new or existing project. Just go into the root of your project directory and type git init in the git bash, then hit enter and you are ready to go.
When you wants to copy a git repository from remote source to your local machine. Also you will sets the remote to original source so that you can pull again. To clone remote repository, type the following command in your git bash:
git clone <repository-url>
To check the status of files you have changed in your working directory. This command will show you what all has changed since your last commit that you have made. Just type the command git status in your working directory and hit enter. Then it will list out all the files that have been changed.
Cool! Right. Yes i know. I am using git everyday as a part of my software development. Here is more commands that you need to know as well.
This command adds changes to stage or index in your working directory. Just type the command git add –all and it will staged everything that have been changed.
This will commit your changes and sets it to new commit object for your remote You must keep in mind that your commit message should be as simple as possible to readable by anyone. You can follow the below command:
git commit -m “you simple sweet readable commit message”
It will push your changes to your remote repository if you have added and committed your changes and you want to push them, then use the below command:
git push <remote name> <branch name>
As like as git push command, it will not pushed your changes to your remote repository. What it does is, it will pull all the changes from your remote repository that you don’t have in your local repository. Use the following command to pull from your remote repository:
git pull <remote name> <branch name>
This command git branch will show you all the branches that you have in your local repository. If you wants to list out all the remote branches as well, then just type git branch -a and it will show you the remote branches as well.
When you wants to switch between current branch to a different branch, then this command will help you to do that. Just follow the below command to achieve it.
git checkout <branch name>
If you want to create and switch to a new branch, then type git checkout -b <branch name>
This command will save changes that you don’t want to commit immediately. Just type the command git stash in your working directory. If you want to bring your saved changes back, then write the command git stash apply.
When you want to merge two branches that you are working on, this command will merge your two branches. First, switch to branch you want to merge everything. The type the command
git merge <branch name you want to merge with>
When you commit changes that are not complete, then this will sets your index to the last commit that you want to work on with. To reset your repository in a particular state, then use the following command:
git reset <mode> <commit>
If you wants to check what remote or source you have, then the command git remote will show you the remote name list. And if you want to set your remote name, the following command will do that job for you:
git remote add <remote name> <remote-url>
That’s all about git i think you should know as a software developer. These are the essential commands that we have using everyday in our project as needed. Let me know if you have any suggestions about this by leave a comment in the comment section below. Also checkout out this free online courses on git to improve your skills.