16 Basic Git Commands with EXAMPLES

16 Basic Git Commands with EXAMPLES

As a beginner knowing all the git commands could be overwhelming, but you can get away with some basic git commands at the start.

In this article, we will go through these 16 basic git commands that you should know as a new developer.

  1. git clone
  2. git init
  3. git checkout
  4. git branch
  5. git config
  6. git status
  7. git diff
  8. git add
  9. git commit
  10. git push
  11. git remote
  12. git pull
  13. git log
  14. git rm
  15. git stash
  16. git fetch

Now let's go through them in little detail...

1. Git clone

Git clone git command is used when you are trying to download a project from a remote repository.

Syntax:

$ git clone <repository>

Example:

$ git clone https://github.com/git/git.git

 

2. Git init

Git init command is used to convert a directory into a git repository. This command is useful when you are starting a new project or want to initialize git in an existing project.

Syntax:

$ git init

 

3. Git checkout

Git checkout command is used to switch your current branch to another one.

Syntax:

$ git checkout <branch_name>

With -b option Git checkout branch can also create a new branch from the current branch.

Syntax:

$ git checkout -b <new_branch>

 

4. Git branch

Git branch command is used to list all the branches on your local repository. And the output will highlight the current branch you are in.

Syntax:

$ git branch

Git branch command can also be used to create a new branch by adding the name of the new branch at end of the command.

Syntax:

$ git branch <new_branch>

 

5. Git config

Git config command is used to set the git user config for the project or globally. At the start of your project make sure you set the correct user.name and user.email for your project.

Syntax:

$ git config <setting> <command>

Example:

git config user.email [email protected]

git config user.name “John Corner”

Git config command can also be used to set this config globally. You can find more information about the git config command here.

 

6. Git status

Git status command returns the current status of your working directory. This is the best way to know what is modified, added or deleted in your repository.

Syntax:

$ git status

 

7. Git diff

Git diff command can be used in many different ways and initially, you can use it to check what is modified in a file or directory.

$ git diff <file_name or directory_name>

 

8. Git add

Git add command is used to move new or modified files to the staging area. After this changes can be committed.

Syntax:

$ git add <file_name or directory_name>

If you want to add all files then use the git add command with “.”.

Syntax:

$ git add .

 

9. Git commit

Once you are done with your changes, the git commit command is used to save your change in the git branch.

Syntax:

$ git commit -m “Your commit message.”

 

10. Git push

To more your local committed changes to remote use git push command. With this command, you push your local branch to the remote repository.

Syntax:

$ git push <remote> <branch>

 

11. Git remote

Git remote command with different options helps to sync the local repository with the remote repository. You can view, create/add, delete, rename remote to other repositories.

Git remote command is used to show the remote repository name and URL to which your local repository is connected.

Syntax:

# View remote
$ git remote -v

# Add remote
$ git remote add <remote_name> <remote_path>

# Delete remote
$ git remote rm <remote_name>

# Rename remote
$ git remote rename <old_remote_name> <new_remote_name>

 

12. Git pull

As the name suggests, the git pull command will fetch all the changes from the remote repository and merge them into your local repository.

$ git pull

 

13. Git log

Git log command is used to show commit history in the current branch on your local repository. Commit history will be in chronological order with unique commit hash, commit author and date.

$ git log

You can use the git log command with other options to filter the logs.

 

14. Git rm

Git rm command is used to remove files or directories from the git index. Different options can be used with the git rm command to get the desired result.

And keep in mind the git rm command will also remove files from the working directory.

Syntax:

$ git rm <file_name>

 

15. Git stash

Git stash command is used to save current changes of modified files and get a clean branch.

You will need git stash when you want to move to a different branch but can't because you have modified changes in your current branch.

$ git stash

You can always come back to your original branch and do the "git stash pop" command to get back your stashed changes.

 

16. Git fetch

Git fetch is used when you want to get updates from a remote repository.

It works the same as git pull doesn't merge the changes into your local repository.

Syntax:

$ git fetch <remote_name>

 

Conclusion

These are only the basic git command that every beginner should start with. All these git commands can be used in many different ways by adding arguments with them.

And you can always test these git commands and get familiar with them by creating a free git repository on Github or Gitlab.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.