git add & git commit

git add & git commit

Β·

5 min read

Overview:

In this blog I talk about -

  • Why one should not create a repo inside a repo.
  • Git workflow
  • git add command
  • git commit command
  • git log command

Pretty interesting stuff. Let's dive into it, shall we?πŸ™Œ

Important :

Before jumping onto 'Committing & Adding' we've established in the previous blog that one should not create a repo inside a repo. But why is it so?

When we initialize a git repo that's in one directory, in our case, it was the ProjectOne directory. Know that Git watches everything that happens in that directory, including anything nested further down (ie... child or grandchild directories) For example:

image.png

  • We were inside the ProjectOne dir and initialized the repo there. in that particular dir -> made another dir webdev, let's say I want to work on a navbar -> made another dir Navbar inside it. Hence now, we are 2 directories deep.
  • when I checked the git status it says -> we are currently in a git repo!

Hence, it's not just the ProjectOne dir that gets tracked by git, it's everything Nested in the folder as well.

So, if we initialize (run git init command) another repo inside an existing repo it'll be like Git tracking Git or a workspace inside a workspace which will get confusing.

To prevent this:

  • Always run git status command to check whether you are already in a repo or not.
  • If you do happen to initialize a repo inside an existing one, simply delete the corresponding .git folder.

GIT Workflow:

image.png

  • Make changes: Refers to making new files, edit files, delete files etc.
  • Stage/Add changes: Group specific changes together & add them, in preparation of committing. (This is done with git add command)
  • Commit changes: Commit everything that was previously added. Here we actually update the .git folder (also known as the hidden folder). With every commit Git is updating this folder -> the actual git repository

Hence, committing is not a single step process - it is a multi-step process. There are intermediate steps to call out the particular change that we want to include in a commit & then commit those changes. This allows us to selectively make commits!

Now we can answer the question, What is a Commit?

image.png A commit is a checkpoint in time, basically a snapshot of changes in our repository. Eventually, we will be able to go back to earlier commits, merge commits, undo commits.

Staging changes with git add

The first step towards committing the changes is adding those changes first. image.png image.png Here, via touch command I've created a file index.html & opened it with text-editor Visual Studio Code. image.png image.png

  • I wrote some HTML boilerplate code in index.html file ie.. made some changes.
  • When I ran git status command git notices the changes made and shows the above message for index.html file. image.png image.png
  • Here I made another file info.txt and wrote some Lorem Ipsum text in it.
  • Now Git shows the above message for both the files. There are 2 files that Git is aware of and its changes but is not tracking.

Here comes the real deal:

Adding: Using git to add specific files to staging area.
Separate files with spaces to add multiple at once.

Staging area can be understood as "the pet store, once you bring a pet home you're committed" ~ StackOverflow
(This is one of the best analogies I've come across about the staging area & it totally makes sense.)

image.png

Command: git add <file name>
or we can use
Command: git add .
we can use git add .(dot) to stage all changes at once. image.png Now git status shows that, we've staged only one file that is - index.html to commit.
I could just commit this one file but I want to group the two files together into ONE commit because I made changes to both of them. image.png So, whenever we make a commit those two files will be included.
For more, check out Git Add Documentation

Finally! git commit

Git Commit: We use git commit command to actually commit changes from the staging area.

When making a commit, we need to provide a commit message that summarizes the changes and work snapshotted in the commit.

Running git commit will commit all staged changes. It also opens up a text editor & prompts you for a commit message. An alternate to this is running this command:
git commit -m "my message"
The -m flag allows us to pass an inline commit message, rather than launching a text editor.

image.png

  • Here, I commit the changes along with a message. It says 2 files changed with 20 insertions.
  • Now when we run git status it says nothing to commit, and working tree clean- which means everything we've done in the working directory git knows about & git has been tracking & it's up to date!

For more, check out Git Commit Documentation

Another command git log

image.png

  • It retrieved information of the log of the commit that we made. Includes -> Author, Date/Day/Time of the commit & the commit message.

Congratulations! Pat yourself on the back for learning & understanding how we actually 'add changes' and 'commit' them in a repository! πŸŽ‰

Do share this blog with someone who you think will get benefitted from it. Share your questions and feedback in the comment section as well πŸ˜ŠπŸ™Œ

Β