繁体   English   中英

如何克隆到非空目录?

[英]How do I clone into a non-empty directory?

我有目录 A 与目录 B 匹配的文件。目录 A 可能有其他需要的文件。 目录 B 是一个 git repo。

我想将目录 B 克隆到目录 A 但 git-clone 不允许我这样做,因为该目录是非空的。

我希望它只会克隆 .git 并且由于所有文件都匹配,我可以从那里去吗?

我无法克隆到空目录,因为我在目录 A 中有文件不在目录 B 中,我想保留它们。

复制 .git 不是一种选择,因为我希望 refs 可以推/拉,我不想手动设置它们。

有没有办法做到这一点?

更新:我认为这可行,任何人都可以看到任何问题吗? -->

cd a
git clone --no-hardlinks --no-checkout ../b a.tmp 
mv a.tmp/.git .
rm -rf a.tmp
git unstage # apparently git thinks all the files are deleted if you don't do this

这对我有用:

git init
git remote add origin PATH/TO/REPO
git fetch
git reset origin/master  # Required when the versioned files existed in path before "git init" of this repo.
git checkout -t origin/master

注意: -t将为您设置上游分支,如果这是您想要的,而且通常是这样。

在以下 shell 命令中, existing-dir是一个目录,其内容与repo-to-clone git 存储库中的跟踪文件匹配。

# Clone just the repository's .git folder (excluding files as they are already in
# `existing-dir`) into an empty temporary directory
git clone --no-checkout repo-to-clone existing-dir/existing-dir.tmp # might want --no-hardlinks for cloning local repo

# Move the .git folder to the directory with the files.
# This makes `existing-dir` a git repo.
mv existing-dir/existing-dir.tmp/.git existing-dir/

# Delete the temporary directory
rmdir existing-dir/existing-dir.tmp
cd existing-dir

# git thinks all files are deleted, this reverts the state of the repo to HEAD.
# WARNING: any local changes to the files will be lost.
git reset --hard HEAD

对对我有用的答案之一稍作修改:

git init
git remote add origin PATH/TO/REPO
git pull origin master

立即开始在 master 分支上工作。

警告 - 这可能会覆盖文件。

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

@cmcginty 的答案修改 - 没有 -f 它对我不起作用

当我遇到同样的问题时,这就是我最终做的事情(至少我认为这是同样的问题)。 我进入目录 A 并运行git init

由于我不希望目录 A 中的文件后跟 git,因此我编辑了 .gitignore 并将现有文件添加到其中。 在此之后,我运行git remote add origin '<url>' && git pull origin master et voíla,B 被“克隆”到 A 中,没有任何问题。

另一个简单的食谱似乎对我很有效:

git clone --bare $URL .git
git config --unset core.bare

我签出包含现有文件的目录的主要用例是使用 Git 控制我的 Unix 点文件。 在一个新帐户上,主目录中已经有一些文件,甚至可能是我想从 Git 获取的文件。

我刚才使用过这个,需要最少潜在破坏性的命令:

cd existing-dir
git clone --bare repo-to-clone .git
git config --unset core.bare
git remote rm origin
git remote add origin repo-to-clone
git reset

瞧!

这对我有用:

cd existing_folder
git init
git remote add origin path_to_your_repo.git
git add .
git commit
git push -u origin master

我计划用作登台 Web 服务器的新 Apache Web 目录(使用 WHM 创建的帐户)也有类似的问题。 我最初需要用那里的代码库克隆我的新项目,并通过从存储库中提取来定期部署更改。

问题是该帐户已经包含 Web 服务器文件,例如:

.bash_history
.bash_logout
.bash_profile
.bashrc
.contactemail
.cpanel/
...

...我不想删除或提交到我的存储库。 我需要他们呆在那里不上演和不跟踪。

我做了什么:

我去了我的网络文件夹(existing_folder):

cd /home/existing_folder

接着:

git init
git remote add origin PATH/TO/REPO
git pull origin master
git status

它显示(如预期的那样)许多未暂存文件的列表 - 那些最初从我的 cPanel 网络帐户中已经存在的文件。

然后,感谢这篇文章,我刚刚将这些文件的列表添加到:

**.git/info/exclude**

该文件与.gitignore文件几乎一样,允许您忽略暂存的文件。 在此之后,我在 .git/ 目录中没有什么可提交的了——它就像一个其他人无法看到的个人.gitignore一样工作。

现在检查git status返回:

On branch master
nothing to commit, working tree clean

现在我可以通过简单地从我的 git 存储库中拉取更改来部署对这个 Web 服务器的更改。 希望这可以帮助一些 Web 开发人员轻松创建登台服务器。

以下对我有用。 首先,我要确保a目录中的文件是源代码控制的:

$ cd a
$ git init
$ git add .
$ git commit -m "..."

然后

$ git remote add origin https://URL/TO/REPO
$ git pull origin master --allow-unrelated-histories
$ git push origin master

也许我误解了您的问题,但是如果您将文件从 A 复制/移动到 git repo B 并使用git add添加所需的文件,这不是更简单吗?

更新:来自 git 文档:

仅当目录为空时才允许克隆到现有目录。

来源:http: //git-scm.com/docs/git-clone

这是我正在做的事情:

git clone repo /tmp/folder
cp -rf /tmp/folder/.git /dest/folder/
cd /dest/folder
git checkout -f master

这对我有用,但你应该将远程存储库文件合并到本地文件:

git init
git remote add origin url-to-git
git branch --set-upstream-to=origin/master master
git fetch
git status

我一直在寻找类似的东西,这就是我想出的:

我的情况是我有一个活动的网络树,我试图为它创建一个远程存储库,而不移动当前网络树中的任何文件。 这是我所做的:

  1. 转到 web 树并运行git init
  2. 转到存储库的预期位置并运行: git clone --bare /path/to/web/repo
  3. 在我的远程仓库中编辑配置文件并删除[remote "origin"]部分。
  4. 在 web 树中的 .git/config 中添加一个[remote "origin"]部分,指向新的远程仓库。

我喜欢戴尔的回答,我还添加了

git clone --depth 2 --no-checkout repo-to-clone existing-dir/existing-dir.tmp
git branch dev_new214
git checkout dev_new214
git add .
git commit
git checkout dev
git merge dev_new214

较浅的深度避免了很多额外的早期开发提交。 新分支为我们提供了一个很好的视觉历史,其中放置了来自该服务器的一些新代码。在我看来,这是完美的使用分支。 我感谢所有在这里发帖的人的深刻见解。

我在尝试克隆到 c/code 时遇到了同样的问题

但是这个文件夹包含一大堆项目。

我在 c/code/newproject 中创建了一个新文件夹,并将我的克隆映射到该文件夹​​。

git for desktop 然后询问我的用户,然后克隆得很好。

当我偶然发现这个问题时,我正在寻找一个不同的问题:如何克隆到文件不存在的现有目录中?

无论如何,我最近在几台服务器上为一些非源代码控制的文件副本做了这个。

cd <target directory>
git init
git remote add origin <repository URI>
git fetch
git branch -f master origin/master
git reset
git show HEAD:.gitignore > .gitignore

这个:

  • 为您初始化一个空的 repo 目录
  • 为您打算连接到的存储库添加远程
  • 检索存储库文件夹内容 (/.git)
  • 强制git init的默认分支跟踪 origin/master
  • 取消 fetch 创建的所有更改(我不完全了解在fetch上暂存所有文件的机制)
  • 存储库的 .gitignore 文件中的副本(如果您要使用它,您将需要它)

对于我的问题,答案是 Dale Forester 回答中的git reset --hard HEAD

任意分支和远程名称的替代指令

git init
git remote add <remotename> <repository URI>
git checkout -b <localbranchname>
git fetch <remotename> <remotebranchname>
git branch -f <localbranchname> <remotename>/<remotebranchname>
git reset
git show HEAD:.gitignore > .gitignore

如上所述,这样做:

  • 为您初始化一个空的 repo 目录
  • 为您打算连接到的存储库添加远程
  • 设置本地分支名称。 可选,但如果您在同一环境中执行多个分支,建议您保持分支笔直
  • 检索存储库文件夹内容 (/.git)
  • git checkout -b <localbranchname>强制本地分支跟踪<remote>/<remotebranchname>
  • 取消 fetch 创建的所有更改(我不完全了解在fetch上暂存所有文件的机制)
  • 存储库的 .gitignore 文件中的副本(如果您要使用它,您将需要它)

如果您在此结束时执行 git status,您将看到当前目录(工作目录)中任何文件的未暂存更改,无论工作目录与存储库结构是否有任何关系。

这个问题有很多答案,我认为其中没有一个涵盖将 A 初始化为 Git 存储库然后从 B 拉到 A 的选项。

# Turn `A` into a Git repo and check in files
git -C A init
git -C A add --all
git -C A commit -m "Add existing files"

# Set `B` as a remote
git -C A remote add local-B file:///B

# Fetch & merge B 'master' to A 'master'
git -C A pull --allow-unrelated local-B master

暂无
暂无

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

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