Git Commit in Detail

Git Commit in Detail

Do you know why the Staging area exists? You will.

The Basic Git Workflow

Stage 1: Here you make new files, edit files, delete files, etc within the repository

Stage 2: The untracked and modified files are staged (made ready for commit)

Stage 3: All the files in Stage 2 are committed (a snapshot is stored permanently)

In short,

Untracked, Modified files \=====> Staging Area \=====> Tracked Files


Ok cool, But do we need a Staging area?

While working on a project, you might have many untracked/ modified files, ranging from 1 to 1000 (or even greater).

Committing all the files is not a good idea, as some sets of files have been modified for one purpose and other files for another purpose.

Example:

  • Suppose we have 3 files A, B and C.

  • A and C are modified for adding an image to a webpage

  • B is modified for adding a navigation bar on the webpage.

Now to commit A, B, and C at once will destroy the sole purpose of each set of modifications. This is where the Staging area comes into the picture 😎🔥.

Now you can stage A and C, then commit with the message "add image".

Stage B and commit with the message "add nav bar".

Thus, the staging area groups specific changes together in preparation for committing.


Some notes on Git Commit

  • Committing files is not the same as saving files. In the commit operation, we can group the changes of multiple files and then commit.

  • Every commit to a repository has a unique identifier called a hash.

  • Git can therefore tell what information needs to be saved where by comparing hashes rather than comparing entire files.

  • Each commit overrides the current file (older files can be retrieved by using hash)


Amending Commits

Suppose you just made a commit and then realized you forgot to include a file! Or, maybe you made a typo in the commit message that you want to correct.

Rather than making a brand new separate commit, you can "redo" the previous commit using the --amend option.

# ONLY WORKS FOR LATEST COMMIT
git commit -m "some commit"
git add forgotten-file
git commit --amend  #opens text editor for commit message

The Concept of Atomic Commits

When possible, a commit message should encompass a single feature, change or fix. In other words, try to keep each commit focused on a single thing.

This makes it much easier to undo or roll back changes later on. It also makes your code or project easier to review.

To learn more, you can refer: https://www.aleksandrhovhannisyan.com/blog/atomic-git-commits/


Present-Tense Imperative Commit Messages ??

From the Git docs:

Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "This patch] makes xyzzy do frotz" or ' changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behavior.

Examples:

  • "Fix some bugs"

  • "Add a footer to webpage"

  • "Change background color", etc


Ignoring Files

You can create a file called .gitignore in the root of a repository, which will not get committed. Inside the file, we can write patterns to tell Git which files & folders to ignore:

  • .DS_Store will ignore files named DS_Store

  • folderName/ will ignore an entire directory

  • *log will ignore any files with the .log extension


Summary

  • Staging area groups specific changes together, in preparation for committing.

  • Every commit to a repository has a unique identifier called a hash.

  • You can redo a commit by using --amend for adding any missed out files.

  • A commit message should encompass a single feature, change or fix.

  • Describe your commit messages in an imperative mood.

  • You can specify the names of the files, you want to ignore in .gitignore file.


Thank You <3

git commit -m "YOU ARE AMAZING"

Damn, you've reached this point 🥳👏. This shows that you are a keen learner🔥. Now do not stop here. There is much more to learn in Git.

Writing blogs is not as simple as we think 🥲. It requires planning, resources, understanding and most importantly TIME🧑‍💻. I would be very much grateful if you could just share this blog post among your fellow peers (Linkedin, Facebook, or Whatsapp, etc). Thank You ❤️

Other valuable posts

https://h4rsha.hashnode.dev/git-101-shell-commands-mastery

https://h4rsha.hashnode.dev/all-the-basics-you-need-to-start-working-with-git