Basic Commands of Git

Git is a version control system that helps you keep track of code changes and collaborate on code with others. In this blog post, we will introduce some of the basic commands of Git and show you how to use them with examples.

git init

The git init command creates a new Git repository in the current directory. It initializes the .git folder that contains all the information and history of your project.

To use this command, you need to have an empty directory or a directory that is not already under version control.

For example, if you want to start a new project called my-project, you can create a directory with that name and run git init inside it:

Bash
mkdir my-project
cd my-project
git init
  • mkdir my-project creates a new directory called my-project in your current location. A directory is a folder that can contain files and other directories.
  • cd my-project changes your current directory to my-project. This means that you are now inside the my-project folder and can access its contents.
  • git init initializes a new Git repository in the my-project directory. This means that Git will start tracking the changes in your files and folders inside the my-project directory.

This will output something like:

Initialized empty Git repository in /home/user/my-project/.git/

Now you have a new Git repository that is ready to track your files.

git clone

The git clone command copies an existing Git repository from a remote source, such as GitHub, to your local machine.

It creates a new directory with the same name as the repository and downloads all the files and history from the remote source.

To use this command, you need to have the URL of the repository that you want to clone.

For example, if you want to clone the repository of this blog post, you can run:

Bash
git clone https://github.com/user/git-blog-post.git

This will output something like:

Cloning into 'git-blog-post'…
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 15 (delta 2), reused 15 (delta 2), pack-reused 0
Receiving objects: 100% (15/15), done.
Resolving deltas: 100% (2/2), done.

Now you have a copy of the repository on your local machine that you can modify and update.

git config

The git config command lets you configure various settings and preferences for your Git environment. You can use this command to set your name, email, editor, aliases, and more.

There are three levels of configuration: system, global, and local. The system level applies to all users on the machine, the global level applies to the current user, and the local level applies to the current repository.

To use this command, you need to specify the level with the --system, --global, or --local option, followed by the name and value of the setting.

For example, if you want to set your name and email for the global level, you can run:

Bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

You can check the current configuration settings with the --list option. For example, to see the global settings, you can run:

Bash
git config --global --list

This will output something like:

user.name=Your Name
user.email=your.email@example.com

You can also use the --show-origin option to see where each setting comes from. For example, to see the origin of the user.name setting, you can run:

Bash
git config --show-origin user.name

This will output something like:

file:/home/user/.gitconfig user.name=Your Name

git add

The git add command adds the changes in your files to the staging area, which is a temporary area that holds the changes before they are committed.

The staging area allows you to review and organize your changes before saving them to the repository.

To use this command, you need to specify the files or directories that you want to add.

For example, if you want to add a new file called index.html to the staging area, you can run:

Bash
git add index.html

You can also use the . (dot) to add all the changes in the current directory, or the -A option to add all the changes in the entire repository.

For example, to add all the changes in the repository to the staging area, you can run:

Bash
git add -A

You can check the status of the staging area with the git status command, which we will learn next.

git commit

The git commit command saves the changes in the staging area to the repository with a message that describes what you have done.

The commit message is important for documenting the history of your project and communicating with other developers.

To use this command, you need to use the -m option followed by the message in quotes.

For example, if you want to commit the changes in the staging area with the message “Add index.html file”, you can run:

Bash
git commit -m "Add index.html file"

This will output something like:

[master (root-commit) 3a4b5c6] Add index.html file
1 file changed, 10 insertions(+)
create mode 100644 index.html

You can also use the -a option to skip the staging area and commit all the changes in the tracked files.

For example, if you want to commit all the changes in the tracked files with the message “Update index.html file”, you can run:

Bash
git commit -a -m "Update index.html file"

This will output something like:

[master 6b7c8d9] Update index.html file
1 file changed, 2 insertions(+), 2 deletions(-)

git status

The git status command shows the current state of your working directory and staging area. It tells you which files are modified, staged, untracked, or ignored by Git.

It also gives you hints on how to proceed with your changes. To use this command, you do not need to specify any arguments.

For example, if you run git status in a repository with some changes, you will see something like:

Bash
git status

This output something like:

On branch master
Changes not staged for commit:
(use "git add …" to update what will be committed)
(use "git restore …" to discard changes in working directory)
modified: index.html Untracked files:
(use "git add …" to include in what will be committed)
style.css no changes added to commit (use "git add" and/or "git commit -a")

This output tells you that you are on the master branch, that you have modified the index.html file but not staged it, that you have a new file called style.css that is not tracked by Git, and that you have no changes ready to commit.

git log

The git log command shows the history of your commits in the repository. It displays the commit ID, author, date, and message for each commit.

To use this command, you do not need to specify any arguments.

Bash
git log

For example, if you run git log in a repository with some commits, you will see something like:

commit 6b7c8d9f4c8f4a9f8f8f8f8f8f8f8f8f8f8f8f8f
Author: Your Name your.email@example.com
Date: Tue Nov 28 10:17:40 2023 +0530
Update index.html file
commit 3a4b5c6e7d8f9a0b1c2d3e4f5g6h7i8j9k0l1m2n
Author: Your Name your.email@example.com
Date: Tue Nov 28 10:15:20 2023 +0530
Add index.html file

This output tells you that you have two commits in the repository, the most recent one with the ID 6b7c8d9 and the message “Update index.html file”, and the previous one with the ID 3a4b5c6 and the message “Add index.html file”.

You can also use various options to customize the output of the git log command.

For example, you can use the --oneline option to show each commit on a single line, the --graph option to show a graphical representation of the commit history, or the --reverse option to show the commits in reverse order.

Bash
git log --oneline
git log --graph
git log --reverse

git diff

The git diff command shows the differences between files in your working directory, staging area, and repository.

It tells you what changes you have made, added, or deleted in your files. To use this command, you can specify the files or directories that you want to compare, or use various options to compare different versions of your files.

For example, if you run git diff without any arguments, you will see the differences between your working directory and the staging area.

Bash
git diff

Let me show you an example of how to use the git diff command. Suppose you have a file called hello.txt in your repository that contains the following text:

Hello, world!
This is a sample file.

Now, you make some changes to the file in your working directory, such as adding a new line and deleting a word. The file now looks like this:

Hello, world!
This is a sample file for git diff.
How are you?

Bash
git diff hello.txt

If you run git diff hello.txt, you will see the output like this:

diff --git a/hello.txt b/hello.txt
index 557db03..980a0d5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,2 +1,3 @@
Hello, world!
-This is a sample file.
+This is a sample file for git diff.
+How are you?

This output tells you how the file has changed from the staging area to the working directory. The lines that start with - are the ones that have been deleted, and the lines that start with + are the ones that have been added.

The lines that have not changed are shown without any sign. You can also see the index of the file, which is a hash that identifies the file version, and the mode, which is the file permission.

Now, If you run git diff --staged, you will see the differences between the staging area and the repository.

Bash
git diff --staged hello.txt

If you want to see the differences between the staging area and the repository, you need to add the file to the staging area first with git add hello.txt. Then, you can run git diff --staged hello.txt and see the output like this:

diff --git a/hello.txt b/hello.txt
index e69de29..980a0d5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -0,0 +1,3 @@
+Hello, world!
+This is a sample file for git diff.
+How are you?

This output tells you how the file has changed from the repository to the staging area. Since the file did not exist in the repository before, all the lines are shown as added.

If you run git diff HEAD, you will see the differences between your working directory and the repository.

Bash
git diff HEAD hello.txt

If you want to see the differences between the working directory and the repository, you can run git diff HEAD hello.txt and see the output like this:

diff --git a/hello.txt b/hello.txt
index e69de29..980a0d5 100644
--- a/hello.txt
+++ b/hello.txt
@@ -0,0 +1,3 @@
+Hello, world!
+This is a sample file for git diff.
+How are you?

This output is the same as the previous one, because the staging area and the working directory have the same version of the file.

However, if you make some more changes to the file in the working directory, such as changing a word, the output will be different. For example, if you change the file to this:

Hello, world!
This is a sample file for git diff.
How do you do?

And then run git diff HEAD hello.txt, you will see the output like this:

diff --git a/hello.txt b/hello.txt
index e69de29..f0248d4 100644
--- a/hello.txt
+++ b/hello.txt
@@ -0,0 +1,3 @@
+Hello, world!
+This is a sample file for git diff.
+How do you do?

This output tells you how the file has changed from the repository to the working directory. You can see that the last line has been modified from “How are you?” to “How do you do?”.

Conclusion

We have seen how to create a new Git repository with git init, how to copy an existing Git repository with git clone, how to configure various settings and preferences for your Git environment with git config, how to add changes to the staging area with git add, how to save changes to the repository with git commit, how to check the status of your working directory and staging area with git status, how to view the history of your commits with git log, and how to compare different versions of your files with git diff.

These commands are essential for working with Git and managing your projects. They allow you to track, review, and organize your changes, as well as collaborate with other developers. By mastering these commands, you can take advantage of the powerful features and benefits of Git.

We hope you have enjoyed this blog post and learned something new. If you have any questions or feedback, please leave a comment below. Thank you for reading! 😊

Bharath Adigopula
Bharath Adigopulahttps://www.bharathwick.com
Deep interest in the world of technology, particularly in the areas of Cloud Computing, Internet, Gadgets, Security, Linux, Windows, and DevOps.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Follow Me

0FansLike
0FollowersFollow
28FollowersFollow

Latest Articles