這是一張有關標題為 Using Git to Upload Code to a Remote Repository 的圖片

Using Git to Upload Code to a Remote Repository

Managing multiple remote repositories locally and syncing them to different remote repositories.

Introduction

We can use git clone to copy a remote repository to our local machine. When editing code, we can create branches, add commits, and push them again to the remote repository. This is possible because when cloning a remote repository, Git automatically sets a default remote repository name origin that corresponds to the remote repository’s URL.

But what if we initialize a new repository locally using git init? How do we connect to a remote repository?

Or, if we want to keep an unfinished, private codebase locally and store it in our personal remote repository, how do we push the completed code to the team repository within the same development environment using multiple remote repositories?

This article will explain how to push code to multiple repositories using VS Code.

Initializing a Local Git Repository

Previously discussed articles:

  1. Initializing a Git Directory
  2. Initializing a Local Repository
  3. Source Control

The process is straightforward and will not be repeated here.

Creating a Remote Git Repository

To push local code to a remote repository, you can choose platforms like GitHub, GitLab, Azure DevOps, or even a local NAS.

In the previous article, What is a Remote Repository?, we introduced these concepts and provided links to create new repositories, such as the GitHub Create Repository.

A key point is that a remote repository can be created on a local NAS to avoid uploading company code to the cloud. By creating a folder on the NAS and initializing a remote repository, the folder name becomes the repository name.

Once the remote repository is set up on the NAS, users must authenticate via SSH or similar methods to clone or push from other computers. For convenience in this tutorial, we will use the same computer to initialize both the local Git repository and the remote repository.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
wells@server:~/$ mkdir mdm9607-repo
wells@server:~/$ cd mdm9607-repo
wells@server:~/$ git init --bare

Hint: Set 'master' as the name of the initial branch. This default branch name can be changed.
Hint: If you want to set the initial branch name for all new repositories,
Hint: call (this will hide the warning):
Hint:
Hint:  git config --global init.defaultBranch <name>
Hint:
Hint: Common branch names besides 'master' include 'main', 'trunk', and
Hint: 'development'. A newly created branch can be renamed with this command:
Hint:
Hint:  git branch -m <name>
Initialized empty Git repository in /home/wells/mdm9607-repo/

Displaying Current Remote Repositories

Since git remote does not have many complex input options, it is simpler to operate without using a UI.

After opening the Git project folder with VS Code, open the terminal. Enter git remote -v to display the current remote repositories for the Git project.

Since no remote repositories have been added yet, there will be no output.

1
2
wells@server:~/mdm9607$ git remote -v
wells@server:~/mdm9607$ 

To create a new remote repository on GitHub, you will receive a repository URL.

1
git@github.com:WellWells/PRIVATE_REPO.git

We can add the GitHub repository to the current git directory by using git remote add <remote-name> <URL>. Here, I will name this repository origin.

1
2
3
4
wells@server:~/mdm9607$ git remote add origin git@github.com:WellWells/PRIVATE_REPO.git
wells@server:~/mdm9607$ git remote -v
origin  git@github.com:WellWells/PRIVATE_REPO.git (fetch)
origin  git@github.com:WellWells/PRIVATE_REPO.git (push)

In Git Graph, right-click on the current main branch and select Push Branch… to push the current main branch to GitHub.

Push to Origin

Adding a Remote Server via VS Code UI

The following example uses a local URL for demonstration.

  1. Press F1 to open the command palette.
  2. Enter ‘remote’ to search for related commands, and select Git: Add Remote.

Enter Command Git Remote

  1. For the URL, enter: ~/mdm9607-repo
  2. Name the repository: NAS

After entering these details, the remote repository will be added.

However, VS Code does not directly provide an option to display remote repositories. We can use the git remove command to view the current remote repositories.

  1. Press F1
  2. Enter git remove to see the currently added remote repositories.

Enter Command Git Remove

Pushing to Multiple Remote Repositories

If there are some commits you do not want to push to a remote server, you can create a new branch and use rebase or cherry-pick to avoid uploading sensitive information. Use git diff to compare the branches and, once confirmed, push to the remote server.

For example, in this project, we don’t want to upload private keys. So, we created a release branch and pushed it to the NAS by right-clicking on the branch and selecting push. Since there are multiple remote repositories, we choose NAS for the release.

Select the remote repository to push

Git clone your clone

When we clone a repository from a remote, the URL corresponds to the remote repository.

Locally, we can clone the local URL again to create another copy. In this case, the corresponding remote repository is the local directory.

1
2
3
4
5
wells@server:~$ git clone mdm9607 mdm9607-2
wells@server:~$ cd mdm9607-2
wells@server:~/mdm9607-2$ git remote -v
origin  /home/wells/mdm9607 (fetch)
origin  /home/wells/mdm9607 (push)

The benefit is that we can develop multiple features in parallel without cloning the entire repository from the remote server again, saving time. This is particularly useful when the project directory has many compiled files, and switching branches leads to recompilation.

You can also use the cp command, but cp will copy all uncommitted and untracked files. It depends on your preference which command to use.

Conclusion

Using git remote, you can fetch and push to multiple different remote repositories, allowing the local repository to sync with multiple remote servers. By utilizing these techniques in practice, you can manage synchronization between local and remote repositories, achieving more efficient version control and collaboration.

References

  1. Git - Working with Remotes
  2. unix - git clone vs cp -R
Theme Stack designed by Jimmy