This blog presents the top 10 basic Git commands that every dev should be familiar with, as they improve productivity & efficiency when working with Git.
The 10 commands that we are going to learn are
- Configuring your details
- Cloning repositories
- Get the status of your project
- Adding files/folder
- Commit message for your respective work
- Pushing your files/folders
- Creating branches
- Changing from one branch to another
- Merging branches
- Getting other peer code
1. Configuring your details
The first and most important step is configuring your details, so that git can understand you are the authenticated user and can securely store your work. To configure your details use the below commands.
#your git hub user name
git config --global user.name "username"
#your git hub email address
git config --global user.email "MY_NAME@example.com"
In the below screenshot, as mine is already configured, the status message is empty.
2. Cloning repositories
After configuration, you have to clone the repository into your local computer. We can clone the repository in two methods,
- Cloning repository using ssh key
- Cloning repository using HTTPS
1. Cloning repository using ssh key
First, go to your git account and create a repository and the repository can be either public or private, and copy the ssh key which is present at the right corner as shown below.
2. Cloning repository using HTTPS
In the same way, just switch from ssh to HTTPS and copy the respected url as shown below.
After getting the url the only thing you need to do is use the below command and clone the repository
git clone <repository url>
#cloning with ssh
git clone git@github.com:kosarajunavya/kosarajunavya.github.io.git
#cloning with https url
git clone https://github.com/kosarajunavya/kosarajunavya.github.io.git
Now I’m cloning my repository into my local computer desktop, have a look at the below screenshot.
For setting up ssh and creating repositories in the git hub, please have a look at the below blog https://mycode.blog/navya/creating-repository-git-along-ssh-key-set. Now you have one cloud that is your own repository so that you can add your files and can access them from anywhere at any time with internet access.
3. Get the status of your project
After cloning, you will get a folder like this,
All the folders available in my repository will be cloned here, so if you want to add files/folders and you always need to have a copy of that folder you can add in this repository directory in your local computer and push it to your git repository.
Now add some files or required folders in the cloned repository folder, the go-to command line, and change your present working directory to your repository directory with the command cd repository name, which you can see in the below screenshot.
Now the question is how will you get to know whether the local repository is updated or not with our changes, for that git provide one command to get to know about project status and that is
git status
This command will give you the unstaged files, unstaged in the scene the updated files or folders. Have a look at the below screenshot, all folders/files that appeared in red color are unstaged files and we need to add those files to the repository.
4. Adding files/folder
Here comes a question about how to add files/folders to the git repository and access them, for that we have a solution in the form of the below commands.
Note:
Always remember, before adding files to your repository you have to do git status, to know about untracked/unstaged files.
To add file
git add fileName
To add folder
git add folderName
To add all unstaged files
git add .
5. Commit message for your respective work
After adding your files you have to give a proper message like in which part of the project or folder or file you made changes and for what purpose. Simply the one line of your commit message should reflect your work.
git commit -m “message”
6. Pushing your files/folders
After your commit message, then do git status to get clarity of which files are committed then do the following command to push your files/folders
git push origin master or git push origin main
In the above screenshot, you can see the clear status message that your main branch is ahead of one commit, so then push your changes to the repository. Then go to your git hub profile and check whether the changes are reflected or not.
7. Creating branches
The major purpose of using git is for team collaboration, when multiple people work together, it is better to maintain different branches to avoid conflicts. For that, we have a command to create different branches for individual work.
Most flexible commands used to create git branches are
1. Creates branch with the given name and directly moves to that branch
git checkout -b <branchName>
Now to check whether the branch is created or not run the following commands respectively.
to get all the available branches the command is
git branch -a
to get all the remote branches the command is
git branch -r
to get all the local branches the command is
git branch
Now you can see all the available branches.
2. Create a branch with a name and after checking out to that branch
git branch <branchName>
In the above screenshot, you can see the new branch teamVolkotech is created.
8. Changing from one branch to another
After creating a branch, to have your folders in the respective branch first, you need to check out to the respective branch from master why because by default the git will be in the master branch. For that the required command is
git checkout <branchName>
You can see a clear status message that switched to branch volkotech, now add, commit and push your changes of the related folders in this branch. Look at the below screenshot.
9. Merging branches
If you want to merge your code to the main or master branch, you can do with the following command.
git merge <branchName>
Look at the following screenshots of the steps to merge the branches. After the command, you can see the merge commit screen and you have to give the commit message related to your work.
Save the commit message and push it to the main branch.
10. Getting other peer code
As every successful project has a great effort from the team, you should always get the latest code and append your code as per requirement. For that, you have to pull the code and the command is
git pull origin master
#Or
git pull origin main
Finally, I want to conclude that, learning git is a very basic skill of every developer, and here are very basic commands that can help you a lot. So guys go on and get started on learning git.
Comments