DEV Community

Cover image for Top 12 Git Commands that Every developer must know
rishika vishnoi
rishika vishnoi

Posted on

Top 12 Git Commands that Every developer must know

What is Git?

Git is a version control and open source software that developers worldwide use. It is free for all.
It stores the file, tracks the history, and merges the code changes.
Developers use git to keep track of the updates to a large codebase, roll back to the previous version if required and see any changes that were made as well as who made them and when.
There are a lot of git commands but some commands are more regularly used. So I'm going to share the most useful git commands that every developer must know.

But before moving further let's see some features of git.

Features of Git

  • Branching:- A repository can contain multiple branches and if anybody wants to contribute or work on any open source projects so they can create a branch parallel to the main branch and work locally without affecting the main branch.
  • Distributed development:- Git is fully distributed and it's one of the main features of git. It means you can clone someone's open-source project. Git gives each developer a local copy of the entire development history and can change locally, these changes are imported as additional development branches, and can be merged in the same way as a locally developed branch.
  • Free and Open Source:- Git is a free and open source software which means anyone can use it without any charge and can start contributing to any codebase.
  • Tracks History:- Git use to keep track of the history of the codebase so developers can revert to the earlier version if needed and see the changes and also can update that.
  • Scalable:- Git is scalable which means as the number of user increase day by day, the git has the capability to easily handle such situation
  • Allows for non-linear development:- Developers access the git repository, worked on it, and update the project at any time they want so Git allows for development in a non-linear fashion. Git supports such a kind of development by providing its branching and merging features, and it uses specific tools for navigating through them.
  • Fast & provide Security:- Git is lightning-fast technology and it also provides security to the developers. Git uses the SHA1 (Secure Hash Function) to name and identify objects within its repository Every file and commit is check-summed and retrieved by its checksum at the time of checkout.

1- Git Config

The git config command sets configuration values for your Git installation. Before you start using Git for a project, you will use this command to configure your Git name and email on your computer.

# sets up Git with your name

git config --global user.name "<Your-Full-Name>"

# sets up Git with your email

git config --global user.email "<your-email-address>"

Enter fullscreen mode Exit fullscreen mode

2- Git init

The Git init command is used to create a new blank git repository, so this is the first command that you'll run in new projects. The 'init' stand for initialization so basically it is used to initialize the git setup.
This command creates a .git directory in the project that keeps track of all the information and metadata.

git init
Enter fullscreen mode Exit fullscreen mode

3- Git clone

Git clone is basically making a copy of any repository that you want and saves in your local computer.
It is the best way to download the source code of any project through git. you can make a clone of any remote repository by accessing its URL.

  git clone <https://github.com/<repo-url>
Enter fullscreen mode Exit fullscreen mode

4- Git add

Git add command is used to add your changes to the staging area. Before committing the changes to the remote repository, you need to add changes to the staging area by using the add command. It is an important command because unstaged files won't be included in commits.

To add single or multiple files by specifying them.

    git add <filename> 
    or 
    git add <filename1> <filename2> ... <filenameN>
Enter fullscreen mode Exit fullscreen mode

To add all files

git add . 

Enter fullscreen mode Exit fullscreen mode

5- Git Status

The git status command displays the all necessary information about the current branch.
It displays -

  • whether files are unstaged, staged, and untracked.
  • whether the current branch is up to date or not.
  • Whether there is anything to commit, push or pull
  • Whether there are files created, modified or deleted
  git status
Enter fullscreen mode Exit fullscreen mode

6- Git log

The git log command displays all the commands in the repository's history. It records information about all the commits made to the current branch so far and we can navigate by using log commands.

git log
Enter fullscreen mode Exit fullscreen mode

7- Git commit

Git commit is the next command after git add, it uses to save currently staged changes to the projects.
It saves the changes to the local repository and now you can also push your changes to the master branch. It creates a commit with a passed commit message. By default, git commit opens the locally configured text editor causing a commit message to be entered.

  git commit -m "<message>"
Enter fullscreen mode Exit fullscreen mode

8- Git branch

The Git branch command is used to list all existing branches, add a new branch, and delete a branch.
Branching is the most important feature of git which help to work and make changes parallel to the main branch.
This command is used to create a new branch -

  git branch -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

This command is used to view the existing branch -

 git branch 
 or 
 git branch --list 
Enter fullscreen mode Exit fullscreen mode

This command is used to delete the branch -

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

9- Git Checkout

The Git checkout branch is used to switch one branch with another branch.
you can only use it if

  • the branch you want to switch to must be present in your local repository.
  • all the modifications or changes made to the current repository must be committed or stashed before switching.
git checkout <branch-name>

Enter fullscreen mode Exit fullscreen mode

10- Git push

Git push is an important command. The git push command is used to push the committed change from your local repository to the remote repository.

git push <remote> <branch-name>
Enter fullscreen mode Exit fullscreen mode

However, if your branch is newly created, then you also need to upload the branch with the following command:

git push -u origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

11- Git pull

Git pull is used to get updates from a remote repository. It accesses the changes or commits from the remote repository to the local repository. Git pull command fetches and downloads content from the remote repository and integrate changes to the local repository. The commit is uploaded by git push and downloaded by git pull and git fetch.

git pull <remote>
Enter fullscreen mode Exit fullscreen mode

12- Git merge

The main use of git merge is to join two branches, It is also used to merge multiple commits into one history. When you have completed development in your branch and If there are no conflicts, then git merge command merge your branch to the parent branch.
Merging process-

  • firstly Run git checkout <receiving branch> to switch to the receiving branch.
  • The next step is to fetch the latest remote commits by using

    git fetch

  • Final step to executing with the branch name

git merge <branch name>
Enter fullscreen mode Exit fullscreen mode

Conclusion

 So these are my 12 most-used git commands that I come across in my daily programming. 
If you want to learn all git commands, then learn by doing and do a lot more practice and understand their working. If you have any doubt or suggestions related to these blogs then feel free to contact with me...
Enter fullscreen mode Exit fullscreen mode

linkedlin
Github

Thank You

Top comments (0)