Git:丢弃一个目录的本地更改?

时间:2013-12-22 18:08:23

标签: git

我可以丢弃一个目录的本地更改吗?

我更改了一组文件,大多数我想提交,但是有一个目录我不想提交任何更改。如何在保持其他变化完整的同时抛弃这些变化?

谢谢!

3 个答案:

答案 0 :(得分:0)

执行提交时,首先必须选择要添加到提交的文件。做一个

git commit -a 

将所有文件添加到提交中。您可以先使用

选择要添加到提交中的文件,而不是这样
git add [file/directory]

您可以通过这种方式逐个指定要添加到提交的文件,或者一起添加目录。您可以git add所需的所有目录。您始终可以使用

进行检查
git status

哪些文件包含在提交中,哪些文件不包含在提交中。当您添加所需的文件并保留其余文件时,可以执行git commit,然后可以对要还原的文件执行git checkout,所有这些都可以将所有文件存储在使用git stash隐藏。

编辑:另一方面,有多个IDE包含了可视化git工具,您可以轻松选择这些工具来选择要提交的文件。

答案 1 :(得分:0)

您可以先添加所有更改,然后执行git reset directory/,然后git commit。这将提交其他所有内容,但不包括提交中的目录。

如果你需要摆脱该目录中的本地修改,你可以在提交后运行git reset --hard

答案 2 :(得分:0)

您可以有选择地选择要提交的内容:

git add -p #(or git add --patch)

来自git手册页的解释

   -p, --patch
       Interactively choose hunks of patch between the index and the work tree and add them to the
       index. This gives the user a chance to review the difference before adding modified contents
       to the index.

       This effectively runs add --interactive, but bypasses the initial command menu and directly
       jumps to the patch subcommand. See "Interactive mode" for details.

如果您不想永久地从该文件夹提交任何内容,可以将其添加到.gitignore文件,git将永久忽略对该文件夹的任何更改,查看示例gitignore文件here

相关问题