Pushing/creating 远程 GIT 仓库上的新目录,无需克隆整个仓库,只需使用 git init 进行初始化

Pushing/creating new directory on remote GIT repo without cloning whole repo, just initialized with git init

有点懒,想走捷径:

我想将一些新的编程示例上传到我的 GIT 远程仓库,所以我在桌面上创建了 VS 解决方案(对于非 .NET 用户,只是一个简单的目录)。所以它看起来像这样:

因此,为了将 MyProgrammingExamples 推送到 GitHub 存储库,我在 PowerShell 中导航到我的桌面,启动了

git init
git add MyProgrammingExamples
git commit -m 'some message'

现在,我想把它推送到远程,所以我 运行 命令进一步:

git remote add 'url to my repo'
git push origin master

但它给了我:

fatal: The current branch master has no upstream branch.

所以我尝试了:

git remote add master --mirror=push 'url to my repo'

这导致了

fatal: remote master already exists.

好吧,最好有人解释一下发生了什么,但我真正想要的是如何实现我的目标,即在某处(任何地方,而不是 GIT 目录)创建目录,然后轻松将其推送到现有的远程存储库。

根据这个SO post,这样的操作是不可能的,但是有一个解决方法:

# create new branch and commit to it, then push to origin
# (assumed, that remote repo is already added, as in question)
git checkout -b single_file_branch
git add file_to_add_to_repo
git commit -m 'some message'
git push origin

然后我们切换到连接到远程仓库的本地仓库,这是最新的并执行:

# pull newest changes (as well as the branch)
git pull
# merge branches, allowing unrelated histories (without it it would generate an error)
git merge --allow-unrelated-histories origin/single_file_branch

您可以直接从 visual studio 创建一个新的存储库,只需从 visual studio 登录到 github,然后使用 visual studio 中的 git 菜单做 git!

支持的所有事情