Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
git status
2.1.
Result for No-History
2.2.
Result for Modified Files
2.3.
Untracked 
3.
git log
4.
git commit
5.
git diff
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Git Activity Command

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Linux developers created git as an open-source version control system in 2005. You can clone the project and work on it on your machine using git, a distributed version control system. Using git, every developer has their local copy of their code repository history. It makes the initial clone of the repository slower, but subsequent operations such as commit, diffs, merges, and logs become significantly faster. The data is kept as snapshots in git. Every time you make a change to a file in git, it goes through three steps:

  • Staged
  • Committed
  • Modified

So in this article, we will focus on some Git Activity Commands that help us track our file's condition, whether the file is in a staging area, committed area, or modified area.

Learn more, wget command in linux

git status

The git status command will tell you what changes you have made, what is in your staging area, and which of your files git is tracking. Git also tells you where your files are at this moment. This allows you to know exactly where your documents or changes are at this time.

The following are the results for git status command.

Result for No-History

You can use git status command directly like below, which has no historical details of the files. 

Result for Modified Files

If we make some changes to the tutorial file and again run git status, it will display that the File is not stagged, and we have to use git add to save the changes.

Untracked 

After creating the File, you must use the git add to send your File into the staging area. If you don't use this, the git status will repeatedly return to the git add command.

Now, after using the git add command, the error resolves.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

git log

You can examine the history of your code with git since it tracks every commit over time. To navigate your local repository, you will need to use CLI tools, such as git log, to view the Git commit history.

You can use patch (-p) and the number of logs to get more information.

git commit

Whenever you make changes to your project, they are committed. Therefore, a commit is generated. Committing is the equivalent of putting an end to a project's history. Commits are identified by their identifiers so that you can refer to them later. In addition, every commit except the first one, known as the root commit, has multiple parents. It is from this relationship that the repository's revision history is built.

Use git commit whenever you modify your File so that it will create a history for each commit.

An associated commit message must accompany every git commit. This message informs the reader why a change was made.

Below, you can see that your files have been committed.

Now, if you check the status of your File, it will show that there is nothing to commit.

git diff

You often use git diff and git status to analyze your file changes. You can check the status of your files using the git diff command. git diff shows you the exact differences in your files, the working directory changes, and the staging changes. Diff is the very definition of difference.

Consider we have added some content to the File. 

And then send the File into the staging area using git add . Then check the git status.

Again if we remove the word "File" from the File and again check the git status, then we can see below that modified is written in two colors green means it is in the staging area, and red means it is in work right now. So to know the difference between them, we can use the git diff command to know which File is in the working area and which is in the staging area.

 

Check out most important Git Interview Questions here.

Frequently Asked Questions

  1. What is the difference between a git diff and a git status?
    It's important to note that git diff is specifically made for comparisons, and it's very powerful at it: it can compare commits, branches, a single file across revisions or branches, etc. GitHub status, however, reports on the status of the working tree.
     
  2. Write different methods to check git logs?
    $ git log
    $ git log -p -2. -To display recent 2 commits
    $ git log –stat. -To display each commit log summary
    $ git log --pretty=oneline -To display each commit log in one line format
    $ git log --pretty=format:"%h - %an, %ar : %s" -To display customized output of Git log
     
  3. Write the best way to commit to a message?
    Using the "-m" option and your commit message is the easiest way to create a Git commit with a message. If you are using the Git CLI, keep in mind that your commit message should be limited to prevent it from being wrapped.
     
  4. Is it necessary to git add every time?
    git commit -am "message" will add everything that was modified to the commit without you having to use git add every time.

Conclusion

In this blog, we have seen git Activity Commands and the implementation for each command. You can read the following git blogs to learn more about git.

Github Guide

Git First commands

Git Change commands 

We hope that this blog has helped you enhance your knowledge about git commands and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Git First Commands
Next article
Git Change Commands
Live masterclass