Hello Sunil
Essential Git commands - Basic to advance with examples

Essential Git commands : Basic to advance with examples

Git is no longer tracking “Link.txt” file. In this simple way, you can untrack files if necessary. As an alternative to “rm –cached “, you can use the “reset” command too.

git reset <file_name>

You can consider “reset” as the opposite of “add”.

#5 git commit

The git commit command is run after the git add command. In our git add example, you had added the files for tracking, but you haven’t committed it to the Git repository yet.

Tracking stays local until a commit is made. When you commit the changes, they become part of the permanent record.

Now type the following command in your terminal:

git commit -m ‘Write your commit statement’

 git commit -m - git commands

To commit to a repository, use the “commit” command. Next, pass the “commit” command the “-m” option, which stands for “message”. Lastly, type in your commit message.

We wrote ‘First commit’ for our example, but it’s recommended that you write more meaningful messages like “Add admin panel” or “Update admin panel”.

Note: A commit message must tell what your commit does – adds or removes files, updates app features, and so on.

The message above tells you that there have been six files added to the current branch, which in our example is the master or the main branch. The “create mode 100644” message tells you that these files are regular non-executable files.

If you check the status now:

Checking the git status after our first commit in a git repo

Sometimes when working on a project, chances are you will modify some files and commit them many times. In other words, every time you make changes you will need to add a modified file to the staging area and then commit.

But this standard flow is tedious. And why should you have to ask Git to track a file that was tracked before?

Let’s edit our “file1.txt” and add some random text as follows and save it.

Edit file1.txt and add some random text

Now check the git status:

Check git status after modification of file1.txt

The question is how can you add modified files to the staging area and commit them at the same time.

Git provides the following super command to achieve it.

git commit -a -m ‘Write your commit statement’

Second commit after modification of file1.txt

Note: “-a” option, which stands for “add”.

Again, there will be times when you will regret committing to a repository. Let’s say you have modified ten files, but committed only nine.

How can you add that remaining file to the last commit? And how can you modify a file if you have already committed it? There are two ways out.

First, you can undo the commit by following command:

git reset –soft HEAD^

Undo the commit by using reset command

As you may recall, the “reset” command is the opposite of the “add” command. This time, “reset” tells Git to undo the commit. What follows “reset” is the “–soft” option. The “–soft” option means that the commit is cancelled and moved before HEAD.

You can now add another file to the staging area and commit, or you can amend files and commit them.

head-points-to-current-branch-in-git

What you see in the image is that each dot represents a separate commit, and the latest commit is at the top of the branch (HEAD). In the command “git reset –soft HEAD^” the last character “^” represents the last commit.

We can read “git reset –soft HEAD^” as “Undo the last commit in the current branch and move HEAD back by one commit.”

Note: To understand what that “HEAD” thing represents, recall that you work in branches. Currently you are in the master branch, and HEAD points to this master branch.

When you switch to a different branch later, HEAD will point to that different branch. HEAD is just a pointer to a branch.

Instead of resetting the HEAD and undoing the last commit, you can rectify a commit by using the “–amend” option when committing to a repository.

Just add the remaining file to the staging area and then commit:

git add ‘file_name’
git commit –amend -m ‘Write your commit statement’

Third commit by using amend command

Sunil Pradhan

Hi there đź‘‹ I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.

Add comment

Stay Updated

Want to be notified when our article is published? Enter your email address below to be the first to know.

Sunil Pradhan

Hi there đź‘‹ I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.