Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details

Introduction

 
In previous article, I described more details on configuration git in local machine, git clone and pull commands and how to implement it and create public and private repositories and their differences.
 
In this article, I will describe the below-mentioned points in detail.
  1. Git complete flow.
  2. How to push changes from local git to GitHub.
  3. What are git add, git commit, git status and git push commands.
  4. How developers share code from single repository.
Note 
Before going through this session, please visit my below-mentioned session related to Git and GitHub.
  1. Introduction To Git
  2. Steps For Configuring Git With Details
Let's understand Git Flow with an example,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Here in my GitHub account, I have created two repositories called SatyaOne and SatyaTwo. Now I describe this example by taking the repository called SatyaTwo. On this SatyaTwo repository there are two developers i.e. Local Dev One and Local Dev Two. These two developers are from different locations. Local Dev One is from UK location and Local Dev Two is from India location. Here Local Dev One is working on two different projects. So, The Local Dev One cloned from the two different repositories
called SatyaOne and SatyaTwo. Here Local Dev Two is working on one project. So, Local Dev Two cloned from one repository called SatyaTwo. So, How to create repository and clone repository, For this you need to visit my previous articles which links are mentioned as above.
 
Now I created two different folders called Dev1 and Dev2 in my local machine and clone the SatyaTwo repository from GitHub using Git Bash. After successful clone the output is shown as below,
 

SatyaTwo Repo In GitHub

 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
 
Local Dev One / Dev1 clone SatyaTwo Repo  
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
 
Local Dev Two / Dev2 clone SatyaTwo Repo
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Now Local Dev One and Local Dev Two clone single repository called SatyaTwo. Here the physical files that means where we created the files in local repositories that is nothing but the workspace. There is one workspace for each developers called Workspace One.
 
Workspace One For Local Dev One
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
Workspace One For Local Dev Two
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
Here .git folder and README.md are nothing but the physical files. So, when I create any physical files in local and want to push those files to GitHub repository then first i need to add those files in staging area or logical area in Workspace One. It should not be a physical area. 
 

What Is This Staging Area 

 
For example, I created or modified 10 more files in my local but i want to push only 2 files out of 10 files to GitHub repository. So, In that case, I want add these 2 files to my staging area.  These 2 files in staging area indicates that they are ready to be committed. 
 

What Is This Commit 

 
Moving the files from staging area to our local git repository. It is nothing but a commit. Once the files are committed to .git repository which is my local repository then I can push the files to GitHub repository.
 
So, To move our changes from local to GitHub repository we need to follow below steps.
  1. We have to add our files to staging area
  2. We commit to local git repository which is present in our local machine
  3. We push them to GitHub repository
When we commit any files to local git repository, It will create unique version ID for the tracking purpose. So, The same version ID will be pushed to GitHub repository along with changes or files. 
 

Let's Understand In Practical

 
As per earlier discussion, I already have created workspace for Local Dev One and Local Dev Two. Here in Local Dev Two workspace I will add a new file called MyFile.cs.
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Then I open Git Bash in Local Dev Two workspace as shown in below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
To get the new files along with existing files which are present in Local Dev Two workspace as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
To get the content of this new file called MyFile.cs as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
To move this new file from local git to my GitHub repository SatyaTwo, We need to add this new file to the staging area using command called git add as shown below,
 
Full command is : git add MyFile.cs 
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Before executing this command I will show you very important command called git status as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Untracked files in case of MyFile.cs means this file is not present in staging area.
 
So, first we need to add this new file to staging area using git add command as mentioned earlier. After executing this command then check status and the result will be as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Changes to be committed in case of MyFile.cs means this file is now present in staging area and it is now ready to be committed. That means my physical files can't commit directly into local git. So, for this I need to add this file from physical files to staging area and from staging area we can commit directly into local git repository. Now my files are added in staging area and I will commit these files to local git repository. For this, I need a command called git commit.
 
Full command is : git commit -m "Add new file called MyFile.cs" 
 
Here the message or comment I added that is "Add new file called MyFile.cs". That indicates what is the purpose behind this commit. So, That other user can easily identify the specific commit reason in GitHub repository.
 

Let's know about Author identity unknown

 
During run of git command if you face the below issue like Author identity unknown, then you need to set user email and user name using below commands,
  • git config user.email "satya_inet@xxxx.com"
  • git config user.name "sa**aCsgit**"
Using the above command mention your own Email-ID and User Name during creation of GitHub account. Check the image as shown below and try the same,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Then run the below command as shown in image for commit,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Now my new file is committed successfully to local git repository from staging area. Here you get a message that 1 file changed, 25 insertions. That means 1 file changed is nothing but one new file called MyFile.cs and 25 insertions is nothing but the total number of lines in this new file as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Now I will run git status command and here you see there is no new files or existing files in local which means all files are committed to local git repository. Let's see as below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Then I need to keep the same new file from local git repository to GitHub repository.  For this we need a command called git push.
 
Full command name : git push origin main
 

Authorize Git Credential Manager 

 
First it asks for authorization as shown below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
After click on this button, It will ask for your GitHub account password. After given proper credentials your authentication will be successful as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Then, It starts pushing changes from local repository to GitHub repository as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Now I can see new file in my GitHub repository with comments given during commmit command as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Also you can see the updated content of this file as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details

Let's know about sharing file between different Developers

 
Here the Local Dev One wants same file as Local Dev Two has already pushed the file to GitHub repository. Local Dev One connects to same GitHub repository.
 
For this Local Dev One will open Git Bash and type command git pull as shown below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
Now the Local Dev One has same file as below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
Now Local Dev One can see this file using ls command using Git Bash as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
Next, Local Dev One wants to modify this file as per below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
 
Also, Local Dev One wants to create 2 new files called MyFile2.cs and MyFile3.cs as shown below,
 
Content of MyFile2.cs
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
 
Content of MyFile3.cs
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
 
Now, Local Dev One wants to move these files from local to GitHub repository as shown below,
 
The list of files 
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Check the status 
 
Here you can see the modified file name as well as 2 newly created files as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details
 
Here the existing file called MyFile.cs changes is not committed to staging area and the new files called MyFile2.cs and MyFile3.cs are under untracked files means these files are not in Git history. So, I need to add first these new files to staging area but only MyFile2.cs not MyFile3.cs. Finally, I want to commit MyFile.cs and MyFile2.cs files to staging area and push those files to GitHub repository and changed made in MyFile3.cs put in local itself.
 
Run below commands as shown in image, 
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
If I check the status then see the result as already ready to be committed to staging area and not committed files are shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Then commit those changes to the staging area using command as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Then push those files from local git to GitHub repository using command as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Here the command is : git push origin main. The origin here nothing but the alias name of your GitHub repository. Here main is nothing but the default branch name as shown in below image,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
In GitHub repository, You can see those committed files are pushed by Local Dev One with added comments as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
So, as per mentioned in flow diagram the Local Dev One is working on multiple repositories and projects. To know which repository the Local Dev One has pushed these changes using command as shown below,
 
The command is : git remote -v
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
It shows the GitHub repository name is SatyaTwo. 
 
Here you can see the MyFile.cs content in GitHub repo as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Here you can see the MyFile2.cs content in GitHub repo as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Now Local Dev Two wants these changes to his/her workspace using command as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
After successful run above command, Those files are available in Local Dev Two workspace with latest changes as shown below,
 
Let's Know About Git Add, Git Commit, Git Status And Git Push 📥📤 In Details 
 
Here clone and pull are with single commands but for commit and push we need few steps. Those steps are already described using flow diagram as mentioned above.
 

SUMMARY

 
In this write-up, we have learned the below details,
  • Understand Git complete flow.
  • How to push changes from local git to GitHub.
  • What are git add, git commit, git status and git push commands.
  • How developers share code from single repository.
  • What is Author identity unknown.
  • What is Authorize Git Credential Manager.
                                                                     ****Thank You & Stay Tuned For More****