如何将本地未版本化代码关联到 git 远程存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13052172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:40:34  来源:igfitidea点击:

How can I associate local unversioned code to git remote repository?

gitversion-controlrepositorygit-remote

提问by bato

I need to associate a clean unversioned code to an existing git remote repository.

我需要将干净的未版本控制代码与现有的 git 远程存储库相关联。

I explain better my situation. My project is moved from svn to git. I have a svn local working copy with old code (nothing new to commit) that has to be syncronized with the new git repository. I could clone the repo but I should restore many ignored configuration files by hand and I would avoid it.

我更好地解释了我的情况。我的项目从 svn 移动到 git。我有一个 svn 本地工作副本,其中包含必须与新的 git 存储库同步的旧代码(无需提交任何新代码)。我可以克隆 repo,但我应该手动恢复许多被忽略的配置文件,我会避免它。

So I removed all .svn directories and I would track my code with the git remote repository.

因此,我删除了所有 .svn 目录,并使用 git 远程存储库跟踪我的代码。

Is there a way to do it?

有没有办法做到这一点?

采纳答案by bato

Ok, I post the right answer for my question based on Adam Dymitrukand Eugene Sajineanswers. Thanks everyone for the support :)

好的,我根据Adam DymitrukEugene Sajine 的回答发布了我的问题的正确答案。谢谢大家的支持:)

  • First of all, remove all .svndirectories in working copy directory;
  • initialize new git repository in the working copy directory

    cd /path/to/my/project
    git init
    
  • add git remote repository and fetch it

    git remote add origin url-to-your-remote
    git fetch origin
    
  • reset working copy to remote repository

    git reset --hard origin/master
    
  • 首先,删除.svn工作副本目录中的所有目录;
  • 在工作副本目录中初始化新的 git 存储库

    cd /path/to/my/project
    git init
    
  • 添加git远程存储库并获取它

    git remote add origin url-to-your-remote
    git fetch origin
    
  • 将工作副本重置到远程存储库

    git reset --hard origin/master
    

At the end local repository will be synchronized with remote repository.

最后,本地存储库将与远程存储库同步。

回答by Adam Dymitruk

I would commit your working directory to ensure that it exists in history before fetching. (ensure you are in the projects root folder)

我会提交您的工作目录以确保它在获取之前存在于历史记录中。(确保您在项目根文件夹中)

git init
git add -A
git commit -m "my latest'

now we have it

现在我们有了

git remote add origin url-to-your-remote
git fetch origin

now we have the history from the remote

现在我们有了远程的历史

git reset origin/master

now our current commit is the latest from the remote

现在我们当前的提交是来自远程的最新提交

git add -A
git commit -m "Committed my state"

now we use our current working directory as the snapshot for the next commit.

现在我们使用当前工作目录作为下一次提交的快照。

git push -u origin master

push up your changes and track the branch.

推送您的更改并跟踪分支。

the key is the resetcommand with no --hardoption. It keeps your working folder the same while pointing the branch to what you got from the remote.

关键是reset没有--hard选项的命令。它使您的工作文件夹保持不变,同时将分支指向您从远程获得的内容。

回答by Eugene Sajine

If you really mean what you're saying then the sequence will be:

如果你真的是你所说的,那么顺序将是:

cd {your-project}
git init
git remote add {remote_name} {address}
git fetch {remote_name}

Then you can create a local branch from one of the branches that the remote has for example:

然后,您可以从远程拥有的分支之一创建本地分支,例如:

git branch master {remote_name}/master

and then you can add your code:

然后你可以添加你的代码:

git add {files}

or

或者

git add .
git commit

UPDATE:

更新:

The OP specifies that the code in question is old and there is nothing to commit, therefore it seems that the whole situation is overcomplicated for no reason. OP just should clone the new repository and be done with it. Ignore configurations should be redone in accordance to the project policies (either .gitingore is stored in the repo or not and etc.)

OP 指定有问题的代码是旧的,没有什么可提交的,因此整个情况似乎无缘无故地过于复杂。OP 应该克隆新的存储库并完成它。忽略配置应根据项目策略重做(.gitingore 是否存储在 repo 中等)

回答by erase.ego

Would the following work? I tried it on one of my projects and it worked.

以下会起作用吗?我在我的一个项目中尝试了它,它奏效了。

1) Clone the git repository to a location different from the directory that contains the unversioned code.

1) 将 git 存储库克隆到与包含未版本化代码的目录不同的位置。

2) Copy the .git folder from the directory created as a result of (1) to the directory that contains the unversioned code.

2) 将 .git 文件夹从作为 (1) 的结果创建的目录复制到包含未版本化代码的目录。

This effectively achieved for me what Adam Dymitruk steps in his answer above.

这对我来说有效地实现了 Adam Dymitruk 在上面的回答中的步骤。