如何在GitHub for Windows中提交除一个文件之外的所有文件

问题描述 投票:16回答:3

我想提交除一个文件以外的所有文件。我可以使用GitHub for Windows客户端执行此操作,还是需要使用命令行?如果我需要使用命令行,我该如何使用它?

git github-for-windows
3个回答
33
投票

据我所知,如果不使用git shell /命令行,这是不可能的。在Git Shell中,您有几种选择。所有这些都会产生略微不同的结果。

  1. 如果您只想将文件排除一段时间(可能是一次提交)并在将来的提交中添加它,则可以执行以下命令: git add . git reset filename git commit -m "commit message" 第一个命令将所有文件添加到暂存区域。 Then the second command removes the one file from the staging area. This means the file is not added in the commit but the changes to it are preserved on your local drive.
  2. 如果文件已提交到存储库但您只是偶尔想要提交文件的进一步更改,则可以通过以下方式使用git update-index --assume-unchanged`git update-index --assume-unchanged filename` 如果您在本地更改文件,则在执行添加所有文件(如git add .)的操作时,它将不会添加到暂存区域。 When, after some time, you want to commit changes to the file again, you can run git update-index --no-assume-unchanged filename to stop ignoring the changes.
  3. 如果您根本不想跟踪文件,可以使用.gitignore文件。 To ignore a file named filename you create, or edit, a file called .gitignore. Put filename on a line of its own to ignore that file.现在执行git add .时文件未添加到临时区域。备注:如果文件已经签入,则必须将其从存储库中删除才能真正开始忽略它。执行以下操作即可: `git rm --cached filename` The --cached option specifies that the file should only be removed from the index.本地文件,无论是否改变,都将保持不变。您可以添加.gitignore文件并提交以使其在具有相同存储库的其他计算机上被忽略。
  4. 如果要忽略未跟踪的文件但不想与其他存储库贡献者共享此忽略,则可以将忽略文件的名称放入文件.git/info/exclude中。 .git目录通常是隐藏的,但您可以通过更改文件夹选项使其可见。与.gitignore文件一样,如果文件已由先前的提交签入,则必须执行git rm --cached filename

一些说明:

  • filename更改为要排除的文件的实际名称。
  • 您也可以排除完整目录,而不是排除单个文件。你可以通过在filename的地方替换目录名来做到这一点。
  • 启动Git Shell的一种简单方法是启动GitHub for Windows,右键单击要排除/忽略文件的项目,然后选择Open in Git Shell选项。现在,您位于git存储库的根目录下,您可以开始执行此答案中显示和描述的命令。

1
投票

我会做以下事情:

git add .
git checkout filename
git commit -m "commit message"

第一行添加列表中的所有文件,第二行删除您指定的文件,其中“filename”应该是您在提交时不想要的文件的名称。

要么

git checkout filename
git commit -am "commit message"

git commit -am添加文件并同时发出提交消息。

如果您需要删除您在上一个文件中所做的更改:

git stash -u

1
投票

您只需创建一个.gitignore文件,并添加要在其中排除的文件的名称。

© www.soinside.com 2019 - 2024. All rights reserved.