【问题标题】:How to add my current project to an already existing GitHub repository如何将我当前的项目添加到已经存在的 GitHub 存储库
【发布时间】:2013-05-29 15:53:15
【问题描述】:

我对 Git 很陌生。我一直在寻找答案,但找不到答案。

在我的电脑中,我有一个这样的项目文件夹:

project_a
--some_folder
--another_folder
--.git

我在 GitHub 上有一个存储库,比如说https://github.com/company/our_repo.git。在这个存储库下我有一些文件夹。所以我的目标是将我的project_a 放在trunk/bin 下。我如何实现这一目标? (再说一次,我是非常非常非常新的。)

【问题讨论】:

    标签: git github version-control git-remote


    【解决方案1】:

    打开您的终端,访问此文件夹并写入:

    git init
    git add .
    git commit -m "my commit"
    git remote set-url origin git@github.com:username/repo.git
    git push origin master
    

    【讨论】:

    • 但我的目标是将我的project_a 放在现有的repo 下,这样在 github 上它看起来像这样repo/trunk/bin/project_a
    • 然后首先将现有的 repo 克隆到本地计算机的文件夹中。然后将 project_a 添加到 trunk/bin 文件夹。然后推送到github。
    • 注意:如果您是第一次为您的文件夹执行此操作,那么您应该使用git remote add 而不是git remote set-url
    • @b_dubb 假设您有“工作区”文件夹,并且在此文件夹中您有“新”文件夹,其中包含要添加到现有存储库的代码。现在,您进入“工作区”文件夹并 git clone 那里的现有仓库,文件夹名称为“existing”。现在,在“workspace”文件夹中,您有两个文件夹:“existing”包含来自现有 repo 的代码,“new”包含要添加到现有 repo 的代码。现在,将代码从“new”文件夹复制到“existing”文件夹,进入“existing”文件夹,然后提交并将更改推送到远程服务器。
    • @ZiyaddinSadigov 感谢回复。我昨天结束了这样的事情。但对于任何好奇的人来说,我的问题的简短答案是肯定的。如果你 git clone 信息一个包含文件的目录,你将丢失该代码。
    【解决方案2】:

    在我的终端中导航到我想要添加到存储库的目录时,我更幸运了,然后(假设您正在处理一个名为 master 的分支):

        git init
        git add .
        git commit -m "my commit"
        git remote add origin <remote repository URL>
        git push origin master
    

    这是一篇文章的链接,该链接更详细地解释了如何做到这一点:https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

    请注意,您将无法运行“git add”。如果有问题的目录是打开的,则行。

    【讨论】:

    • 工作完美。
    • 这对我有用,但我必须先拉动,然后推动,然后它才起作用。否则我得到一个错误,提到“更新被拒绝,因为你当前分支的尖端落后”
    【解决方案3】:
    1. first create a git repostry.
    2. second open git bash in  existing or uploading project.
    3. perform git init 
    4. git add .
    5. git commit -m "print message"
    6. git remote add github<repostry url>
    7. git remote -v
    8. git push github master
    

    git push origin master
    

    如果遇到任何错误,您可以使用它

    git push -f origin master
    

    【讨论】:

    • 这是 2021 年对我有用的答案。
    【解决方案4】:

    以上所有答案似乎都可以指导在 git 中创建新存储库,但问题是关于将文件夹添加到现有存储库。为此,可以按照以下步骤操作。

    • 使用以下命令克隆您现有的存储库: git clone https://github.com/company/our_repo.git
    • 手动将您的项目文件夹带到所需位置,即trunk/bin
    • 现在提交,然后使用以下命令推入 repo: git commit -m "message"git push origin master

    【讨论】:

      【解决方案5】:

      我认为最好先将现有的 Github repo 拉到本地,然后将新文件添加到 Github repo

      此链接会有所帮助:https://stackoverflow.com/a/61285076/5840973

      【讨论】:

        【解决方案6】:

        假设我想将FreeRTOS 存储库(URL 为https://github.com/FreeRTOS/FreeRTOS-Kernel.git)添加到我的存储库中,示例URL 为https://github.com/username/example 作为子模块

        git submodule add https://github.com/FreeRTOS/FreeRTOS-Kernel.git
        git add .
        git commit -m 'add a submodule'
        git push
        

        使用 HTTPS 进行克隆:

        git clone https://github.com/username/example.git --recurse-submodules
        

        使用 SSH:

        git clone git@github.com:username/example.git --recurse-submodules
        

        如果您在没有使用--recurse-submodules 参数的情况下下载了repo,则需要运行:

        git submodule update --init --recursive
        

        【讨论】:

          【解决方案7】:

          当你要推送已经存在的仓库时,你必须使用-f

          git init
          git add *
          git commit -m "Initial commit"
          git branch -M main
          git remote add origin <repo url>
          git push -f origin main
          

          【讨论】:

          • 警告:只有在您不想保留该仓库中已有的内容时,使用git push -f 才是安全的!这里的许多答案都假设您没有,但是指定的原始问题在那里存在现有文件。事实上,我觉得奇怪的是 stackoverflow.com/a/51018115/3216427 不是被接受的答案,因为接受的答案还假设您在遥控器上没有预先存在的提交。
          猜你喜欢
          • 2017-04-06
          • 2012-09-23
          • 2020-05-12
          • 2020-01-09
          • 2013-07-01
          • 1970-01-01
          • 2014-12-24
          • 1970-01-01
          • 2013-01-04
          相关资源
          最近更新 更多