Top 10 basic git commands that every developer should learn

navya , Credit to  volkotech-solutions Dec 24
List of git commands banner image

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

  1. Configuring your details
  2. Cloning repositories
  3. Get the status of your project
  4. Adding files/folder
  5. Commit message for your respective work
  6. Pushing your files/folders
  7. Creating branches
  8. Changing from one branch to another
  9. Merging branches
  10. 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.Configuring git account as a global user

2. Cloning repositories

After configuration, you have to clone the repository into your local computer. We can clone the repository in two methods, 

  1. Cloning repository using ssh key
  2. 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.
Copying repository using ssh key

2. Cloning repository using HTTPS

In the same way, just switch from ssh to HTTPS and copy the respected url as shown below.
Copying repository using https

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.Cloning the git repo with ssh key using Ubuntu terminal

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,
Validating cloned repo files in Ubuntu file explore

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.

Checking untracked files with git status using Ubuntu terminal

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

Adding files to git using Ubuntu terminal

To add folder

    git add folderName

    Adding folder to git using Ubuntu terminal

    To add all unstaged files

      git add . 

      Adding all the unstaged files to git using Ubuntu terminal

      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”

      Commiting the changes to git using Ubuntu terminal

      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

      Pushing the changes to git using Ubuntu terminal

      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>

      Creating branches in git using Ubuntu terminal

      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 

      Showing all the available branches in git using Ubuntu terminal

      to get all the remote branches the command is

      git branch -r 

      Showing all the remote branches in git using terminal

      to get all the local branches the command is

      git branch 

      Showing all the local brances in git using Ubuntu terminal

      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>

      Creating branch in git using Ubuntu terminal

      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>
      

      Checkout to respective git branch using Ubuntu terminal

      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.Merging git branches using Ubuntu terminal  After the command, you can see the merge commit screen and you have to give the commit message related to your work.Writing merge commit in nano editor using Ubuntu terminal

      Save the commit message and push it to the main branch.

      Pushing files to git using Ubuntu terminal

      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
      

      Taking latest code in the respective branch using Ubuntu terminal

      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

      Authors

      Read Next