How to push code to GitHub using Git

How to push code to GitHub using Git

Jun 9, 2022ยท

2 min read

Play this article


GitHub is a cloud-hosted Git management tool for software development and version control. It offers the distributed version control and source code management functionality (SCM) of Git.

In this article, I'll show you how to use Git to push code to GitHub.


Step:1 Create a GitHub Repository

Sign in to GitHub and create a new repository. If you want, you can initialize the repository with README by selecting the Add a README file option. new-github-repo

Step:2 Initialize git in the project folder

Run the following commands from the terminal inside the project folder:

Initialize git

$ git init

If you've already initialized git, you may skip this step.

This command generates a file named .git in the project folder's root directory.

Add files to the index

$ git add -A

The git add command is used to tell git which files to include in a commit, and the -A argument is used to add all files to the commit.

  • Commit added files
    $ git commit -m "Initial commit"
    The git commit command is used to commit the newly added files, and the -m argument is used to specify the commit message.

Add remote origin

$ git remote add origin

Note: Remember to replace the username and repo with your own.

Push code to GitHub

$ git push -u -f origin master

There are a few things to consider in this regard. The -f flag represents force. Everything in the remote directory will be overwritten automatically. We're only using it here to overwrite the README that GitHub generated automatically. If you skipped that step, the -f flag isn't really required.

The -u option makes the remote origin the default. This allows you to easily do git push and git pull later without having to specify an origin because we always want GitHub in this case.

All combined

$ git init
$ git add -A
$ git commit -m 'Initial commit'
$ git remote add origin
$ git push -u -f origin master


You can now use git to push code changes to GitHub.

Thank you for reading!

Let's Connect