git 将现有项目推送到 Github
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17291995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push existing project into Github
提问by MyTitle
I have a folder with my project sources. How I can push this project into Github's repository?
我有一个包含项目源的文件夹。我如何将这个项目推送到 Github 的存储库中?
I tried using this steps:
我尝试使用以下步骤:
- I created empty repository on GitHub.
- I run git-bash and typed
git init
, so inside project root appeared.git
folder. - I added some files to version control using
git add sourcesFolderName
- I committed files added in previous step using
git commit -m "initial commit"
- I specified remote repository using
git remote add MyProject <url>
- Finally
git push
, but nothing is pushed to remote repo... (no authorization failure)
- 我在 GitHub 上创建了空存储库。
- 我运行 git-bash 并输入了
git init
,所以在项目根目录中出现了.git
文件夹。 - 我添加了一些文件到版本控制使用
git add sourcesFolderName
- 我提交了在上一步中使用的文件
git commit -m "initial commit"
- 我使用指定的远程存储库
git remote add MyProject <url>
- 最后
git push
,但没有推送到远程仓库...(没有授权失败)
So how I can push existing sources into newly created github repo?
那么我如何将现有源推送到新创建的 github 存储库中?
回答by Rose Perrone
git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master
The -f
option on git push
forces the push. If you don't use it, you'll see an error like this:
该-f
期权git push
的力量推动。如果你不使用它,你会看到这样的错误:
To [email protected]:roseperrone/project.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]: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.
回答by JGallardo
In less technical terms
用较少的技术术语
My answer is not different but I am adding more information because those that are new could benefit from filling in the gaps in information.
我的答案没有什么不同,但我正在添加更多信息,因为那些新信息可以从填补信息空白中受益。
After you create the repo on github they have instructions. You can follow those. But here are some additional tips because I know how frustrating it is to get started with git.
在 github 上创建 repo 后,他们有说明。你可以跟着那些。但这里有一些额外的提示,因为我知道开始使用 git 是多么令人沮丧。
Let's say that you have already started your project locally. How much you have does not matter. But let's pretend that you have a php project. Let's say that you have the index.php, contact.php and an assets folder with images, css, and fonts. You can do it this way (easy), but there are many options:
假设您已经在本地启动了您的项目。你有多少并不重要。但是让我们假设您有一个 php 项目。假设您有 index.php、contact.php 和一个包含图像、CSS 和字体的资产文件夹。你可以这样做(简单),但有很多选择:
Option 1
选项1
Login to your github account and create the repo.
In the following screen you can copy it down where you need it if you click the button (right side of screen) to "clone in desktop".
在下面的屏幕中,如果您单击按钮(屏幕右侧)“克隆到桌面”,则可以将其复制到您需要的位置。
You can (or do it another way) then copy the contents from your existing project into your new repo. Using the github app, you can just commit from there using their GUI (that means that you just click the buttons in the application). Of course you enter your notes for the commit.
您可以(或以其他方式)然后将现有项目中的内容复制到您的新存储库中。使用 github 应用程序,您可以使用他们的 GUI 从那里提交(这意味着您只需单击应用程序中的按钮)。当然,您为提交输入注释。
Option 2
选项 2
- Create your repo on github as mentioned above.
- On your computer, go to your directory using the terminal. using the linux command line you would cdinto the directory. From here you run the following commands to "connect" your existing project to your repo on github. (This is assuming that you created your repo on github and it is currently empty)
- 如上所述在 github 上创建您的存储库。
- 在您的计算机上,使用终端转到您的目录。使用 linux 命令行,您将cd进入目录。从这里运行以下命令将现有项目“连接”到 github 上的存储库。(这是假设你在 github 上创建了你的 repo 并且它目前是空的)
first do this to initialize git (version control).
首先执行此操作以初始化 git(版本控制)。
git init
then do this to add all your files to be "monitored." If you have files that you want ignored, you need to add a .gitignore
but for the sake of simplicity, just use this example to learn.
然后执行此操作以添加要“监视”的所有文件。如果你有想要忽略的文件,你需要添加一个,.gitignore
但为了简单起见,就用这个例子来学习。
git add .
Then you commit and add a note in between the ""
like "first commit" etc.
然后您提交并在""
“第一次提交”等之间添加注释。
git commit -m "Initial Commit"
Now, here is where you add your existing repo
现在,您可以在此处添加现有的存储库
git remote add github <project url>
But do not literally type <project url>
, but your own project URL. How do you get that? Go to the link where your repo is on github, then copy the link. In my case, one of my repos is https://github.com/JGallardo/urbanhistoricalso my resulting url for this command would just add .gitafter that. So here it would be
但不要直接输入<project url>
,而是输入您自己的项目 URL。你怎么得到的?转到您的 repo 在 github 上的链接,然后复制该链接。就我而言,我的存储库之一是https://github.com/JGallardo/urbanhistorical,因此此命令的结果 url 将在此之后添加.git。所以这里就是
git remote add github https://github.com/JGallardo/urbanhistorical.git
Test to see that it worked by doing
测试看看它是否有效
git remote -v
You should see what your repo is linked to.
您应该看到您的回购链接到什么。
Then you can push your changes to github
然后你可以将你的更改推送到 github
git push github master
or
或者
git push origin master
If you still get an error, you can force it with -f
. But if you are working in a team environment, be careful not to force or you could create more problems.
如果仍然出现错误,您可以使用-f
. 但是,如果您在团队环境中工作,请注意不要强迫,否则可能会造成更多问题。
git push -f origin master
回答by brice
you will need to specify which branchand which remotewhen pushing:
您需要在推送时指定哪个分支和哪个远程:
? git init ./
? git add Readme.md
? git commit -m "Initial Commit"
? git remote add github <project url>
? git push github master
Will work as expected.
将按预期工作。
You can set this up by default by doing:
默认情况下,您可以通过执行以下操作进行设置:
? git branch -u github/master master
which will allow you to do a git push
from master without specifying the remote or branch.
这将允许您在git push
不指定远程或分支的情况下从 master执行。
回答by Tcoz
If you're on a Mac (and this probably works the same on a PC), here's a very easy way to do this. Strangely enough I've looked high and low for this simple process and never found it.
如果您使用的是 Mac(这在 PC 上可能也一样),这是一个非常简单的方法。奇怪的是,对于这个简单的过程,我一直在寻找高低,但从未找到它。
- Do not do anything on Github (other than having an account, and not having used up all your available repos).
- Download GitHub for Mac and install. Go through the account setup, etc. Do NOT create any repositories for your existing project.
- "Add New Local Repository" in repositories.
- Select your existing folder. It'll ask if you want to do that, say yes.
- Once done, you'll see a list of all your files, etc. Commit them.
- Go to Repositories and Publish (this will create the new repo on GitHub for you, if you set up your account properly).
- Go to Repositories and Push (you'll either see the "nothing to push" thing, or it'll push your files/changes to the newly-auto-made repo).
- Wonder why you could not find this simple process anywhere else.
- 不要在 Github 上做任何事情(除了拥有一个帐户,并且没有用完所有可用的存储库)。
- 下载 Mac 版 GitHub 并安装。完成帐户设置等。不要为现有项目创建任何存储库。
- 存储库中的“添加新的本地存储库”。
- 选择您现有的文件夹。它会问你是否想这样做,说是。
- 完成后,您将看到所有文件等的列表。提交它们。
- 转到 Repositories and Publish (这将在 GitHub 上为您创建新的存储库,如果您正确设置了您的帐户)。
- 转到存储库和推送(您将看到“无推送”内容,或者它将您的文件/更改推送到新自动制作的存储库)。
- 想知道为什么您在其他任何地方都找不到这个简单的过程。
I know it is not recommended to use the project folder as the repo folder. I do it all the time, it always works, it makes it simple, and I never have any trouble with it.
我知道不建议使用项目文件夹作为 repo 文件夹。我一直这样做,它总是有效,它使它变得简单,而且我从来没有遇到任何问题。
回答by shaurya uppal
In summary;
总之;
git init
git status
git add "*"
git commit -m "Comment you want"
git remote add origin https://link
git push -u origin master
I would like to share a source with you so that you learn about Git more easily.
我想与您分享一个源代码,以便您更轻松地了解 Git。
回答by Carlos
I will follow the previous comment from Rose P. It took me a long time to find the solution so I'm reposting (hopefully in plain English) what worked for me...
我将遵循 Rose P 之前的评论。我花了很长时间才找到解决方案,所以我重新发布(希望用简单的英语)对我有用的内容......
step 1: Create your new repository on Github.com (skip if you already have one)
第 1 步:在 Github.com 上创建您的新存储库(如果您已经有,请跳过)
step 2: Close XCode...not needed
第 2 步:关闭 XCode...不需要
step 3: Open a new Terminal window (yes, you have to use terminal...I tried all other ways...nothing worked)
第 3 步:打开一个新的终端窗口(是的,您必须使用终端……我尝试了所有其他方法……没有任何效果)
step 4: Using the command cdto find your folder location to your project (the project you want to add to your existing or new repository)
第 4 步:使用命令cd查找项目的文件夹位置(要添加到现有或新存储库的项目)
step 5: type git inityou'll get something like this. Reinitialized existing Git repository in /{current directory}
第 5 步:输入git init你会得到这样的东西。 重新初始化 /{current directory} 中的现有 Git 存储库
step 6: type git add .nothing happens after this step, but type it and go on to the next step.
第 6 步:输入git add 。在这一步之后没有任何反应,但输入它并继续下一步。
step 7: type git commit -m "Initial commit" you'll get the following: # On branch master nothing to commit, working directory clean
第 7 步:输入 git commit -m "Initial commit" 你会得到以下信息:# 在分支 master 上没有什么可提交的,工作目录是干净的
or
或者
some explanation about configuration and then a list of files that have changed.
关于配置的一些解释,然后是已更改的文件列表。
step 8: type git remote add origin {project url}The project url can be found in Github.com. Its the HTTPS clone URL...you should be able to just copy and paste to the terminal window. If the system tell you that origin already exists, create a different name or use your project name (something different)
第八步:输入git remote add origin {project url}项目 url 可以在 Github.com 找到。它是 HTTPS 克隆 URL……您应该能够复制并粘贴到终端窗口。如果系统告诉您来源已经存在,请创建不同的名称或使用您的项目名称(不同的名称)
step 9: go to your GitHub application on your mac and click the "Sync Branch" button (even if there are no pending changes). It takes awhile I think for it to actually commit, but if you go back to your local repository folder you should see your new project. I had to re-create the parent folder, but it's just a matter of moving your files around. Go to GitHub.com and refresh your browser and your new files should also be there.
第 9 步:转到 Mac 上的 GitHub 应用程序并单击“同步分支”按钮(即使没有待处理的更改)。我认为它需要一段时间才能真正提交,但是如果您返回本地存储库文件夹,您应该会看到您的新项目。我不得不重新创建父文件夹,但这只是移动文件的问题。转到 GitHub.com 并刷新浏览器,您的新文件也应该在那里。
I hope that helps.
我希望这有帮助。
回答by Ankita_systematix
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)
回答by Shahid Karimi
git init
Add the files in your new local repository. This stages them for the first commit.
在新的本地存储库中添加文件。这将它们暂存为第一次提交。
git add .
Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
在本地存储库中添加文件并将它们暂存以进行提交。要取消暂存文件,请使用“git reset HEAD YOUR-FILE”。
Commit the files that you've staged in your local repository.
提交您在本地存储库中暂存的文件。
git commit -m "First commit"
# Commits the tracked changes and prepares them to be pushed to a remote
repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again. Copy remote repository URL fieldAt the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL.
存储库。要删除此提交并修改文件,请使用“git reset --soft HEAD~1”并再次提交并添加文件。复制远程存储库 URL 字段在 GitHub 存储库的快速设置页面顶部,单击以复制远程存储库 URL。
In the Command prompt, add the URL for the remote repository where your local repository will be pushed.
在命令提示符中,添加将推送本地存储库的远程存储库的 URL。
git remote add origin remote repository URL
# Sets the new remote
git remote -v
# Verifies the new remote URL
Push the changes in your local repository to GitHub.
将本地存储库中的更改推送到 GitHub。
git push origin master
# Pushes the changes in your local repository up to the remote repository you
specified as the origin
指定为原点
回答by Ali Ezzat Odeh
- From command line, navigate to your local repository directory.
- Create new repository in GitHub, it will provide you with a link ends with
.git
. - in cmd run :
git remote add origin [your_GitHub_Repository_link]
(remember link should end with.git
) - then run :
git push -u origin master
- 从命令行,导航到您的本地存储库目录。
- 在 GitHub 中创建新的存储库,它将为您提供一个以
.git
. - 在 cmd 中运行:(
git remote add origin [your_GitHub_Repository_link]
记住链接应该以 结尾.git
) - 然后运行:
git push -u origin master
Hope this was useful.
希望这是有用的。
回答by Anit Kumar
Create a new repository
创建一个新的存储库
git clone <url>
cd "repositoryName"
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
Existing folder
现有文件夹
cd existing_folder
git init
git remote add origin <url>
git add .
git commit -m "Initial commit"
git push -u origin master
Existing Git repository
现有的 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