git 如何克隆到非空目录?

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

How do I clone into a non-empty directory?

git

提问by Dale Forester

I have directory A with files matching directory B. Directory A may have other needed files. Directory B is a git repo.

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

I want to clone directory B to directory A but git-clone won't allow me to since the directory is non-empty.

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

I was hoping it would just clone .git and since all the files match I could go from there?

我希望它只是克隆 .git 并且因为所有文件都匹配我可以从那里去?

I can't clone into an empty directory because I have files in directory A that are not in directory B and I want to keep them.

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

Copying .git is not an option since I want refs to push/pull with and I don't want to set them up manually.

复制 .git 不是一个选项,因为我希望引用推/拉,我不想手动设置它们。

Is there any way to do this?

有没有办法做到这一点?

Update: I think this works, can anyone see any problems? -->

更新:我认为这有效,有人能看到任何问题吗?-->

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

采纳答案by Dale Forester

In the following shell commands existing-diris a directory whose contents match the tracked files in the repo-to-clonegit repository.

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

# 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

回答by cmcginty

This worked for me:

这对我有用:

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

NOTE:-twill set the upstream branch for you, if that is what you want, and it usually is.

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

回答by mohsaied

A slight modification to one of the answers that worked for me:

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

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

to start working on the master branch straight away.

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

回答by JohnFF

Warning - this could potentially overwrite files.

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

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

Modified from @cmcginty's answer- without the -f it didn't work for me

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

回答by BjornSnoen

Here's what I ended up doing when I had the same problem (at least I think it's the same problem). I went into directory A and ran git init.

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

Since I didn't want the files in directory A to be followed by git, I edited .gitignore and added the existing files to it. After this I ran git remote add origin '<url>' && git pull origin masteret voíla, B is "cloned" into A without a single hiccup.

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

回答by KuttKatrea

I have used this a few moments ago, requires the least potentially destructive commands:

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

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

And voilá!

瞧!

回答by Mike6679

This worked for me:

这对我有用:

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

回答by Ken Williams

Another simple recipe seems to work well for me:

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

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

My main use case for checking out to a directory with existing files is to control my Unix dotfiles with Git. On a new account, the home directory will already have some files in it, possibly even the ones I want to get from Git.

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

回答by Vlado

I had a similar problem with a new Apache web directory (account created with WHM) that I planned to use as a staging web server. I needed to initially clone my new project with the code base there and periodically deploy changes by pulling from repository.

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

The problem was that the account already contained web server files like:

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

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

...that I did not want to either delete or commit to my repository. I needed them to just stay there unstaged and untracked.

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

What I did:

我做了什么:

I went to my web folder (existing_folder):

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

cd /home/existing_folder

and then:

进而:

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

It displayed (as expected) a list of many not staged files - those that already existed initially from my cPanel web account.

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

Then, thanks to this article, I just added the list of those files to:

然后,多亏了这篇文章,我才将这些文件的列表添加到:

**.git/info/exclude**

This file, almost like the .gitignorefile, allows you to ignore files from being staged. After this I had nothing to commit in the .git/ directory - it works like a personal .gitignorethat no one else can see.

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

Now checking git statusreturns:

现在检查git status返回:

On branch master
nothing to commit, working tree clean

Now I can deploy changes to this web server by simply pulling from my git repository. Hope this helps some web developers to easily create a staging server.

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

回答by Philip Kirkbride

Here is what I'm doing:

这是我在做什么:

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