z_tjona
z_tjona

Reputation: 680

How to publish an organization repository in Github using VSCode

I added my files and commited with no problem. The problem is that when I press "Publish to Github" the only options there appear are private and public repositories as individual. But I would like to publish as an organization. enter image description here

Upvotes: 53

Views: 21427

Answers (6)

Antonius Bloch
Antonius Bloch

Reputation: 2729

I initialized the repository and tried adding via VS Code's 'Git: Add Remote`, but I got an error:

Remote name format invalid

So I just wanted to point out that the command line is always there. Open the terminal, navigate to the root dir of the repo and:

git remote add origin https://github.com/YourOrganization/YourRepo.git

That worked for me with no issue.

Upvotes: 1

almaceleste
almaceleste

Reputation: 467

The built-in VS Code GitHub extension does not currently (in 2024) have the ability to publish a local repository to an organization.
But you can easily create an organization repository from a local copy using the GitHub CLI.

gh repo create organization-name/repo-name --source . [--private|--public|--internal]

--source string - Specify path to local repository to use as source
--internal - Make the new repository internal
--private - Make the new repository private
--public - Make the new repository public

Most modern popular Linux distros have an official GitHub CLI in their system package repository, so you can install it using your favorite package manager. The GitHub CLI package is usually called gh or github-cli.
macOS has gh package in brew.
On Windows you can install GitHub CLI with release files from the official GitHub CLI repository.

Upvotes: 1

NetYogi
NetYogi

Reputation: 601

By default, GitHub allows you to publish to your own public or private repositories. To publish to an organization repository, follow the steps below -

  1. Create an organization repository in GitHub, if not already created.

  2. Press "Initialize Repository" in VS Code's "Source Control" and commit the code.

  3. Open the Command Palette in VsCode by clicking Ctrl+Shift+P, select "Git: Add Remote", and paste your organization's repository URL. You can also open the "Source Control" tab, go to the Remotes section, click the plus sign, and paste the URL of your organization's repository.

This should add the repository as a remote. This enables you to push the code to your organization's repository.

Handling Errors - Sometimes we may get errors like "failed to push some refs to...". This occurs when your local repository hasn't been updated with the changes in the remote repository. To fix this error, open the terminal and execute the following commands -

  • git pull --rebase origin main

  • git push -u origin main

NOTE- The origin in the above command refers to the organization repository's URL.

If the first command is successful, you should get a response 'Successfully rebased and updated refs/heads/main'

Now whenever you push/pull any code, it will update seamlessly to/from the organization's repository.

Upvotes: 4

Stuart
Stuart

Reputation: 86

Github Desktop allows you to choose the organization to publish. Just click the "publish repository" and choose the organization to publish at the bottom list.

Upvotes: 3

JCoordes
JCoordes

Reputation: 217

I just wanted to add another workaround as I came across the same issue.

  1. Publish as a private or public repository within your personal GitHub instance
  2. Open the repository and go to "settings" (GitHub UI) enter image description here
  3. Use the "transfer ownership" option to transfer the repository to your organization. enter image description here
  4. Done. As from my experience tools like VS Code and VS 2022 will not loose the connection to the repository and you can still commit code without further changes (Assuming the correct permissions of the organization are provided to you).

Upvotes: 13

zoku
zoku

Reputation: 7236

Workaround for the lack of functionality:

You can add your project to a local repository by pressing "Initialize Repository" in your "Source Control" tab.

After that, go to Github and make a new empty organisation repository.

Now you can hit F1 and type "Add Remote". You can now choose "Git: Add remote..." and then paste the repository's URL, instead of adding from Github.

Now committing and pushing should work as expected.

Upvotes: 43

Related Questions