How to git (diff, log, remote, push)

Read Time 2 minutes

In the previous post we learnt how to create an empty git repository, added new files, viewed repo status using git status and committed local changes.

Today, we are going to learn how to see the difference using git diff and push local changes to a github remote repository.

git diff

We can see the difference between commits and files using git diff command. So let’s add some more text in firstfile.txt and see.

Sohails-MBP-2:myRepo sohailaziz$ echo "add another line" >> firstfile.txt 

Sohails-MBP-2:myRepo sohailaziz$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:   firstfile.txt

no changes added to commit (use "git add" and/or "git commit -a")
Sohails-MBP-2:myRepo sohailaziz$ git diff firstfile.txt 
diff --git a/firstfile.txt b/firstfile.txt
index efbc80b..e76e99b 100644
--- a/firstfile.txt
+++ b/firstfile.txt
@@ -1 +1,2 @@
 this is my first file
+add another line
Sohails-MBP-2:myRepo sohailaziz$ 

So we added “add another line” and git status tells us that firstfile.txt has been modified. We can add and commit this file again.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Menlo; color: #000000} span.s1 {font-variant-ligatures: no-common-ligatures}
Sohails-MBP-2:myRepo sohailaziz$ git add firstfile.txt 
Sohails-MBP-2:myRepo sohailaziz$ git commit -m "modifies firstfile.txt with some more text"
[master 3d9e6e9] modifies firstfile.txt with some more text
 1 file changed, 1 insertion(+)
Sohails-MBP-2:myRepo sohailaziz$ 

git log

We can check the commit history using git log command. It shows all the details about a commit with commit hash, message, date and author name.

Sohails-MBP-2:myRepo sohailaziz$ git log
commit 3d9e6e96353756afda13280a913841bffa83d269 (HEAD -> master)
Author: Sohail Aziz <sohail.aziz05@gmail.com>
Date:   Mon May 18 01:51:37 2020 +0200


    modifies firstfile.txt with some more text


commit 776bb5a7705654dbe496a5bcd00435c2b38fc751
Author: Sohail Aziz <sohail.aziz05@gmail.com>
Date:   Mon May 18 00:01:48 2020 +0200


    adds firstfile.txt as an example

So we have made two local commits. Next step is to push them to github remote repository. We first need to create a github repository. Create a new github account or signIn using Google. Follow https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-new-repository to create a new repository.

Next is to set remote to your local repository.

git remote

We can add remote for our local repository using git remote add command. Just copy the repository url in clone section and use it as follows. Sohails-MBP-2:myRepo sohailaziz$ git remote add origin https://github.com/aLazyHacker/lazyAppRepo.git

Sohails-MBP-2:myRepo sohailaziz$ git remote add origin https://github.com/aLazyHacker/lazyAppRepo.gi

This basically set the origin of our local repository to

https://github.com/aLazyHacker/lazyAppRepo.git

Now whenever we pull or push code it will be pulled and pushed from/to this remote repository.

git push

Final step is to push changes in local repository to our remote repository. We use git push command for this purpose. Since we have already added remote origin, following command will push all the local changes to remote repository.

Sohails-MBP-2:myRepo sohailaziz$ git push

In the next post, I’ll be explaining how you can create a remote repository first, clone it locally, create and checkout new branch and push local changes back to remote.

You can watch this how-to tutorial on lazyHacker youtube channel at https://youtu.be/I7YPfKQNr8k and the source code at aLazyHacker at github

Happy coding ðŸ™‚

Leave a Comment

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