{{announcement.body}}
{{announcement.title}}

8 Basic Git Commands Every Newbie Developer Must Know

DZone 's Guide to

8 Basic Git Commands Every Newbie Developer Must Know

In this article, you are going to learn the eight most important basic Git commands.

· Open Source Zone ·
Free Resource

Learn more about these essential Git commands.

Git is one of the most important parts of the developer’s day-to-day work. So learning Git is a must for a newbie developer. In this article, you are going to learn the eight most important basic Git commands.

Below, I have listed down all the eight commands. Then, we will have a look at them one by one.

  1. git init
  2. git clone
  3. git add
  4. git commit
  5. git status
  6. git branch
  7. git pull
  8. git push
You may also like: Top 20 Git Commands With Examples

1. git init

git init command initializes brand new Git repository (locally) and begins tracking the existing directory.

When you hit the git init command, git adds a sub-folder within an existing directory that manages all required files for version control.

GitHub Flavored Markdown




xxxxxxxxxx
1


 
1
HP@Gaurav MINGW64 /e/example
2
$ git init
3
Initialized empty Git repository in E:/example/.git/
4
 
          
5
HP@Gaurav MINGW64 /e/example (master)
6
$



You are hitting git init means you want to initialize the current directory as Git repository.

The following GIF show initializing a new repository and a hidden sub-folder containing all data structure required for version control.

Initializing a git repository locally

Initializing a git repository local.


2. git clone

git clone creates a local copy of a repository that already exists remotely. The local copy is the exact copy of the remote repository, it contains the same files, history, and branches.

GitHub Flavored Markdown




xxxxxxxxxx
1


 
1
$ git clone <remote-repository-link>


GitHub Flavored Markdown




xxxxxxxxxx
1
11


 
1
HP@Gaurav MINGW64 /e/directory
2
$ git clone https://github.com/gauravkukade/example.git
3
Cloning into 'example'...
4
remote: Enumerating objects: 16, done.
5
remote: Counting objects: 100% (16/16), done.
6
remote: Compressing objects: 100% (10/10), done.
7
remote: Total 16 (delta 3), reused 7 (delta 2), pack-reused 0
8
Unpacking objects: 100% (16/16), done.
9
 
          
10
HP@Gaurav MINGW64 /e/directory
11
$



You can clone any public repository from the platforms like GitHub, BitBucket, GitLab, and other Git hosting platforms.

The following GIF shows the git clone command

Cloning the remote repository

Cloning the remote repository


3. git add

git add stages a change.

If you are done with changes in your code then its necessary to stage that changes and take a snapshot of it to include it in the repository’s history.

git add does the first step, it stages a change.

GitHub Flavored Markdown




x


 
1
$ git add <path-of-the-file-you-made-changes-in>



If you made changes in the multiple files, and you want to stage all files in the same command, then add the file path of all files separated with a single space.

GitHub Flavored Markdown




xxxxxxxxxx
1


1
$ git add <first-filepath-you-want-to-stage> <second-filepath-you-want-to-stage> <nth-filepath-you-want-to-stage>
2
 
          


If you want to stage all the files, then write ‘.’ (dot) after git add

GitHub Flavored Markdown




xxxxxxxxxx
1


 
1
$ git add .



Any staged changes will become the part of the next snapshot and a part of the repository’s history.

You can stage and take a snapshot of the current changes in a single command also but is not recommended.

Staging your changes first and then taking snapshot gives you complete control over the repository’s history.

The following GIF shows the git add command:

Staging the changes

Staging the changes


4. git commit

git commit saves the snapshot to the repository’s history.

git add does the first step, i.e. staging the changes and git commit does the final step, i.e. it saves the snapshot to the repository’s history. In Git, these two steps complete the changes-tracking process.

GitHub Flavored Markdown




xxxxxxxxxx
1


1
$ git commit -m "<meaningful-git-commit-message>



You can write a meaningful message to the commit. It is recommended to write a commit message in the ‘Simple Present Tense’.

If you are committing a new feature to your project, then your commit message should be "Add <feature-name> feature".

The following GIF shows the git commit command:

Committing the staged changes

Committing the staged changes


It is the simple way to write the commit message, but there is a more in-depth way to write a commit message with title and description. We will see that in a separate blogpost.

5. git status

git status shows the status of the changes as untracked, modified, or staged.

GitHub Flavored Markdown




xxxxxxxxxx







The following GIF shows the git status command:

Showing status of the current change process

Showing status of the current change process


6. git branch

git branch list the existing branches from the local repository. The current branch will be highlighted in green and marked with an asterisk.

GitHub Flavored Markdown




xxxxxxxxxx
1


1
HP@Gaurav MINGW64 /e/directory/example (master)
2
$ git branch
3
* master
4
  new_branch
5
 
          
6
HP@Gaurav MINGW64 /e/directory/example (master)
7
$


The following GIF shows the git branch command:

Showing the list of locally present branches

Showing the list of locally present branches


7. git pull

git pull update the local repository with updates from its remote counterpart, i.e. remote repository.

GitHub Flavored Markdown
 




xxxxxxxxxx
1
17


1
HP@Gaurav MINGW64 /e/directory/example (master)
2
$ git pull
3
remote: Enumerating objects: 4, done.
4
remote: Counting objects: 100% (4/4), done.
5
remote: Compressing objects: 100% (3/3), done.
6
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
7
Unpacking objects: 100% (3/3), done.
8
From https://github.com/gauravkukade/example
9
   15e8755..d7aefb1  master     -> origin/master
10
Updating 15e8755..d7aefb1
11
Fast-forward
12
 new-file-added-to-remote-repository.txt | 1 +
13
 1 file changed, 1 insertion(+)
14
 create mode 100644 new-file-added-to-remote-repository.txt
15
 
          
16
HP@Gaurav MINGW64 /e/directory/example (master)
17
$



If your teammate made commits to the remote branch, and you want to reflect these changes in your local environment, then you will hit the command git pull.

This command will check if there are any updates on the remote branch as compared to your local environment, if yes, then it will update your local environment with these changes. If no, then it will do nothing.

The following GIF shows the git pull command:

Pulling the changes committed by others on remote repository

Pulling the changes committed by others on remote repository

8. git push

git push updates the remote repository with any commits made locally to a branch

GitHub Flavored Markdown




xxxxxxxxxx
1


1
$ git push origin <branch-name-you-have made commits on>



If the branch does not exist on a remote repository, then the whole branch with its commits will be pushed to the remote repository.

GitHub Flavored Markdown




xxxxxxxxxx
1
16


 
1
HP@Gaurav MINGW64 /e/directory/example (master)
2
$ git push origin master
3
fatal: HttpRequestException encountered.
4
   An error occurred while sending the request.
5
Username for 'https://github.com': gauravkukade
6
Counting objects: 2, done.
7
Delta compression using up to 4 threads.
8
Compressing objects: 100% (2/2), done.
9
Writing objects: 100% (2/2), 255 bytes | 255.00 KiB/s, done.
10
Total 2 (delta 1), reused 0 (delta 0)
11
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
12
To https://github.com/gauravkukade/example.git
13
   d7aefb1..dc3d3ef  master -> master
14
 
          
15
HP@Gaurav MINGW64 /e/directory/example (master)
16
$



The following GIF shows the git push command:

Pushing locally committed changes to the remote repository

Pushing locally committed changes to the remote repository

Since you've made it to the end of this post, here is some bonus content!

How to Create a New Branch Locally

You can create a new branch locally using the following command:

GitHub Flavored Markdown




xxxxxxxxxx






Creating a new branch locally

Creating a new branch locally

This article originally published at https://coderolls.com/basic-git-commads/

Further Reading

Top 20 Git Commands With Examples

Useful Git Commands

Commands and Operations in Git

Topics:
beginner, commands, git, git commands, github, newbie developer, open source, shell

Published at DZone with permission of Gaurav Kukade . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}