Git Push Command Explained With Demo

Git is a DevOps tool used for source code management. It is one of the most prominent version control systems (VCS) today that is widely used to handle small and large projects efficiently. It helps in tracking changes in source code, enabling different people to collaborate on different parts of the same program. 

Some of the exciting topics covered in this Git Push Command article include:

  1. What is Git?
  2. What is GitHub?
  3. Different commands in Git
  4. Git push 
  5. Demo of the push command

Before diving into our tutorial on the Git Push Command, let's understand a little more about what Git is in general.

What is Git?

Git is a version control system for tracking changes in computer files. It helps in coordinating work amongst several people in a project and tracks progress over time. Unlike the centralized version control system, Git branches can be easily merged. A new branch is created every time a developer wants to start working on something. This ensures that the master branch always has a production-quality code.

GIT

Git is a distributed version control system, so here, every developer gets their local repository with full commit history. The commit history makes Git fast, as now a network connection is not needed to create commits or perform diffs between commits.

Now that we know more about Git let us explore GitHub.

Learn the basics of Devops

Watch the beginner level video lessons for FREEGet Access Now
Learn the basics of Devops

What is GitHub?

GitHub is a Git repository hosting service that provides a web-based graphical interface (GUI). It helps every team member work together on a project from anywhere, making it easy to collaborate. 

GitHub is one place where project managers and developers coordinate, track, and update their work, so projects stay transparent and on schedule. The packages can be published privately, within the team, or publicly for the open-source community. Downloading packages from the GitHub enables them to be used and reused. GitHub helps all team members stay on the same page and stay organized. Moderation tools, like issue and pull request locking, helps the team focus on the code.

github

Next in this article on the Git Push Command, let's look at the several commands used in Git.

Different Commands in Git

After learning about Git and GitHub, let us now look at the various Git commands:

  • Git config
  • Git init
  • Git add
  • Git diff
  • Git commit
  • Git reset
  • Git status
  • Git merge
  • Git push
  • Git pull

Next, let us get into the details of the Git push command. 

Git Push Command

The Git push command is used to push the local repository content to a remote repository. After a local repository has been modified, a push is executed to share the modifications with remote team members. Pushing is the way commits are transferred from the local repository to the remote repository. 

Git-push-command

Now that we have looked into the details about the Git push command, let us take a look at a demo of the Git push command using Git Bash.

Learn the basics of Git VCS and understand how to setup Git in your system with the Git Training Course. Check out the course now.

Demo of Git Push Command

Let's begin with opening Git Bash and configuring it with a user name and email ID.

To configure, we use the following commands:

Git config --global user.name "Simplilearn GitHub"

Git config --global user.email siddam.bharat@simplilearn.net

Git config –list

working-push-command

Then, let's check the current working directory:

pwd

taha

To create a repository in the working directory, use the following commands:

mkdir Git_Demo

cd Git_Demo

pwd

We can go to the directory location and check the Git_demo folder. 

taha

The directory "Git_demo" will be empty for now.

Let's create a folder for the repository.

mkdir FirstRepo

cd FirstRepo

pwd


first repo

The folder "FirstRepo" is empty. We will now initialize a repository to our folder.

Git init

git-init

Something called the "master" appears on the screen. Whenever a Git repository is created for the first time, it creates a branch, and the name of the branch is master. Navigate to the folder; you can find a hidden ".git" folder. 

If you check the folder, you can see several directories and configurations. Make sure you don't make any changes to any of the directories.

directories

This is created when a repository is initialized. 

DevOps Engineer Master's Program

Bridge between software developers and operationsExplore Course
DevOps Engineer Master's Program

Moving further, let's make some commits. For that, I will create two notepads and commit them one by one.

For the first notepad, the commands are as follows:

touch alpha.txt

notepad alpha.txt

A notepad opens on the screen. Type anything inside it, save it and close it.

 notepad

Next, let's check the status of the file that was created.

git status

This shows that there isn't a file committed yet, and there are untracked files. The untracked files can be seen in red.

seen-in-red.

For Git to track that file, add command is used. If you know the exact name of the file, you can specify it and simply type the following command:

git add .

untracked-files.

The next step is to commit the file.

git commit -m "alpha"

branch-master

Let's check the status of the file again.

git-status

git status

You'll notice that there are no more commits to be made, as there was a single notepad and that was committed in the previous step.

Next, check all the information regarding the commits that were made.

git log

git-log

This displays the commit ID, author's name, and email ID used. You can also find the date and commit message on the screen.

Let's make one more commit.

Repeat the same process again. I will make a notepad, add something to it, and close it.

touch beta.txt

notepad beta.txt

git add .

git commit -m "beta"

git-comit

git log

git-log-2

We can see the commit number and order of the commits.

Now, let's push the two notepads on GitHub. Open your GitHub account, and create a new repository. The name of the repository will be "FirstRepo."

firstrepo.J

Copy the "git remote add origin" URL.

Paste the copied URL onto the Git Bash.

git-bash

git remote -v

content

Now, let's push the content on to the remote repository.

git push -u origin master

origin-master

The repository is created on the server, and the content is pushed into that repository. It links the master branch on the local repository to the master branch on the server.

Next, refresh the GitHub page, and you can find all the commits there.

Each commit has a hash ID, which contains the details of each commit.

github_simpli.

You can open each notepad and check the content inside.

github-simpli-2

Enroll in DevOps Engineer Masters Program and learn to work on tools like Docker, Git, Jenkins, Puppet & Nagios.

Conclusion  

We hope this article helped you understand the Git Push Command helped you in detail. You have learned the basics of the push command and followed a hands-on demo of the Git Push command using Git Bash. In the demo, we saw how files from the local repository could be pushed to the remote repository. The process makes it possible for the team to stay updated on different people performing different tasks in the same program.

Learn More

If you would like to learn more about using Git, check out our DevOps Engineer Master's Program. This comprehensive program will enable you to gain expertise in the principles of continuous development and deployment, automation of configuration management, inter-team collaboration and IT service agility, using DevOps tools such as Git, Docker, Jenkins and more. Enroll now to learn everything you need to know about the immensely popular VCS.

About the Author

Ishan GabaIshan Gaba

Ishan Gaba is a Research Analyst at Simplilearn. He is proficient in Java Programming, Data Structures, and Project Management. Graduated in Information Technology, Ishan is also passionate about writing and traveling.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.