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

24

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


是的,你可以通过客户端做到。 - Vivek Kumar Bansal
请查看此链接。这可能会对您有所帮助。 - user3269886
谢谢您的建议,但该命令会删除所有已修改的文件。我只想提交除一个文件以外的所有文件,并且该文件应该有更改。 - sagar.musale
3个回答

50
据我所知,如果不使用Git shell/命令行是无法实现的。在Git Shell中,您有几个选项,它们都会产生略微不同的结果。
  1. If you only want to exclude the file for a little time (maybe one commit) and add it in a future commit you can execute the following commands:

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

    The first command adds all files to the staging area. 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. If the file is already committed to the repository but you only sporadically want to commit further changes of the file, you can use git update-index --assume-unchanged in this way:

    `git update-index --assume-unchanged filename`
    

    If you then change the file locally it will not get added to the staging area when you execute something to add all files, like 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. You can use the .gitignore file if you don't want to track a file at all. 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. Now the file is not added to the staging area when you execute git add .. Remark: If the file is already checked in you have to remove it from the repository to actually start ignoring it. Execute the following to do just that:

    `git rm --cached filename`
    

    The --cached option specifies that the file should only be removed from the index. The local file, whether changed or not, will stay the same. You can add the .gitignore file and commit to make the file get ignored at other machines, with the same repository, as well.

  4. If you want to ignore an untracked file but don't want to share this ignoring with other repository contributors you can put the names of ignored files into the file .git/info/exclude. The .git directory is normally hidden but you can make it visible by altering the folder options. As with the .gitignore file, you have to execute git rm --cached filename if the file was already checked in by a previous commit.

一些注释:

  • filename更改为要排除的文件的实际名称。
  • 除了排除单个文件外,您还可以排除完整的目录。只需将目录名称替换为filename即可。
  • 启动Git Shell的简单方法是启动GitHub for Windows,右键单击要排除/忽略文件的项目,然后选择选项在Git Shell中打开。现在,您位于git存储库的根目录,并且可以开始执行本答案中显示和描述的命令。

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

0

更新:这是“旧方法”。现在有一个命令叫做git reset,你应该使用它,因为它可以避免删除你想要提交的文件的风险。


我会做以下操作:

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
这很危险!checkout 命令会删除你自上次提交以来对文件所做的所有更改,这不是你想要使用的命令,如果你想像 OP 一样“提交除一个文件之外的所有文件”。你应该使用 reset 命令来替代。 - jmm
在2017年我写这篇答案时,git reset还不存在,但感谢jmm指出这一点 - 我会更新我的答案。 - Lirianna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接