Basic and Important Git Commands Used by the Developers

Basic and Important Git Commands Used by the Developers

Hello everyone,

This is my first post on my hashnode blog!

If you are working in a software industry specifically in a software development team then you must know about Git.

Git is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to.

In this post, we will talk about the basic and important Git commands used by the developers.

Basic and Important Git Commands Used by the Developers

1. git init

The git init command creates the Git repository. This is the first command to start a new project in a GitHub repository.

Go to the directory that contains your project files ( or the empty directory in which you will start the development of your project ) and run the git init command. A hidden .git subdirectory will be added to it.

So the main purpose of the git init command is - to initialize a git repository for a new or existing project.

After running this command you will be able to run other git commands to add or commit the changes, view logs, etc.

2. git config

The git config command is used to set up the configurations for your GitHub repository/projects.

The few important configurations you are required to do:

  • Set Username: git config --global user.name = "Vanesh Mali"
  • Set Email: git config --global user.email= "vaneshmali@gmail.com"

If you want to check your configuration settings, you can use the git config --list command to list all the settings Git can find at that point:

git config --list

It will give you output something like this -

  • user.name=Vanesh Mali
  • user.email=vaneshmali@gmail.com

3. git checkout

The git checkout branch is used to switch to a different branch or create a new branch and switch to it.

If you want to switch to your new branch called feature_branch_1 then you can execute the following git command.

git checkout feature_branch_1

If you want to create a new branch feature_branch_2 and switch to it then you need to execute the following git command.

git checkout -b feature_branch_1

4. git branch

The git branch command is used to list your branches.

Using this command with different options you will be able to delete/rename the branches.

If you want to list the branches then you can execute the following git command

git branch or git branch --list

If you want to delete the branch then execute the following git command.

git branch -d feature_branch_1 The branch must be fully merged in its remote branch then and then you will be able to delete the branch with this option.

If you want to forcefully delete the branch then you can execute the following command.

git branch -D feature_branch_1

If you want to rename the branch from feature_branch_1 to feature_branch_2 then you can execute the following command.

git branch -m feature_branch_1 feature_branch_2

If you want to rename your current branch then you can execute the following command.

git branch -m feature_branch_2

5. git status

The git status command used to display what are the changes you have done. It lists the changed files, staged files, newly added files, etc.

Before executing git add or git commit or git push I always check my changes using the command git status.

6. git add

The git add command is used to make your changes ready to commit. If you have made any changes to the files or if you have added/removed the files then you are required to execute the following command.

  • To add a single file: git add file_name.txt
  • To add multiple files: git add file1.txt file2.txt
  • To add all the changes files: git add .

7. git diff

The git diff command is used to see the differences in the changed files in your repository. As a developer, I always use the git diff command before committing my changes to check the differences in changed files.

The following command will help you to check what changes you have done in the file file_name_1.txt.

git diff file_name_1.txt

The git diff command can also be used to create the patch file as -

git diff > my_patch_file.patch This command will create the patch file of all your local changes which are not committed yet.

If you want to apply the patch file you can execute the following command.

git apply my_patch_file.patch

8. git commit

Once you have made changes and added them using the git add command then you can commit your changes.

The git commit command is similar to saving your changes. It will commit a file or files to the local repository.

It is recommended to use commit message while committing your changes as -

git commit -m "fix for the 404 error in product details module"

9. git push

The most important git command is git push. This command is used to push the changes to the remote branch that you just have committed using git commit command.

After committing your changes using git commit command, the next thing you want to do is send your changes to the remote server. To push your changes to the remote branch you need to execute the following command

git push -u origin feature_branch_1

10. git pull

This is the most used git command by developers. The git pull command is used to get updates to your local branch from the remote branch.

You can pull your feature_branch_1 remote branch changes using the following command.

git pull origin feature_branch_1

So, in this way we learned or understood the basic and important git commands used by the developers.

You may want to follow me on Twitter @vaneshmali