Push your Code to GitHub Repository using Command Line

Keeping a track of your changes and its history is very important for a developer. GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

In this post we will push our code to the created repository in GitHub using the Command line with Git. Check out my previous post where we saw How to Create GitHub Repository?

Pre-requisites

  1. Git : Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
  2. GitHub Account

Push your Code to GitHub

You can make use of Git Bash or Command line for this purpose. Open the terminal from the project folder. To do that, on the navigation path type cmd and hit enter. It will open the terminal from the project directory.

To initialize empty local git repository, type the below command.

git init

Next we need to add files to the staging area for Git. Before the files are committed to a repository, the files needs to be added to the Staging area. You can either add individual files or directories or add all unstaged files using below command.

git add .

For adding individual file, type the file name in place of the dot.

git add README.md

Now we need to commit our code changes made to the files to a local repository. Each commit will have an unique ID for the reference. It is important to add a commit message as well, that will tell us what changes we have made.

git commit -m "first commit"

Below is the output similar to this you will get when you commit any changes.

To see the branches in your repository, use the below command.

git branch

With a -m or -M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.

git branch -M main

To connect a local repository with a remote repository. In this case, our remote repository is GitHub. Please give the URL of your repository. It will be like this – https://github.com/<username>/<repository name>. You may be asked to provide your GitHub credentials if you are doing it for the first time.

git remote add origin <GitHub Repository URL>.git

Finally we push our code to the GitHub and also mention the branch.

git push -u origin main

Below is similar response you will get when code is pushed.

Sometimes there are changes in your GitHub Repo (may be changed by your team member) which are not present locally, in such cases, you might receive an error message as below.


So first we need to get the changes from remote repository to our local repository. This command will get all the changes to the local repo.

git pull

It is a good practice to pull the changes first and then make your changes on top of it. There are many other git commands available. I have covered the basic ones which is sufficient for you to get started. To learn more about the git commands, refer the official documentation here.

Thank you All!!! Hope you find this useful.

If you liked our content and it was helpful, you can buy us a coffee or a pizza. Thank you so much.


3 thoughts on “Push your Code to GitHub Repository using Command Line

Add yours

Up ↑

%d bloggers like this: