Branches and Git Internals(Part 3)

Branches and Git Internals(Part 3)

Table of contents

No heading

No headings in the article.

As of now we have read about basics of git, github, architecture of git and even some hands on with git commands.
But now, let's discuss about git branches, snapshots and git internals.


Commit object

As we have studied earlier about committing changes, let's know what is commit object.
A commit object contains :
  • reference to commit objects and tree objects.
  • reference to blob objects.
  • commit message.
  • also stores pointer to its parent(s) commit(s).
  • About parent commits

    0 parent = initial commit
    1 parent = usual commit
    multiple parents = commit resulting from merging 2 or more branches.

    What happens when we stage a file?

    Let's see what happens at the staging time:

  • computes the checksum for each file.
  • stores the version of a file in a git repository(blob).
  • Adds checksum into the staging area.
  • An illustration to easily understand blobs, trees and commit combined.

    What happens when we commit a file?

    When you make a commit, git stores a commit object that contains a pointer to the snapshot of the content you staged.
    This statement in itself is very much overwhelming, but let's discuss it with a diagram.
    Following happens upon committing a file:
  • checksums each sub directory.
  • stores it as a tree object in GIT repository.
  • Screenshot from 2022-10-12 00-51-39.png Now we have commit, tree and something called blob.

    commit

    The files which are present in the staging area and needs to be committed.
    It has some size , some SHA-1 hash ID of tree , author and committer , and is the first commit of the project.Pointing towards something called tree.
    Screenshot from 2022-10-12 00-59-34.png

    tree

    A Git tree object creates the hierarchy between files in a Git repository. You can use the Git tree object to create the relationship between directories and the files they contain. These endpoints allow you to read and write tree objects to your Git database on GitHub. We can again see size and some blobs and their SHA-1 hash ID and their names pointing towards multiple blobs.

    Screenshot from 2022-10-12 01-02-31.png

    blob

    A Git blob(binary large object) is the object type used to store the contents of each file in a repository. The file's SHA-1 hash is computed and stored in the blob object.

    Screenshot from 2022-10-12 01-08-39.png There is some size, and the details about the files.


    Branches

    Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug(no matter hpw big or small) you spawn a new branch to encapsulate your changes. Branches are cheap and easy to merge. Switching back and forth the branches is also very easy.
    $ git branch testing : This lets us create a branch named testing, but the head is still pointing towards master branch (head---->master).

    Screenshot from 2022-10-12 01-28-03.png

    But now we want to change the head from master to the testing branch.
    $ git checkout testing : Let's us change head from master----->testing.

    Screenshot from 2022-10-12 01-29-49.png Now as the head is on testing branch, let's do some changes. Diagrammatically it's shown below:

    Screenshot from 2022-10-12 01-40-33.png

    Again if we shift the head from testing----->master using the command
    $ git checkout master we'll see , the master would still be at the commit where it was earlier , but now with head at the master branch we'll commit some changes which diagrammatically is shown below:

    Screenshot from 2022-10-12 01-41-04.png Here the diagram explains us everything that is happening (common commit, snapshots to merge and so on).


    That's pretty much it about the branches, git internals, tree, blobs, commits and head. With this we have gained a lot of knowledge about git and github. Hope you like this blog by me. Do consider liking it and sharing it with your friends.
    Connect with me: Twitter
    Thank You.