如何使用 git 从另一个目录添加文件?

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

How do I add files from another directory with git?

gitgithub

提问by OutOFTouch

I created a repo directly on GitHub using by browser, than using git bash cloned the repo to my machine, I was able to do a test commit with bash using the readme file.

我使用浏览器直接在 GitHub 上创建了一个存储库,而不是使用 git bash 将存储库克隆到我的机器上,我能够使用自述文件对 bash 进行测试提交。

I keep getting fatal: This operation must be run in a work tree when running git --work-tree=yourSrcFolder add .

我不断变得致命:运行 git --work-tree=yourSrcFolder add 时,必须在工作树中运行此操作。

Was looking hereand herefor some clue but after 4 hours ready to give up on ever using Git.

这里这里寻找一些线索,但在 4 小时后准备放弃使用 Git。

If you look at the first link I put in this quesion the answer with 4 upvotes, I was trying to do exactly what he said: "That way, you keep separate your initial source folder, from your Git working tree.", which is why I was trying to use --work-tree=yourSrcFolder add, So I hope that gives the downvoters some reasoning behind my motives.

如果你看我在这个问题中给出的第一个链接,得到了 4 个赞成票,我试图完全按照他说的去做:“那样,你将你的初始源文件夹与你的 Git 工作树分开。”,这是为什么我试图使用 --work-tree=yourSrcFolder add,所以我希望这能给投票者一些我动机背后的推理。

采纳答案by Gabriele Petronella

Simply move any file you wish to add into the repo directory, then add them to the git tracking.

只需将您想要添加的任何文件移动到 repo 目录中,然后将它们添加到 git 跟踪中。

Example: suppose you want to add a file named test.txtto your git repo.

示例:假设您想将一个名为的文件添加test.txt到您的 git 存储库中。

  1. Move it into your repository directory
  2. run git add test.txt
  3. commit the file using git commit -m "Added test file to the repo"
  4. push your modification to the remote server git push
  1. 将其移动到您的存储库目录中
  2. git add test.txt
  3. 使用提交文件 git commit -m "Added test file to the repo"
  4. 将您的修改推送到远程服务器 git push

回答by Andrii Abramov

My solution was to create hard link to file that is placed in another directory.

我的解决方案是创建指向放置在另一个目录中的文件的硬链接。

You can use lncommand (mklinkon Windows) to achieve this (assume that you are currently in repository directory):

您可以使用ln命令(mklink在 Windows 上)来实现此目的(假设您当前位于存储库目录中):

# this will add file with the same name to current directory and same inode
ln ../../filename . 

And then just add it to git index:

然后将其添加到 git index 中:

git add filename

And commit it if you need:

如果需要,请提交:

git commit -m "add file from another directory"

See inode, ln.

inode, ln