How to Make an Initial Push to a Remote Repository With Git

John Wachira Feb 02, 2024
How to Make an Initial Push to a Remote Repository With Git

This article outlines the steps needed to push local changes to a remote repository with Git. A repository is virtual storage for projects that allows us to keep different versions of our code which we can access at any given time.

Since development is done locally, you will need to publish your changes to a remote repository.

This article is ideal for developers using a server as their remote repository and those hosting repositories on GitHub.

Make an Initial Push to a Remote Repository With Git

To make our initial push, we will start a project from scratch. First, we will set up a remote repository on the server side.

On the command line, we will run the commands below.

mkdir my-project.git

This command will create a new project on our server called my-project.git. You can give your project the name you desire.

We can now initialize an empty Git repository. Navigate to the project you created using the cd command, as illustrated below.

cd my-project.git

To initialize a repo, run:

git --bare init

The command above will initialize an empty repo in our folder. You can add the --shared flag if you intend to make this a public repository.

It will set up the permissions required. That is all for the server side; let’s move to the client side.

On our local machine, we will create a new project called my-project. On the command line, we will run:

mkdir my-project

Once created, we will open the project with the cd command, as illustrated below.

cd my-project

To initialize a repository on the client side, run:

git init

At this point, we have set up a remote and local repository. Git does not allow us to push branches that do not have any commits.

We will have to add files to our project and commit the changes before we can push.

Let’s create a README.md file and add it to the index for commit.

touch README.md

This will create a file in our repository. If you run the git status command, the file will appear under the Untracked category.

Run the command below to add it to the index.

git add README.md

If you have dozens of untracked files, you can run the git add command with a . to instruct Git to add all the files to the index.

git add .

We can now commit and push the changes. To commit, we will run the command below.

git commit -m "Initial Commit"

You can run the git log command to confirm the commit. Before we can push our changes, we will have to link our local repo to our remote repository.

This is possible with the git remote add command. We will run:

git remote add origin youruser@yourserver.com:/path/to/my-project.git

Note that different hosting servers have different formats and schemas for adding origin.

We can now push to the remote.

git push --set-upstream origin master

This command will push our changes to the remote repository and set up our local master branch to track the remote master branch. This way, we can run git push without origin master every time we need to publish our local changes.

What if you are using GitHub to host your remote repository?

It is pretty straightforward. Assuming you have already created an empty repository on your GitHub account, the process of pushing changes is similar to what we have seen above.

You will need the repository URL for your GitHub repository.

Here is an example.

We will first link our local repository with the GitHub repository using the git remote add command, as shown below.

git remote add https://github.com/youruser/my-project.git

The command above is for those using HTTPS authorization. For SSH, you will run:

git remote add git@github.com:youruser/my-project.git

We can then push the changes to the remote repository, as illustrated below.

git push --set-upstream origin master

There you have it. We have successfully published our changes to a remote repository for the first time.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Push