我有一个与远程文件夹完全相同的本地文件夹,其中包含一些我后来包含的文件(示例中为test2.txt)。问题是我从其中删除了.git文件夹。所以我只想再次添加git。
示例:
本地
folder1/
folder1/test.txt
folder1/test2.txt
远程
folder1/
folder1/test.txt
git init
git add ./
git commit -m "merge"
git pull origin master --allow-unrelated-histories
我添加--allow-unrelated-histories
,因为它给了我错误fatal: refusing to merge unrelated histories
到此为止都是一团糟...
我建议以下内容:
将遥控器克隆到新文件夹中
git clone [url of repo here] new
在这个新克隆中,checkout
是正确的目标分支
cd new
git checkout some/branch
现在将.git/
文件夹从new/
移至您的工作目录
mv new/.git workdir
您应该处于期望的状态。
检查git status
,git diff
,git add file2.txt
等是否按预期工作。
您可以删除(现在无用的)new/
目录。
# Create empty .git/ directory.
git init
# Fetch the remote repository.
git remote add origin <upstream-url>
git fetch
# Get our HEAD index to match origin/master without altering the working tree.
git symbolic-ref HEAD refs/remotes/origin/master
git read-tree HEAD
# Make this the master branch and track upstream origin/master.
git checkout -t -b master