Tutorial 5: Commit files to GIT

What you will Learn:

 

Commit file to local GIT repository


Let us suppose, we build some feature and get to a point where we can say “This code can now be committed”. So, commit is a safe point before starting to implement another feature, recall below diagram

Commit files to GIT

At present, all the 4 files are in staging area

Commit files to GIT

Right now we are in a sub-folder viz testdir. Let us ‘cd’ to root folder viz gitrepo

Commit files to GIT

Now, we will commit all these files to our local GIT repository by running the command (argument ‘m’ is for message) having below syntax:
git commit -m “<some meaningful message>”

Commit files to GIT

Notice the above output. It says ‘4 files changed’.

Also notice the output of status command, it says ‘nothing to commit’. The reason being, we took the files from staging area and we created a safe point in commit history

Commit files to GIT

Commit files to GIT

So, “git commit” will save your file to local GIT repository.

‘git log’ and ‘git show’ commands

Now, how can we see the changes that we have committed?

The ‘git log’ command would show us a commit code that is unique to this commit, the author, date, the commit message. However this command still does not show us the file names that we committed

Commit files to GIT

Copy the code, paste it in ‘git show <code>’ command

Commit files to GIT

So see below. We now see the 4 new files that were committed

Commit files to GIT

 

Modify a file

 

Let us now modify a file using vi editor. You can also open the file using notepad or any other editor to edit it.

Also Read: Tutorial 3: Create Git Repository

Type ‘vi demo.html’

Commit files to GIT

Hit Enter, you would see that we can now edit the file

Commit files to GIT

Type <h1>This is h1 header</h1> in your notepad and copy it

Commit files to GIT

Paste it inside the vi editor

Commit files to GIT

 

Commit files to GIT

To save the file, type Esc key followed by colon : followed by wq

Commit files to GIT

Hit Enter

Now to make sure that the file is modified, you can see the file contents by using the ‘cat’ command

Commit files to GIT

Let us now see the status command output. Notice the red text that says we have modified demo.html

Commit files to GIT

 

git diff

Let us now execute the ‘git diff’ command. This command gives us the difference between what you have in current working directory and what has been committed. We know that in the current working directory we have added a new line (see the + sign in the figure below, the green line color signifies addition). However the committed file does not have this line.

Commit files to GIT

So basically, the difference is that we have added the new line <h1>This is h1 header</h1>

Also Read: Tutorial 4: Initialize GIT

Now let us execute ‘git add .’

Commit files to GIT

Next let us execute status command, see below. As expected it shows that we have staged the demo.html file

Commit files to GIT

Let us now commit this file with a message. Make sure that the messages are meaningful, they should describe the actual change that you have made. As you can see below, it says 1 file changed and there is 1 insertion

Commit files to GIT

The log command will show us 2 commits, we will also see the respective messages for those 2 commits, see below

Commit files to GIT

Copy the code for the latest commit and use it in the below command, notice the last line that shows we added a new line

Commit files to GIT

Let us once more edit the file

Commit files to GIT

Right now we have

Commit files to GIT

Type dd to delete the above line

To save the file, type Esc key followed by colon : followed by wq

Notice that the file is blank now

Commit files to GIT

The diff command now tells us that we have removed a line (see minus sign in the last line, the line is in red color)

Commit files to GIT

Next, the status command shows us that a file has been modified

Commit files to GIT

Thank you for reading!

Also Check: Selenium Online Training

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024