简体   繁体   English

如何将现有的非空目录转换为 Git 工作目录并将文件推送到远程存储库

[英]How to convert existing non-empty directory into a Git working directory and push files to a remote repository

  1. I have a non-empty directory (eg /etc/something) with files that cannot be renamed, moved, or deleted.我有一个包含无法重命名、移动或删除的文件的非空目录(例如 /etc/something)。

  2. I want to check this directory into git in place.我想将这个目录检查到 git 中。

  3. 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.我希望能够使用“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 目录并使一切正常吗?

Given you've set up a git daemon on <url> and an empty repository:鉴于您已经在<url>上设置了一个 git 守护进程和一个空的存储库:

cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master

This is how I do.这就是我的做法。 I have added an explanation to understand what the heck is going on.我添加了一个解释来理解到底发生了什么。

Initialize Local Repository初始化本地存储库

  • first, initialize Git with首先,初始化 Git

    git init混帐初始化

  • Add all Files for version control with添加所有文件以进行版本控制

    git add . git 添加。

  • Create a commit with a message of your choice使用您选择的消息创建提交

git commit -m 'AddingBaseCode' git commit -m 'AddingBaseCode'

Initialize Remote Repository初始化远程仓库

  • Create a project on GitHub and copy the URL of your project.在 GitHub 上创建一个项目并复制项目的 URL。 as shown below:如下所示:

在此处输入图片说明

Link Remote repo with Local repo将远程仓库与本地仓库链接

  • Now use copied URL to link your local repo with the remote GitHub repo.现在使用复制的 URL 将您的本地存储库与远程 GitHub 存储库链接起来。 When you clone a repository with git clone, it automatically creates a remote connection called origin pointing back to the cloned repository.当您使用 git clone 克隆存储库时,它会自动创建一个名为origin的远程连接,指向克隆的存储库。 The command remote is used to manage a set of tracked repositories.命令 remote 用于管理一组跟踪的存储库。

    git remote add origin https://github.com/hiteshsahu/Hassium-Word.git git 远程添加原点https://github.com/hiteshsahu/Hassium-Word.git

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.这一步很关键,否则我们将无法在 GitHub 上推送代码。 You must call 'git pull' before pushing your code.在推送代码之前,您必须调用 'git pull'。

    git pull origin master --allow-unrelated-histories git pull origin master --allow-unrelated-history

Commit your code提交你的代码

  • Finally, push all changes on GitHub最后,在 GitHub 上推送所有更改

    git push -u origin master git push -u origin master

Note : Now Github uses "main" as the default branch.注意:现在 Github 使用“main”作为默认分支。 If your project use "main" instead of "master simply replace "master" with "main" from the above commands如果您的项目使用“main”而不是“master”,只需将上述命令中的“master”替换为“main”

Here's my solution:这是我的解决方案:

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master

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:请查看下面的评论,确保您在正确的仓库中!

When is a github repository not empty, like .gitignore and license什么时候 github 仓库不是空的,比如 .gitignore 和 license

Use pull --allow-unrelated-histories and push --force-with-lease使用pull --allow-unrelated-historypush --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

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.假设您的 PC 上有一个名为“ XYZ ”的项目。 Now you want to make a git repo of this project on github and use its functionalities.现在你想在 github 上制作这个项目的 git repo 并使用它的功能。

Step 1 : go to " www.github.com "第一步:到“ www.github.com

Step 2 : create a repository with a " README.md " file (name it as you like it)第 2 步:创建一个带有“ README.md ”文件的存储库( 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.第 4 步:在克隆的文件夹中,您将获得两样东西:“. .git ”文件夹和“ README.md ”文件。 Copy these two in your project folder " XYZ ".将这两个复制到您的项目文件夹“ 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 存储库。

The new official way to do this in 2021. Navigate to directory containing files. 2021 年执行此操作的新官方方法。导航到包含文件的目录。 This assumes there are no files already in the repository.这假设存储库中没有文件。

git init
git add .
git commit -m "initial commit"   
git remote add origin https://<git-userName>@github.com/xyz.gi
git branch -M main    # New
git push -u origin main # New

Sometimes I have to set the upstream by using this command.有时我必须使用此命令设置上游。

git branch --set-upstream-to=origin/main main

And then force the push with this command.然后用这个命令强制推送。

git push -u origin main --force

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 来制作和打开现有的存储库。

Here's my solution if you created the repository with some default readme file or license如果您使用一些默认readme filelicense创建存储库,这是我的解决方案

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> 

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

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