首页 文章

将现有项目推送到Github

提问于
浏览
180

我有一个包含项目源的文件夹 . 我如何将这个项目推入Github的存储库?

我尝试使用这个步骤:

  • 我在GitHub上创建了空存储库 .

  • 我运行git-bash并输入 git init ,所以在项目根目录下出现 .git 文件夹 .

  • 我使用 git add sourcesFolderName 将一些文件添加到版本控制

  • 我使用 git commit -m "initial commit" 提交了上一步添加的文件

  • 我使用 git remote add MyProject <url> 指定了远程存储库

  • 最后 git push ,但没有任何东西被推送到远程仓库...(没有授权失败)

那么我如何将现有的资源推送到新创建的github repo?

14 回答

  • 92
    git init
    git add .
    git commit -m "Initial commit"
    git remote add origin <project url>
    git push -f origin master
    

    git push 上的 -f 选项强制推送 . 如果你没有't use it, you' ll会看到这样的错误:

    To git@github.com:roseperrone/project.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'git@github.com:roseperrone/project.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first merge the remote changes (e.g.,
    hint: 'git pull') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    
  • 31

    技术性较差

    我的答案没有什么不同,但我正在添加更多信息,因为那些新的信息可以从填补信息空白中获益 .

    在github上创建repo后,他们会有说明 . 你可以关注那些 . 但是这里有一些额外的提示,因为我知道开始使用git是多么令人沮丧 .

    假设您已经在本地启动了项目 . 你有多少无所谓 . 但是让我们假装你有一个php项目 . 假设您有index.php,contact.php和包含图像,css和字体的assets文件夹 . 你可以这样做(简单),但有很多选择:

    选项1

    登录您的github帐户并创建回购 .

    enter image description here

    在下面的屏幕中,如果单击按钮(屏幕右侧)为"clone in desktop",则可以将其复制到需要的位置 .

    enter image description here

    您可以(或以其他方式执行)然后将现有项目中的内容复制到新的仓库中 . 使用github应用程序,您可以使用他们的GUI(这意味着您只需单击应用程序中的按钮)从那里提交 . 当然,您输入提交的注释 .

    选项2

    • 如上所述在github上创建您的仓库 .

    • 在计算机上,使用终端转到您的目录 . 使用linux命令行,您将 cd 放入目录 . 从这里运行以下命令,将现有项目"connect"发送到github上的repo . (这假设你在github上创建了你的repo并且它当前是空的)

    首先执行此操作以初始化git(版本控制) .

    git init
    

    然后执行此操作将所有文件添加到"monitored."如果您有要忽略的文件,则需要添加 .gitignore 但为了简单起见,只需使用此示例进行学习 .

    git add .
    

    然后你提交并在 "" 之间添加一个注释,如"first commit"等 .

    git commit -m "Initial Commit"
    

    现在,您可以在此处添加现有仓库

    git remote add github <project url>
    

    但是不要输入 <project url> ,而是输入您自己的项目URL . 你怎么做到的?转到您的repo在github上的链接,然后复制链接 . 在我的情况下,我的一个回购是 https://github.com/JGallardo/urbanhistorical 所以我的结果url这个命令只会添加 .git 之后 . 所以这就是

    git remote add github https://github.com/JGallardo/urbanhistorical.git
    

    测试看它是否有效

    git remote -v
    

    你应该看看你的回购链接到了什么 .

    enter image description here

    然后,您可以将更改推送到github

    git push github master
    

    要么

    git push origin master
    

    如果仍然出现错误,可以使用 -f 强制它 . 但是,如果您在团队环境中工作,请注意不要强迫或者您可能会产生更多问题 .

    git push -f origin master
    
  • 6

    推送时需要指定 which branchwhich remote

    ➤ git init ./
    ➤ git add Readme.md
    ➤ git commit -m "Initial Commit"
    ➤ git remote add github <project url>
    ➤ git push github master
    

    将按预期工作 .

    您可以通过以下操作默认设置:

    ➤ git branch -u github/master master
    

    这将允许您从主服务器执行 git push 而无需指定远程或分支 .

  • 3

    如果你在Mac上(这可能在PC上运行相同),这是一个非常简单的方法 . 奇怪的是,对于这个简单的过程,我看起来很高,而且从未找到它 .

    • 不要在Github上做任何事情(除了拥有一个帐户,并没有用尽所有可用的回购) .

    • 下载GitHub for Mac并安装 . 完成帐户设置等 . 不要为现有项目创建任何存储库 .

    • "Add New Local Repository"在存储库中 .

    • 选择现有文件夹 . 它会问你是否愿意这样做,说是的 .

    • 完成后,您将看到所有文件的列表等 . 提交它们 .

    • 转到存储库和发布(如果您正确设置了帐户,这将为您在GitHub上创建新的存储库) .

    • 转到存储库并推送(你'll either see the 1340404 thing, or it' ll将你的文件/更改推送到新自动制作的仓库) .

    • 不知道为什么你在其他地方找不到这个简单的过程 .

    我知道不建议将项目文件夹用作repo文件夹 . 我一直这样做,它总是有效,它使它变得简单,我从来没有遇到任何麻烦 .

  • 280

    我将按照Rose P.之前的评论 . 我花了很长时间才找到解决方案,所以我重新发布(希望用简单的英语)对我有用的东西......

    步骤1:在Github.com上创建新的存储库(如果已有,请跳过)

    第2步:关闭XCode ......不需要

    第3步:打开一个新的终端窗口(是的,你必须使用终端......我尝试了所有其他方式......没有用)

    步骤4:使用命令cd查找项目的文件夹位置(要添加到现有或新存储库的项目)

    第5步:输入git init你会得到这样的东西 . 重新初始化/ 中的现有Git存储库

    第6步:输入git add . 在此步骤之后没有任何反应,但键入它并继续下一步 .

    第7步:输入git commit -m“初始提交”你将获得以下内容:#On branch master无提交,工作目录清理

    要么

    关于配置的一些解释,然后是已更改的文件列表 .

    步骤8:键入git remote add origin 项目URL可以在Github.com中找到 . 它是HTTPS克隆URL ...你应该能够只复制并粘贴到终端窗口 . 如果系统告诉您原点已存在,请创建一个不同的名称或使用您的项目名称(不同的东西)

    步骤9:转到Mac上的GitHub应用程序,然后单击“同步分支”按钮(即使没有挂起的更改) . 我认为实际提交需要一段时间,但是如果你回到本地存储库文件夹,你会看到你的新项目 . 我不得不重新创建父文件夹,但这只是移动文件的问题 . 转到GitHub.com并刷新浏览器,您的新文件也应该在那里 .

    我希望有所帮助 .

  • 0
    Follow below gitbash commands to push the folder files on github repository :-
    1.) $ git init
    2.) $ git cd D:\FileFolderName
    3.) $ git status
    4.) If needed to switch the git branch, use this command : 
        $ git checkout -b DesiredBranch
    5.) $ git add .
    6.) $ git commit -m "added a new folder"
    7.) $ git push -f https://github.com/username/MyTestApp.git TestBranch
        (i.e git push origin branch)
    
  • 0

    综上所述;

    git init
    git status
    git add "*"
    git commit -m "Comment you want"
    git remote add origin  https://link
    git push  -u origin master
    

    我想与您分享一个来源,以便您更轻松地了解Git .

    https://try.github.io/levels/1/challenges/1

  • 3
    git init
    

    在新的本地存储库中添加文件 . 这将它们用于第一次提交 .

    git add .
    

    在本地存储库中添加文件并将其分段以进行提交 . 要取消暂存文件,请使用'git reset HEAD YOUR-FILE' .

    提交您在本地存储库中暂存的文件 .

    git commit -m "First commit"
    # Commits the tracked changes and prepares them to be pushed to a remote
    

    库 . 要删除此提交并修改文件,请使用'git reset --soft HEAD~1'并再次提交并添加文件 . 复制远程存储库URL字段在GitHub存储库的“快速设置”页面的顶部,单击以复制远程存储库URL .

    在命令提示符下,添加将推送本地存储库的远程存储库的URL .

    git remote add origin remote repository URL
    # Sets the new remote
    git remote -v
    # Verifies the new remote URL
    

    将本地存储库中的更改推送到GitHub .

    git push origin master
    # Pushes the changes in your local repository up to the remote repository you
    

    指定为原点

  • 1
    • 从命令行,导航到本地存储库目录 .

    • 在GitHub中创建新的存储库,它将为您提供以 .git 结尾的链接 .

    • 在cmd中运行: git remote add origin [your_GitHub_Repository_link] (记住链接应以 .git 结尾)

    • 然后运行: git push -u origin master

    希望这很有用 .

  • 2

    首先,使用您的项目名称在Github上创建一个新的存储库 . 然后按照以下步骤操作 .

    1)git init
    2)git add *
    3)git commit -m "first commit"
    4)git remote add origin https://github.com/yuvraj777/GDriveDemo.git
    5)git push -u origin master
    
  • 2

    我知道,这是一个老问题,但我试图解释每一步,所以它可能会帮助别人 . 这就是我将现有源添加到git的方法:

    • 在git上创建repo,所以'll have the ssh || https where you'将远程添加源代码 .

    • 在您的终端中,转到项目的路径 .

    • 运行 git init (此处您将项目作为git启动) .

    • 运行 git add * (此处添加项目中的所有文件和文件夹) .

    • 运行 git commit -m "Initial Commit." (这里提交在步骤#4中添加的文件和文件夹;请注意,如果不提交更改,则无法推送更改) .

    • 运行 git remote add origin https://your_username@bitbucket.org/your_username/project-name.git (在这里你添加一个远程项目,你的源将被推送;用步骤#1中的ssh || https替换我的链接) .

    • 运行 git push -u origin master (这里将源推送到git存储库) .

    Note: 这些是将源推送到 master 分支的简单步骤 .

  • 1

    讨厌添加另一个答案,但我的特定情况在这里并没有完全涵盖 . 我有一个当地的仓库,里面有我想保留的变化历史,还有一个在Github上为我创建的非空repo(即使用默认的README.md) . 是的,您可以随时重新创建Github仓库作为空仓库,但在我的情况下,其他人有权创建此特定仓库,如果有一个简单的解决方法,我不想让他烦恼 .

    在这种情况下,在设置远程原点后尝试 git push 时,您将遇到此错误:

    ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'git@github.com:<my repo>.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    如错误所示,我需要在设置远程原点后执行 git pull ,但我需要指定 --allow-unrelated-histories 选项 . 如果没有此选项, git pull 会抱怨 warning: no common commits .

    所以这是对我有用的命令的确切顺序:

    git remote add origin <github repo url>
    cp README.md README.md-save
    git pull origin master --allow-unrelated-histories
    mv README.md-save README.md
    git commit -a
    git push
    
  • 3

    如果要离开命令行,另一个选项是使用SourceTree .

    以下是有关如何设置的一些其他资源:

  • 1

    创建一个新的存储库

    git clone <url>
    cd "repositoryName"
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
    

    现有文件夹

    cd existing_folder
    git init
    git remote add origin <url>
    git add .
    git commit -m "Initial commit"
    git push -u origin master
    

    现有的Git存储库

    cd existing_repo
    git remote rename origin old-origin
    git remote add origin <url>
    git push -u origin --all
    git push -u origin --tags
    

相关问题