如何将现有的非空目录转换为 Git 工作目录并将文件推送到远程存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3311774/
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
How to convert existing non-empty directory into a Git working directory and push files to a remote repository
提问by HMW
I have a non-empty directory (eg /etc/something) with files that cannot be renamed, moved, or deleted.
I want to check this directory into git in place.
I want to be able to push the state of this repository to a remote repository (on another machine) using "git push" or something similar.
我有一个包含无法重命名、移动或删除的文件的非空目录(例如 /etc/something)。
我想将这个目录检查到 git 中。
我希望能够使用“git push”或类似的东西将此存储库的状态推送到远程存储库(在另一台机器上)。
This is trivial using Subversion (currently we do it using Subversion) using:
使用 Subversion(目前我们使用 Subversion 来完成)使用以下方法是微不足道的:
svn mkdir <url> -m <msg>
cd <localdir>
svn co <url> .
svn add <files etc>
svn commit -m <msg>
What is the git equivalent?
什么是 git 等价物?
Can I "git clone" into an empty directory and simply move the .git directory and have everything work?
我可以“git clone”到一个空目录中并简单地移动 .git 目录并让一切正常吗?
回答by abyx
Given you've set up a git daemon on <url>
and an empty repository:
鉴于您已经设置了一个 git 守护进程<url>
和一个空的存储库:
cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master
回答by Hitesh Sahu
This is how I do. I have added explanation to understand what the heck is going on.
这就是我的做法。我添加了解释以了解到底发生了什么。
Initialize Local Repository
初始化本地存储库
first initialize Git with
git init
Add all Files for version control with
git add .
Create a commit with message of your choice
git commit -m 'AddingBaseCode'
首先初始化 Git
混帐初始化
添加所有文件以进行版本控制
git 添加。
使用您选择的消息创建提交
git commit -m 'AddingBaseCode'
Initialize Remote Repository
初始化远程仓库
Link Remote repo with Local repo
将远程仓库与本地仓库链接
Now use copied URL to link your local repo with remote GitHub repo. When you clone a repository with git clone, it automatically creates a remote connection called originpointing back to the cloned repository. The command remote is used to manage set of tracked repositories.
git remote add origin https://github.com/hiteshsahu/Hassium-Word.git
现在使用复制的 URL 将您的本地存储库与远程 GitHub 存储库链接起来。当您使用 git clone 克隆存储库时,它会自动创建一个名为origin的远程连接,指向克隆的存储库。命令 remote 用于管理一组跟踪的存储库。
Synchronize
同步
Now we need to merge local code with remote code. This step is critical otherwise we won't be able to push code on GitHub. You must call 'git pull' before pushing your code.
git pull origin master --allow-unrelated-histories
现在我们需要将本地代码与远程代码合并。这一步很关键,否则我们将无法在 GitHub 上推送代码。在推送代码之前,您必须调用 'git pull'。
git pull origin master --allow-unrelated-history
Commit your code
提交你的代码
Finally push all changes on GitHub
git push -u origin master
最后在 GitHub 上推送所有更改
git push -u origin master
回答by csomakk
Here's my solution:
这是我的解决方案:
git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master
回答by Romeo Kienzler
In case the remote repository is not empty (this is the case if you are using IBM DevOps on hub.jazz.net) then you need to use the following sequence:
如果远程存储库不为空(如果您在 hub.jazz.net 上使用 IBM DevOps 就是这种情况),那么您需要使用以下序列:
cd <localDir>
git init
git add -A .
git pull <url> master
git commit -m "message"
git remote add origin <url>
git push
EDIT 30th Jan 17: Please see comments below, make sure you are on the correct repo!
编辑 1 月 30 日 17:请查看下面的评论,确保您在正确的仓库中!
回答by Sebastian Oscar Lopez
When is a github repository not empty, like .gitignore and license
什么时候 github 仓库不是空的,比如 .gitignore 和 license
Use pull --allow-unrelated-historiesand push --force-with-lease
使用pull --allow-unrelated-history和push --force-with-lease
Use commands
使用命令
git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/...
git pull origin master --allow-unrelated-histories
git push --force-with-lease
回答by Genius
The simplest way of doing this which I find useful is the below.
我认为有用的最简单的方法如下。
This might not be the official way but it works well.
这可能不是官方方式,但效果很好。
Suppose you have a project named "XYZ
" on your PC.
Now you want to make a git repo of this project on github and use its functionalities.
假设您XYZ
的 PC 上有一个名为“ ”的项目。现在你想在 github 上制作这个项目的 git repo 并使用它的功能。
Step 1: go to "www.github.com
"
第 1 步:转到“ www.github.com
”
Step 2: create a repository with a "README.md
" file (name it as you like it)
第 2 步:创建一个带有“ README.md
”文件的存储库(随意命名)
Step 3: clone the repository to your PC.
第 3 步:将存储库克隆到您的 PC。
Step 4: In the cloned folder you will get two things : ".git
" folder and a "README.md
" file. Copy these two in your project folder "XYZ
".
第 4 步:在克隆的文件夹中,您将获得两样东西:“ .git
”文件夹和“ README.md
”文件。将这两个复制到您的项目文件夹“ XYZ
”中。
Step 5: Delete the cloned repo.
第 5 步:删除克隆的 repo。
Step 6: add, commit and push all the files and folders of your project.
第 6 步:添加、提交和推送项目的所有文件和文件夹。
Now, you can just use your project "XYZ
" as a git repository.
现在,您可以将项目“ XYZ
”用作 git 存储库。
回答by Harish Karthick
Here's my solution if you created the repository with some default readme file
or license
这是我的解决方案,如果您使用一些默认值readme file
或license
git init
git add -A
git commit -m "initial commit"
git remote add origin https://<git-userName>@github.com/xyz.git //Add your username so it will avoid asking username each time before you push your code
git fetch
git pull https://github.com/xyz.git <branch>
git push origin <branch>
回答by tpage
I had a similar problem. I created a new repository, NOT IN THE DIRECTORY THAT I WANTED TO MAKE A REPOSITORY. I then copied the files created to the directory I wanted to make a repository. Then open an existing repository using the directory I just copied the files to.
我有一个类似的问题。我创建了一个新的存储库,而不是在我想要创建存储库的目录中。然后我将创建的文件复制到我想要创建存储库的目录中。然后使用我刚刚将文件复制到的目录打开现有存储库。
NOTE:I did use github desktop to make and open exiting repository.
注意:我确实使用 github desktop 来制作和打开现有的存储库。