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 the 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 the GitHub repository after pushing changes from local git to the 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 the 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.

Empty repository

Here SatyaThreeEmptyRepo is a public repository but I have not initialized it with the Add a README file option and finally clicked on Create repository. After successful creation, the interface will be shown as below.

SatyaThreeEmptyRepo

If you go to the list of repositories then you can see this repository as an empty repo as shown below.

Repositories

Steps to initialize a git repository

There are some git commands that we need to follow as shown below.

 git repository

I have created a new directory called Dev3 and there I added 3 files as shown below.

 Dev3

The content of this file are same as the content of the files used in the previous session. So, You can check my previous article link as mentioned above. Now I want to initialize this SatyaThreeEmptyRepo repository locally using the command git init and it will show a message that Initialized empty Git repository in F:/MyGitRepo/Dev3/.git/ as shown below.

Initialize

Now if we go to the local directory then we can see a .git directory here as shown below but before that, there is no .git directory.

Local directory

Now let me add the README.md file to the Dev3 workspace using the command echo "# SatyaThreeEmptyRepo" >> README.md as shown below.

README

Now check the list of files using the command ls as shown below.

Check list

Next, I need to add the README.md file to the staging area using the command git add README.md and check the file's status as shown below.

Add README

So, to add multiple files at one time to the staging area using a single command git add . and check the status as shown below. Here "." is nothing but the current directory which is Dev3.

Multiple files

Also, I can add multiple files using the command git add MyFile.cs MyFile2.cs MyFile3.cs as shown below.

Here git add. means it adds all files to the staging area and git add MyFile.cs MyFile2.cs MyFile3.cs means it added these 3 files to the staging area. If you want to add selective files then by specifying the file names using the git add command you use the command as git add file 1 file 2 file 3 .... file n.

Add command

There is another command called git add --all. Using this command we can add all files with the current directory called .git workspace as shown below.

 Another command

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 does not add the current directory to the staging area.
  • git add file 1 file 2 .... file n: It adds only these mentioned files with space separation to the staging area.
  • git add --all: It adds all files including the current directory called .git in the Dev3 workspace to the staging area.

Then I commit those files to my local git repositories using the command git commit -m "first commit" as shown below.

Local git repositories

The list of files is committed from the staging area to the local git repository as shown below.

  1. MyFile.cs
  2. MyFile2.cs
  3. MyFile3.cs
  4. README.md

So, for these committed files only one commit ID will be generated but no different commit IDs for each file. In the previous article, I described the details of committing ID in GitHub.

Now, I need to add the GitHub repository URL using the 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 purposes as described earlier in my previous article.

GitHub repository URL

Now I check my GitHub repository using the command git remote -v as shown below.

Command git remote

Now I need to push these 4 files from local git to My GitHub repositories using the command git push origin master as shown below. Here origin is nothing but the alias of GitHub and the master is nothing but the name of the default branch. So, When I created a GitHub repository 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 pushing files to the GitHub repository requires an internet connection to access the remote workspace that is GitHub.

 Git Bash

After the internet is connected, now I am able to push these files to the GitHub repository successfully as shown below,

Push

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.

Pushed files

Here we can see one commit -- 4 files are pushed. So, as per the earlier discussion, it should be one commit ID for 4 files instead of 4 different commit IDs as shown below.

Different commit IDs

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.

Commit ID

Now I modified one file called MyFile3.cs by adding a new line of code as shown below.

Code

In the next step, I will check the status of this file. Then add this file to the staging area.

Staging area

In the next step, I will commit this file to the local git repository and then push this file from the local git repository to the GitHub repository as shown below.

 Local git repository

Now, we can see the MyFile3.cs with the latest changes with the commit message and the no. of commit is updated to 2 as shown below.

MyFile3

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 the latest commit changes of this file.

Latest commit changes

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 the staging area using a single command

Thank You & Stay Tuned For More


Similar Articles