Making sense of Git and GitHub - II [ Staging and Committing, Commit History, Difference]

This is in continuation of this post(Making sense of Git and GitHub - I). Please read before proceeding.


We will create a new file Index.css in the working directory.

What would be the output if we give git status now?

Does the new file added tracked by the local repo(git)? No!. Therefore it is an untracked file.


On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Index.css

nothing added to commit but untracked files present (use "git add" to track)

We will add an Index.css to the staging area/index.

Giving git add Index.css,


Giving git status now,

Is that file committed to the local repo? No!. Therefore it is under Changes to be committed.

Is that file already existing file? No!. Therefore it is the new file.

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Index.css

We will commit to the local repo.


The corresponding git status will be,

[master 6b558f4] Index.css is the new file, and I want to commit it in the local repo(git)
 1 file changed, 8 insertions(+)
 create mode 100644 Index.css

We have done many commits and add, let's see our commits history.

Type git log in the terminal,

The result would be,


commit 6b558f457206fceff293f29b141ab2a137a2e544 (HEAD -> master)
Author: <your git username>
Date:   <Time stamp>

    Index.css is the new fil,e and I want to commit it in the local repo(git)

commit e3935b49f94745a9944809a14da60217f403f912
Author: <your git username>
Date:   <Time stamp>

    Index.html is changed, and I want to commit it in the local repo(git)

commit 069667a685050a170b829964d0391eca08990283
Author: <your git username>
Date:   <Time stamp>

    Index.html is committed

If we need to know what happened in every commit,

Type git log --stat,

commit 6b558f457206fceff293f29b141ab2a137a2e544 (HEAD -> master)
Author: <your git username>
Date:   <Time stamp>

    Index.css is the new file and I want to commit it in the local repo(git)
 Index.css | 8 ++++++++
 1 file changed, 8 insertions(+)

commit e3935b49f94745a9944809a14da60217f403f912
Author: <your git username>
Date:   <Time stamp>

    Index.html is changed, and I want to commit it in the local repo(git)

 Index.html | 3 +++
 1 file changed, 3 insertions(+)

commit 069667a685050a170b829964d0391eca08990283
Author: <your git username>
Date:   <Time stamp>

    Index.html is committed

 Index.html | 5 +++++
 1 file changed, 5 insertions(+)

Output with one-liners,

Type git log --oneline,

6b558f4 (HEAD -> master) Index.css is the new file, and I want to commit it in the local repo(git)

e3935b4 Index.html is changed, and I want to commit it in the local repo(git)

069667a Index.html is committed
Listing files:

For simplicity's sake, we will create a new repository and add two files to the staging area. I hope you can do it on your own by this time. Ensure you have git bash(automatically installed when you install git on your machine). Git bash is a command-line interface, and basically, we can use Linux commands here.

We will create a new file in the working directory,

Just type ls in the git bash terminal,

'ls' command will give you all the files in the working directory.




If you want all the files in the staging area, you need to prefix git because staging is in git. The command is git ls-files,


We have only File1 and File2 in the staging area.


Okay, let's add File3.txt to the staging area,

Now,



Now ls and git ls-files remain the same.





Finding differences:

We know three levels,

Working directory ----> Staged/Indexed/Cached ---> Local Repo

There will be files in all levels once you do the first commit.

Then you modify it in the working directory and add it to the staged area.

Then commit the modified file to the local repo.

Now you may be working on some changes in the working directory.

Naturally, there will be some differences between levels, be it in the file content, directory, new files being added, etc.


How do you find the difference between various levels?

To illustrate, we will just have only one file File.txt.

Finding the difference between the Staging and Working directory:

Remember the command git diff, and that command is the key.

Do you find any difference between the contents of File.txt in the staging and the working directory? No!.

So git diff File.txt won't give you anything.





Let us change the content in the working directory,

Do you find the difference? Yes! "Banana" is added in the second line.

So git diff File.txt will give you the result,

Leave the warning.

diff --git a/File.txt b/File.txt

diff --git --> This says git diff is the command we used.

a/File.txt --> Source --> Older version of file --> Staging area copy.

b/File.txt --> Destination --> Newer version of file --> Changed --> Working directory copy.


index 05ceae9..e1b73ac 100644 --> Metadata, and it is a git internal way of storing data(hashes and mode). Let's leave for now.


--- a/File.txt --> Something is missing in the source copy(Staging).

+++ b/File.txt --> Something is added to the destination copy(Working directory).


@@ -1 +1,2 @@ --> We will leave now because reading this will be a different topic by itself. This will have metadata about changes in the source and destination files.


Then,

Apple --> [Blank space] Apple --> Blankspace signifies unchanged content.

+Banana --> +Banana --> [+] Signifies content s added in destination copy(Working directory).

and so [-minus] represents missing in the destination copy.


Between the Working directory and the Last commit:

Local repo consists of a series of commits with the HEAD pointing to the latest commit.

But wait, if we commit now, will "Banana" move inside the commit? No, because the commit happens from the staging area, and the staging area copy doesn't contain "Banana" in it.

Let's add Cat to the File.txt in the working directory,


What is command now? Same git diff with different options.

So git diff HEAD File.txt will give you the,

What is the source here? Older version = Local repo = Last commit = HEAD.

What is the destination here? Newer version = Working directory.

So compared to the older copy, we added Banana and Cat in the newer copy.


Between the Staging area and the Last commit:


What is the command? Same git diff with the option HEAD and --staged.

The result of git diff --staged HEAD File.txt would be,


Between the Working directory and Specific commit:

HEAD always points to the latest commit. Then, how do we find the specific commit? Simple by "Commit ID".

Note: HEAD always points to the last commit.

We will compare working directory with first commit with id 8ebacb3.

The command is git diff 8ebacb3 File.txt and the result is,

Similarly, for staged and specific commits,

The command is git diff --staged 8ebacb3 FiIe.txt.


Between commits, we need to give

git diff <commit-id> <commit-id> File.txt.