JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Initial push to the remote repository using Git

Author:JIYIK Last Updated:2025/03/29 Views:

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

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

This article is ideal for developers who use a server as a remote repository and for those who host their repositories on GitHub.


Initial push to the remote repository using Git

To do the 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 following command.

$ mkdir my-project.git

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

We can now initialize an empty Git repository. Use the cd command to navigate to the project you created, as shown in the following image.

$ cd my-project.git

To initialize the repo, run:

$ git --bare init

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

It will set the required permissions. That’s all for the server side; let’s move on 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

After creation, we will open the project using the cd command as shown in the following image.

$ cd my-project

To initialize the repository on the client side, run:

$ git init

At this point, we have set up our remote and local repositories. Git does not allow us to push a branch that does not have any commits.

Before pushing, we have to add the files to our project and commit the changes.

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 following command to add it to the index.

$ git add README.md

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

$ git add .

We can now commit and push our 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 push our changes, we must link our local repository to our remote repository.

This can be done with git remote addthe command. We will run:

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

Please note that different hosting servers have different formats and patterns to add origins.

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 our local master branch to track the remote master branch. This way, every time we need to publish local changes, we can run git push without the origin master.

What if you use GitHub to host your remote repository?

It's pretty straightforward. Assuming you've already created an empty repository on your GitHub account, the process for pushing changes is similar to what we saw 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 above commands are for users using HTTPS authentication. For SSH, you would run:

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

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

$ git push --set-upstream origin master

At this point we have successfully published changes to the remote repository for the first time.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial