Steps To Initialize A Git Repository And Push The Changes To GitHub 📥📤 In Details

Introduction

 
In a previous article, I described the steps that Git uses to maintain the commit IDs and Version IDs. The same commit IDs are shown in GitHub repository as it is created in local git during commit changes. Also, I described more details about the additions and deletions symbols in changed files of GitHub repository after pushing changes from local git to GitHub repository.
 
In this article, I will describe the below-mentioned points in detail.
  1. Steps to initialize a git repository
  2. Using this repository how to push the changes to GitHub
  3. How to add multiple files to staging area using a single command and commit
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
  3. Let's Know About Git Add, Git Commit, Git Status And Git Push In Details
  4. Let's Understand Git Maintains History Of Version Or Commit Ids With Details

Steps to create an empty repository

 
We learned the steps for how to create a new repository in my previous session. Here I will create an empty repository named SatyaThreeEmptyRepo as shown below.
 
 
Here SatyaThreeEmptyRepo is a public repository but I have not initialized with Add a README file option and finally click on Create repository. After successful creation the interface will be shown as below.
 
 
If you go to list of repositories then you can see this repository as empty repo as shown below.
 
 

Steps to initialize a git repository

 
There is some git commands that we need to follow as shown below.
 
 
I have created a new directory called Dev3 and there I added 3 files as shown below.
 
 
The content of these file are same as the content of the files used in previous session. So, You can check my previous article link as mentioned above. Now I want to initialize this SatyaThreeEmptyRepo repository in my local using command git init and it will show a message that Initialized empty Git repository in F:/MyGitRepo/Dev3/.git/ as shown below. 
 
Now if we go to local directory then we can see a .git directory here as shown below but before that there is no .git directory. 
 
 
Now let me add README.md file to Dev3 workspace using command echo "# SatyaThreeEmptyRepo" >> README.md as shown below.
 
 
Now check list of files using command ls as shown below.
 
 
Next I need to add the README.md file to staging area using command git add README.md and check the files status as shown below.
 
 
So, to add multiple files at one time to staging area using single command git add . and check status as shown below. Here "." is nothing but the current directory that is Dev3. 
 
 
Also I can add multiple files using command git add MyFile.cs MyFile2.cs MyFile3.cs as shown below.
 
Here git add. means it is added all files to staging area and git add MyFile.cs MyFile2.cs MyFile3.cs means it is added these 3 files to staging area. If you want to add selective files then by specifying the file names using git add command then you use command as git add file 1 file 2 file 3 .... file n.
 
 
There is another command called git add --all . Using this command we can add all files with current directory called .git workspace as shown below.
 
 
 
Difference between commands git add . / git add file 1 file 2 .... file n / git add --all 
  • git add . : It adds all new files and modified files but not added current directory to staging area.
  • git add  file 1 file 2 .... file n : It adds only these mentioned files with space separation to staging area.
  • git add --all :   It adds all files including current directory called .git in Dev3 workspace to staging area.
Then I commit those files to my local git repositories using command git commit -m "first commit" as shown below.
 
 
The list of files are commited from staging area to local git repository as shown below.
  1. MyFile.cs
  2. MyFile2.cs
  3. MyFile3.cs
  4. README.md
So, for these commited files only one commit ID will be generated but not different commit IDs for each file. In the previous article, I described the details on commit ID in GitHub.
 
Now, I need to add GitHub repository URL using command git remote add origin https://github.com/satyaCsgithub/SatyaThreeEmptyRepo.git as shown below.
 
Here origin is the alias of GitHub and https://github.com/satyaCsgithub/SatyaThreeEmptyRepo.git is nothing but the GitHub repo URL. This URL is used for clone purpose as described earlier in my previous article.
 
 
Now I check my GitHub repository using command git remote -v as shown below. 
 
 
Now I need to push these 4 files from local git to My GitHub repositories using command git push origin master as shown below. Here origin is nothing but the alias of GitHub and master is nothing but the name of the default branch. So, When i created a GitHub repository that s created a default branch called master for my repository.
 

Make Sure Internet Connection Is Active

 
I got this message when I ran  my push command using Git Bash as shown below. This is because to push files to GitHub repository, it requires internet connection to access remote workspace that is GitHub.
 
 
After the internet is connected, now I am able to push these files to GitHub repository successfully as shown below,
 
 
If I refresh my GitHub repository page then we can see a different interface as expected as shown below. There we can see those pushed files and in this way the GitHub empty repository can be filled with files.
 
 
Here we can see one commit -- 4 files are pushed. So, as per earlier discussion it should be one commit ID for 4 files instead of 4 different commit IDs as shown below.
 
 
If I click on 1 commit link as shown above then we can see these 4 files with contents with one commit ID as shown below.
 
 
Now I modified one file called MyFile3.cs by adding a new line of code as shown below.
 
 
In the next step, I will check status of this file. Then add this file to staging area. 
 
 
In the next step, I will commit this file to local git repository and then push this file from local git repository to GitHub repository as shown below.
 
 
Now, we can see the MyFile3.cs with latest changes with commit message and no. of commit is updated to 2 as shown below.
 
 
Let's see the latest changes of this MyFile3.cs as modified earlier as shown below. Here We can see a new Commit ID is generated with latest commit changes of this file.
 
 

SUMMARY

 
In this write-up, we have learned the below details,
  • Steps to create an empty repository
  • Steps to initialize a git repository
  • Using this repository how to push the changes to GitHub
  • How to add multiple files to staging area using a single command
                                                                                          ****Thank You & Stay Tuned For More****