About Git #03
Tags: git, tutorial, beginners
Published: 2021-10-28 04:21:55

In this tutorial we are touching some more commands and aspects about Git.

#Short status

Let's modify a file in your directory.
`echo hey >> file1.txt`
When you run `git status` it gives quiet a lot of information. But you can have summary status by simply using the command,
`git status -s`
![7_gitStatus-S](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/7_gitStatus-S.PNG?token=APOCKO5HWG744YXRUKO3F4LBPIQIO)

As you can see in the above screen shot we have a red 'M' as we have done modifications which have yet not in the staging area.
Let's add file1 to the staging area and get the git status.
![8_gitStatus-S.PNG](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/8_gitStatus-S.PNG?token=APOCKOYXEE3J3CQRAUHG3ODBPIQJ2)
Now you can see the green 'M'.
Now let's change file1 again and get the status.
`echo there >> file1.txt`
`git status -s`
![9_gitStatus-S.PNG](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/9_gitStatus-S.PNG?token=APOCKO2LYEJAFRNG6N7H7XTBPIQMG)

Now you can see a green 'M' in left for the changes in the staging area which we have not yet committed, and a red 'M' in right for the changes we have not yet added to the staging area.

#Staged and un-staged changes
To check what we have in the staging area you can use the command,
`git diff --staged`
![10_gitDifStaged](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/10_gitDifStaged.PNG?token=APOCKOYHSQ2UNPPMFRQPER3BPIQNO)

The output I got is shown in the above screen shot.
Here we are comparing the a/fil1.txt with b/file1.txt. In other words, we are comparing two copies of file1.txt. The oldest copy is a/file1.txt. So the changes in the old copy is stated in a minus (-) sing while plus (+) sign indicates the changes of the new file. The 7th line gives us an idea on what parts of our code has changed. The part with minus sign as a prefix tells about the old copy. Hence according to my screen shot, in old copy starting from line 1, 3 lines have been extracted and shown in the terminal ( check the lines hello, world, and test in the terminal). In the new copy starting from line 1, 5 lines have been extracted. With lines hello, world, and test two more words hey and there have been added to the new copy. That is why they are green and has + as a prefix.

Incase you want to see the changes in our working directory that are not staged yet, you can run the command `git diff`.
You can also use a diff tool like LDiff3, P4Merge, WinMerge, and VSCode to compare files. Here I am going to use VSCode as our default diff tool. So you have to set two configuration settings;
`git config --global diff.tool vscode`
`git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"`
Now check whether everything is done correctly by running the command `git config --global -e` to edit the global configuration settings in the default editor.
Now run the command `git difftool` and as shown below you have to give 'Y' to open the VSCode.
![11_DiffTool](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/11_DiffTool.PNG?token=APOCKO66FPB7L4W244VXAKLBPIQP2)

When VSCode is open it will display two copies (old and new) of the file that we have changed.
![12_DiffTool.PNG](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/12_DiffTool.PNG?token=APOCKO4VKXQXZLHVS5W2VVLBPIQQE)

Now it is much easier to check and compare the changes.

#View History

For us to view the history of our repository we can use the command `git log`.
![13_gitCommit](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/13_gitCommit.PNG?token=APOCKO367P6OU4TPKF3XQP3BPIPX6)

It displays our commits from latest to the earliest. Each commit has a unit identifier. And each commit segment displays its author, the date of commit, and your commit message. If you feels like this is too informative, you can only view commit id and message with the code `git log --oneline`
![14_history01](https://raw.githubusercontent.com/chathuRashmini/Blog-Images/main/Git/14_history01.PNG?token=APOCKO6SBFZZA3A76GZDELTBPIPZM)

You can also reverse the commit order and have the initial commit at top using the command, `git log --oneline --reverse`.

#View a commit

To check what we have changed in a given commit we can use the command `show` by specifying the commit you want. There are two ways to specify commits;
1. Using its unique Id
1. Using head pointer

##1. Using commit id
`git show 2d7eddb`
Here you need not to write complete id. You can specify first few characters of the id if there is no other id that starts with those same characters.

##2. Using head pointer
By running the command `git log --oneline` you can check where your head pointer is at the moment. Then to trace backward you should use the symbol **'~'**. For example if you want to trace one commit back from the current head pointer, then the command should be,
`git show HEAD~2`
If you set 1, that means you are referring the current head pointer. Incase you want to see all the files and directories in a commit you can use the command,
`git ls-tree HEAD~2`

#Unstaging Files

How can you revert a commit that you have done? You should undo the add command. Hence you need to use the command,
`git restore --staged fileName.extention`
Now file you have specified in the above command is now not in staging area.

#Discarding Local Changes

To discard local changes you can use a similar command like the one above,
`git restore fileName.ext`

Now you went through three tutorials about Git. So did you ~~Git~~ get it??? 😉