简体   繁体   English

在远程 GIT 存储库上推送/创建新目录而不克隆整个存储库,只需使用 git init 初始化

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

I got little bit lazy, so I wanted to take a shortcut:我有点懒,所以我想走捷径:

I wanted to upload some new programming examples to my GIT remote repo, so I created VS solution (for non .NET pepole, just a simple directory) on my desktop.我想将一些新的编程示例上传到我的 GIT 远程存储库,因此我在桌面上创建了 VS 解决方案(对于非 .NET 人员,只是一个简单的目录)。 So it looks like this:所以它看起来像这样:

  • Desktop桌面
    • some desktop file.txt一些桌面文件.txt
    • some catalog一些目录
    • MyProgrammingExamples我的编程示例

So, in order to push MyProgrammingExamples onto GitHub repo, I navigated in PowerShell to my desktop, fired因此,为了将 MyProgrammingExamples 推送到 GitHub 存储库,我在 PowerShell 中导航到我的桌面,然后触发

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

Now, I'd liek it to push it onto remote, so further I run commands:现在,我想把它推到远程,所以我进一步运行命令:

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

But it gives me:但它给了我:

fatal: The current branch master has no upstream branch.致命:当前分支 master 没有上游分支。

So I tried:所以我试过:

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

which reulsted in这导致

fatal: remote master already exists.致命:远程主机已经存在。

Well, it would be good that someone explain what is hapenning, but what I am really after is to how to achieve my goal, ie create catalog somewhere (anywhere, not a GIT catalog) and then easily push it to existing remote repository.好吧,有人解释正在发生的事情会很好,但我真正想要的是如何实现我的目标,即在某处(任何地方,不是 GIT 目录)创建目录,然后轻松地将其推送到现有的远程存储库。

According to this SO post , such operation is impossible, but there's workaround:根据this 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

Then we switch to local repo connected to the remote one, which is up to date and do:然后我们切换到连接到远程仓库的本地仓库,这是最新的,并执行以下操作:

# 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创建一个新的repo,只需从visual studio登录github,然后使用visual studio中的git菜单来完成git支持的所有事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM