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
At present, all the 4 files are in staging area
Right now we are in a sub-folder viz testdir. Let us ‘cd’ to root folder viz gitrepo
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>”
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
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
Copy the code, paste it in ‘git show <code>’ command
So see below. We now see the 4 new files that were committed
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’
Hit Enter, you would see that we can now edit the file
Type <h1>This is h1 header</h1> in your notepad and copy it
Paste it inside the vi editor
To save the file, type Esc key followed by colon : followed by wq
Hit Enter
Now to make sure that the file is modified, you can see the file contents by using the ‘cat’ command
Let us now see the status command output. Notice the red text that says we have modified demo.html
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.
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 .’
Next let us execute status command, see below. As expected it shows that we have staged the demo.html file
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
The log command will show us 2 commits, we will also see the respective messages for those 2 commits, see below
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
Let us once more edit the file
Right now we have
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
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)
Next, the status command shows us that a file has been modified
Thank you for reading!
Also Check: Selenium Online Training