Let's Know About Git Add, Git Commit, Git Status And Git Push πŸ“₯πŸ“€ In Details

Introduction

In the previous article, I described more details on the configuration of git in the 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 a 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.

Github

Here in my GitHub account, I have created two repositories called SatyaOne and SatyaTwo. Now I describe this example by taking the repository calledSatyaTwo. 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 the UK location and local Dev Two is from the India location. HereLocal Dev One is working on two different projects. So, the local Dev One was cloned from the two different repositories

called SatyaOne and SatyaTwo. HereLocal Dev Two is working on one project. So, Local Dev Two cloned from one repository calledSatyaTwo. So, How do create a repository and clone a 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 theSatyaTwo repository from GitHub using Git Bash. After a successful clone, the output is shown below,

SatyaTwo Repo in GitHub

Repo In GitHub

Local Dev one /Dev1 clone SatyaTwo Repo

Local Dev

Local Dev two /Dev2 clone SatyaTwo Repo

Β Dev2 clone

Now Local Dev One and Local Dev Two clone a single repository called SatyaTwo. Here are the physical files which means where we created the files in local repositories that are nothing but the workspace. There is one workspace for each developer called Workspace One.

Workspace one for local Dev One

Workspace One

Workspace one for local Dev Two

README

Here .git folder and README.md are nothing but physical files. So, when I create any physical files locally and want to push those files to the GitHub repository then first I need to add those files in the 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 the GitHub repository. So, In that case, I want to add these 2 files to my staging area. These 2 files in the staging area indicate that they are ready to be committed.

What Is This Commit?

Moving the files from the staging area to our local git repository. It is nothing but a commitment. Once the files are committed to the .git repository which is my local repository then I can push the files to the GitHub repository.

So, To move our changes from the local to the GitHub repository we need to follow the below steps.

  1. We have to add our files to the staging area
  2. We commit to the local git repository which is present in our local machine
  3. We push them to the GitHub repository

When we commit any files to the local git repository, It will create a unique version ID for tracking purposes. So, The same version ID will be pushed to the GitHub repository along with changes or files.

Let's Understand In Practical

As per the earlier discussion, I already have created a workspace for Local Dev One and Local Dev Two. Here in the Local Dev Two workspace, I will add a new file calledMyFile.cs.

Myfile

Then I open Git Bash in the Local Dev Two workspace as shown below image.

New volume

To get the new files along with existing files that are present in the Local Dev Two workspace as shown below.

 New files

To get the content of this new file called MyFile.cs as shown below.

 Content

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 a command called git add as shown below,

Full command is

git add MyFile.cs

MyFile.csΒ 

Before executing this command I will show you a very important command called git status as shown below.

 Git status

Untracked files in the case of MyFile.cs mean this file is not present in the staging area.

So, first, we need to add this new file to the staging area using the git add command as mentioned earlier. After executing this command then check the status and the result will be as shown below.

Staging

Changes to be committed in case ofMyFile.cs means this file is now present in the 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 the staging area, and from the staging area we can commit directly into the local git repository. Now my files are added in the staging area and I will commit these files to the 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 is the message or comment I added that is"Add a new file called MyFile.cs". That indicates what is the purpose behind this commitment. So, That other users can easily identify the specific commit reason in the GitHub repository.

Let's know that Author's identity is unknown

During the run of the git command if you face the below issue like Author identity unknown, then you need to set the user email and user name using the below commands.

git config user.email "[email protected]"
git config user.name "sa**aCsgit**"

Using the above command mention your own Email and User Name during the creation of the GitHub account. Check the image as shown below and try the same.

Identity

Then run the below command as shown in the image for commit.

Commit

Now my new file is committed successfully to the local git repository from the 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 and25 insertions are nothing but the total number of lines in this new file as shown below.

 Local git

Now I will run the git status command and here you see there are no new files or existing files locally which means all files are committed to the local git repository. Let's see as below image.

Git repository

Then I need to keep the same new file from the local git repository to the 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.

Authorization

After clicking on this button, It will ask for your GitHub account password. After being given proper credentials your authentication will be successful as shown below.

 Proper credentials

Then, It starts pushing changes from the local repository to the GitHub repository as shown below.

Pushing changes

Now I can see a new file in my GitHub repository with comments given during the commit command as shown below.

Commmit command

Also, you can see the updated content of this file as shown below.

 Updated content

Let's know about sharing files between different Developers

Here Local Dev One wants the same file as Local Dev Two has already pushed the file to the GitHub repository. Local Dev One connects to the same GitHub repository.

For this Local Dev One will open Git Bash and type the command git pull as shown below image.

Git Bash

Now the Local Dev One has the same file as the below image.

GitRepo

Now Local Dev One can see this file using the ls command using Git Bash as shown below.

.git

Next, Local Dev One wants to modify this file as per the below image.

 Modify this file

Also, Local Dev One wants to create 2 new files called MyFile2.cs and MyFile3.cs as shown below.

Content of MyFile2.cs

Content of MyFile2.cs

Content of MyFile3.cs

Content of MyFile3.cs

Now, Local Dev One wants to move these files from local to the GitHub repository as shown below.

The list of files

 List of filesΒ 

Check the status

Here you can see the modified file name as well as 2 newly created files as shown below.

Created files

Here the existing file called MyFile.cs changes is not committed to the 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 the staging area but only MyFile2.cs, not MyFile3.cs. Finally, I want to commit MyFile.cs and MyFile2.cs files to the staging area and push those files to the GitHub repository and changes made in MyFile3.cs put in local itself.

Run the below commands as shown in the image.

Run command

If I check the status then see the result as already ready to be committed to the staging area and not committed files are shown below.

Commited to staging

Then commit those changes to the staging area using the command as shown below.

Staging area

Then push those files from local git to the GitHub repository using the command as shown below.

Push

Here the command is : git push origin main. The origin here is nothing but the alias name of your GitHub repository. Here main is nothing but the default branch name as shown below image.

Code

In the GitHub repository, You can see those committed files are pushed by Local Dev One with added comments as shown below.

Packages

So, as mentioned in the 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 the command as shown below.

The command is

git remote -v

Check

It shows the GitHub repository name is SatyaTwo.

Here you can see the MyFile.cs content in the GitHub repo as shown below.

 Content in GitHub

Here you can see the MyFile2.cs content in the GitHub repo as shown below.

History

Now Local Dev Two wants these changes to his/her workspace using the command as shown below.

Workspace using command

After successfully running the above command, Those files are available in the Local Dev Two workspace with the latest changes as shown below.

Latest changes

Here clone and pull are with single commands but for commit and push we need a few steps. Those steps are already described using a flow diagram as mentioned above.

Summary

In this write-up, we have learned the below details.

  • Understand Git's 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 a single repository.
  • What is the Author's identity unknown?
  • What is Authorize Git Credential Manager?

Thank You & Stay Tuned For More


Similar Articles