簡體   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