Tuesday, June 13, 2017

Git for Beginners - Part 5 - Git Special Commands

 


In this tutorial, I will show you special commands in Git. These commands will help you to use Git in very efficient way.

1. Git diff:   

       If you add some file to the staging area and then you change the content of the file, you can notice that $ git status will say that your file has been modified. The difference of current status of the file and status of the staging area is tracked by this $ git diff command. 

As an example, I have changed the content of the file1. 



Here I have added '!' for the second line and 'find the difference' statement. 

1. I haven't changed the first line. Therefore it appears in white color.
2. In second line I added '!'. Therefore it shows the difference between the current status of the file1        (in green color) and the status of the file1 (in red color) in the staging area.
3. I have added a new third line. Therefore it shows in green color.

Note: Remember when you stage files with "git add" you are duplicating files. That is why you can get the difference of current working file with the same staging area file.  

Once you add the file1 and see the $ git diff, you cannot see any difference. 

2. Git diff --cached:

   
        When you modify some file and add this to the staging area, this will show you the difference between previous state and the current state of the file.

For an example, I will use the same above example.  


You will see the same differences which are mentioned earlier. 

3. Git log:

        
         $ git log will show you a log of all your commits in your repository. This command shows the most recent commits first. You can scroll down and see the first commit as well. 


When "git log" is too long to fit on the screen, it will throw you into a command line editor. To exit the editor, hold Shift + zz. Otherwise, you will be stuck in there. 

4. Git log --oneline:

         This command will give you all commits in your repository as messages in very small space. 

5. Git commit -a -m "msg":

         If you add a new file or modify some file, you cannot commit them directly. So first you need to add these files, then commit them. Using this command we can commit quickly without adding the files.


6. Git status -s:

          This tells you what has been modified in your repository in shorthand. Basically, this will save the screen space of your terminal.



7. Remove the latest commit:

         $ git reset --hard will delete your working directory changes. Make sure that you keep your local changes secretly before running this command. 
Assume you are sitting on that commit, use $ git reset --hard HEAD~1 to remove it.  

8. Git reflog show:

        This command will show all the information  such as commits, checkouts, merging etc. 








0 comments:

Post a Comment

Copyright © iTecTricks | Powered By Blogger

Design by Shehan Vanderputt