【问题标题】:How to use Git for updating my directory and other contents?如何使用 Git 更新我的目录和其他内容?
【发布时间】:2011-12-18 05:06:51
【问题描述】:

我有一个如下目录:

/var/tmp/main
.. plugin1
.... mysource.c
.. plugin2
.... mysource.c
.. plugin3
.... mysource.c

我创建了一个存储库。之后,我应用了以下内容:

$ cd /var/tmp/main
$ git init
$ echo "hello git plz work" >> README
$ git add README
$ git commit -m 'first commit works'
$ git remote add origin git@...../main.git

$ git push origin master

我也试过

$ git add .
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)
#
#   modified:   folder1 (modified content)
#   modified:   folder2 (modified content)
#   modified:   folder3 (modified content)
#   modified:   folder4 (modified content)
#   modified:   folder5 (modified content)
#   modified:   folder6 (modified content, untracked content)
#   modified:   folder7 (modified content)
#   modified:   library (modified content, untracked content)
#
no changes added to commit (use "git add" and/or "git commit -a")

$ git commit -m 'Suggested by Stack experts'
$ git push

也试过 .gitignore

$ cat .gitignore 
*.[oa]
*.pyc
*.gcda
*.gcno
*.la
*.lo
*.loT
*.sw[po]
*.tar.*
*~
.deps
.libs
ABOUT-NLS
INSTALL
Makefile
Makefile.in
aclocal.m4
autom4te.cache
autoregen.sh
compile
config.guess
config.h
config.h.in
config.log
config.rpath
config.status
config.sub
configure
depcomp
install-sh
libtool
ltmain.sh
missing
stamp-h1

到现在都一样……

一切就绪,但 14 小时后我仍然看不到 Git 中的所有目录,我只看到 README 文件显示。

我怎样才能全部提交? (关注http://scottr.org/presentations/git-in-5-minutes/

【问题讨论】:

  • 添加 all 内容,而不是单个文件。 “......添加。”,AFAIR
  • 我这样做没用: git add .
  • @LazyBadger:请参阅上面的更新。
  • @Google 很确定您的问题是:git commit -m 'Suggested by Stack experts' 应该是 git commit -am 'Suggested by Stack experts'(注意添加的 -a 参数)。事实上,您最好删除 -m "Some commit message" 内联提交消息参数并仅使用 git commit -a 直到您更熟悉该过程。

标签: version-control c git linux github


【解决方案1】:

下面是添加空目录的简单方法:

//create some empty, gitignore files.
touch ./someDir/.gitignore
touch ./someOtherDir/.gitignore

...等等..

git add ./someDir/.gitignore
git add ./someOtherDir/.gitignore

...等等...

git commit -am "  Add empty project directories."

这是因为,正如其他答案中提到的,git 会跟踪目录中的文件,但会自行忽略目录。这也可能在最新版本的 git add 机制中得到修复/解决(我依稀记得他们将自己的 .gitignore 文件添加到空目录),所以我真的建议尝试升级到最高的 git 版本为了这个和其他好处,你的发行版可能会默认提供一个旧版本,就像 ubuntu 和 debian 一样。

编辑:

上述技术旨在最大限度地减少对工作目录的干扰,以便跟踪空文件夹。 -在我的示例中,每个目录都会有自己的 .gitignore 文件(您可以拥有任意数量的 .gitignore 文件,它们是附加的)。

所以最后你的文件夹结构应该是这样的:

/var/tmp/main
.. plugin1
.... mysource.c
.... .gitignore
.. plugin2
.... mysource.c
.... .gitignore
.. plugin3
.... mysource.c
.... .gitignore

然后您将添加 .gitignore 文件作为占位符!

但让我们退后一步,尝试相反的策略:

最大可见度。

转到您要添加的任何文件夹,例如plugin1 。在该文件夹中创建一个文件,命名为placeholder

现在从命令行导航到该文件夹​​,例如cd /var/tmp/main/plugin1/git add 占位符文件,例如git add placeholder 。您已经告诉 git 您希望跟踪该文件(如果您键入 git diff,您可以查看“建议的”更改。它会告诉您它看到了该文件,但它只是一个空文件,这很好) .

现在提交文件:git commit placeholder -m " Adding a Placeholder file."

当您添加任何文件时,包含该文件的所有文件夹(一直到主 git 文件夹)也会被添加,因此您现在将在 git 中跟踪 /plugin1/。

检查一下,在您拥有的任何 (?) c 源文件上使用 git add /path/to/file,然后通过 git commit /path/to/file 提交更改。一般来说,任何纯文本的东西,当然最好添加到存储库中。

最后:请注意,git status 仅用于告诉您修改、跟踪的文件,包括新添加的文件。如果没有修改,它只会给你大部分空白输出。

要查看 git 实际跟踪的文件,请使用 git ls-tree HEAD,它只会显示跟踪的文件!

一个干净的开始

这是我通常如何启动 git 存储库的方式。

cd /path/to/project/
git init
echo "README" >> README
git add .
git commit -am "  Initial Commit of readme and files."

【讨论】:

  • 没有您的创建方式。我已经在每个目录下都有该文件。但还是一样。
  • 请注意我的 .gitignore 不是空文件。请参见上文。
  • 对不起,不完全理解您的 cmets 的意思,“我已经在每个目录下都有那个文件。”您可能对我的建议感到困惑,因此我将使用简化的用例进行编辑。
  • 我尝试了你所有的建议。但我还不能把我的目录。还没有上传。
  • Git 有一个本地的内置存储库,以及您想要添加的许多远​​程存储库。如果您从远程存储库中“克隆”,这是常见且有用的,那么该远程将被称为源,您可以“git push”来推送您的更改,并 git pull 来下载更改。
【解决方案2】:

您只添加了您的README 文件。如果您键入git status,您将看到所有其他文件都被视为“未跟踪”。您需要使用git add 添加它们,以便跟踪它们:

git add file1 file2 file3
git commit -m 'Added remaining files to repository'
git push

【讨论】:

  • 我也试过了 $ git add .; git commit -m '是的,我这样做了'; git推送
  • 只是让你知道,据我所知, git commit -m "yes i did this" 是一个毫无意义的命令。为了提交所有添加/提议/修改的更改,您必须将 -a 添加到您的 git 提交中:git commit -a
【解决方案3】:

关于“git add”的重点。是它查看工作树并将所有这些路径添加到分阶段更改中(如果它们已更改或新且未被忽略)

Git 或 Mercurial 都不跟踪空目录

【讨论】:

  • 我没有任何空目录。都有一个源代码文件。
  • git reset ... + git add -A + git status -s 结果在这里
  • 没有帮助我还有我的文件夹只是没有来源gist.github.com/1333646
  • ls -lAR;都有这个“-rwxrwxrwx 1 root root 1830 2011-10-30 08:37 test.c”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多