Register to get access to 15+ free programming courses with interactive exercises

Analyzing changes made Git fundamentals

During development, programmers often have to stop and analyze the changes they have made since the last commit. The need to check changes becomes evident when you imagine what it's like to work on a real project. As a rule, it can be thousands (or even tens and hundreds of thousands) of lines of code, hundreds and thousands of files, and sometimes several days of work. You just need to have spent a few hours on a project like this before it becomes very hard to remember what was changed and where, and what still needs to be changed.

Analyzing changes is important even in small projects. Right now, while developing this course, several files have changed and git status looks like this:

git status

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   300-working-directory/README.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   100-intro/README.md
    modified:   250-github/README.md
    modified:   300-working-directory/README.md
    modified:   300-working-directory/spec.yml
    modified:   350-changes/README.md

Let's try to reproduce a similar situation in our project. Run the following code in the hexlet-git repository:

echo 'new line' >> INFO.md
echo 'Hello, Hexlet! How are you?' > README.md

git status

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   INFO.md
    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

Both files have changed. In one we added a line, and in the other we replaced a line. How do I check these changes now? To do this, git has a git diff command that shows the difference between the initial and modified file:

git diff output

git diff

diff --git a/INFO.md b/INFO.md
index d5225f8..40f51f1 100644
--- a/INFO.md
+++ b/INFO.md
@@ -1 +1,2 @@

 git is awesome!
+new line

diff --git a/README.md b/README.md
index ffe7ece..00fd294 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@

-Hello, Hexlet!
+Hello, Hexlet! How are you?

The command output may be confusing at first. There's quite a lot of service data, followed by changes to the lines of code. The git diff outputs the lines that have changed (and sometimes the lines around the changed ones for easy analysis), not the entire files. There'll be a "-" if a row was deleted, and "+" if a row was added.

The command itself not only displays the difference between the files, but also launches a pager, a special program that allows you to navigate through the output and search for the data you want within it. Press f to scroll down through the diff, and b or u to scroll up. To exit viewing mode, press q.

By default, git diff only shows changes to modified files that have not yet been added to the index. Thus, the files added to the index don't need to be looked at, because we've already prepared them for the commit. In reality, however, you often want and, in fact, need to see these changes. To do this, run the diff output command with the --staged flag:

# Outputs all changes made in the working directory
# that have been added to the index
git diff --staged

git diff is a command that must be run before each commit. It allows you to analyze the changes you've made and fix possible errors. Sometimes programmers mistakenly add things to a commit that shouldn't go there.


Do it yourself

  1. Follow all of the steps in the lesson
  2. Make a commit with the message add new content
  3. Upload the changes to GitHub

Аватары экспертов Хекслета

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From zero to a developer. Refunds in case you won't get a job

Frontend Developer icon
Profession
New
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.