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 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.
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
# 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
- Follow all of the steps in the lesson
- Make a commit with the message add new content
- Upload the changes to GitHub