git 如何在 GitHub for Windows 中提交除一个之外的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26647359/
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
How to commit all file except one in GitHub for Windows
提问by sagar.musale
I want to commit all files except for one file. Can I do that using the GitHub for Windows client or do I need to use the command line? If I need to use the command line, how do I use it?
我想提交除一个文件之外的所有文件。我可以使用 GitHub for Windows 客户端执行此操作还是需要使用命令行?如果我需要使用命令行,我该如何使用它?
回答by dennisschagt
As far as I know this is not possible without using the git shell/command line. In the Git Shell you have several options. All of them yield a slightly different result.
据我所知,如果不使用 git shell/命令行,这是不可能的。在 Git Shell 中,您有多种选择。所有这些都会产生略有不同的结果。
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.
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 rungit update-index --no-assume-unchanged filename
to stop ignoring the changes.You can use the
.gitignore
file if you don't want to track a file at all. To ignore a file namedfilename
you create, or edit, a file called.gitignore
. Putfilename
on a line of its own to ignore that file.Now the file is not added to the staging area when you executegit 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.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 executegit rm --cached filename
if the file was already checked in by a previous commit.
如果您只想排除该文件一小段时间(可能是一次提交)并将其添加到将来的提交中,您可以执行以下命令:
git add . git reset filename git commit -m "commit message"
第一个命令将所有文件添加到暂存区。然后第二个命令从暂存区中删除一个文件。这意味着该文件不会添加到提交中,但对其所做的更改会保留在您的本地驱动器上。
如果文件已经提交到存储库,但您只是偶尔想提交文件的进一步更改,则可以通过
git update-index --assume-unchanged
以下方式使用:`git update-index --assume-unchanged filename`
如果您随后在本地更改文件,则在您执行某些操作以添加所有文件(例如
git add .
. 一段时间后,当您想再次提交对文件的更改时,您可以运行git update-index --no-assume-unchanged filename
以停止忽略更改。.gitignore
如果您根本不想跟踪文件,则可以使用该文件。要忽略名为filename
您创建或编辑的文件,名为.gitignore
. 单独放置filename
一行以忽略该文件。现在,当您执行git add .
. 备注:如果文件已经签入,您必须将其从存储库中删除才能真正开始忽略它。执行以下操作来做到这一点:`git rm --cached filename`
该
--cached
选项指定只应从索引中删除文件。本地文件,无论是否更改,都将保持不变。您可以添加.gitignore
文件并提交以使文件在具有相同存储库的其他机器上也被忽略。如果您想忽略未跟踪的文件但不想与其他存储库贡献者共享此忽略,您可以将被忽略文件的名称放入文件中
.git/info/exclude
。该.git
目录通常是隐藏的,但您可以通过更改文件夹选项使其可见。与.gitignore
文件一样,git rm --cached filename
如果文件已经被先前的提交签入,则必须执行。
Some notes:
一些注意事项:
- Change
filename
into the actual name of the file you want to exclude. - Instead of excluding a single file you can also exclude a complete directory. You can do that by simply substituting the directory name in the place of
filename
. - An easy way of starting a Git Shell is by starting GitHub for Windows, right clicking on the project in which you want to exclude/ignore the file and selecting the option
Open in Git Shell
. Now you are at the root of the git repository and you can start executing the commands that are shown and described in this answer.
- 更改
filename
为要排除的文件的实际名称。 - 除了排除单个文件,您还可以排除整个目录。您可以通过简单地将目录名称替换为
filename
. - 启动 Git Shell 的一种简单方法是启动 GitHub for Windows,右键单击要排除/忽略文件的项目并选择选项
Open in Git Shell
。现在您位于 git 存储库的根目录,您可以开始执行本答案中显示和描述的命令。
回答by Lirianna
I would do the following:
我会做以下事情:
git add .
git checkout filename
git commit -m "commit message"
The first line adds all files on the list, and the second line removes the file you specify, where "filename" should be the name of the file you don't want in your commit.
第一行添加列表中的所有文件,第二行删除您指定的文件,其中“filename”应该是您不想要提交的文件的名称。
Or
或者
git checkout filename
git commit -am "commit message"
git commit -am adds files and makes the commit message at the same time.
git commit -am 添加文件并同时生成提交消息。
And if you need to REMOVE the changes you made in the last file:
如果您需要删除您在最后一个文件中所做的更改:
git stash -u
回答by ó?th Manē
You can just create a .gitignore file and add the name of the file you want to exclude in it.
您可以创建一个 .gitignore 文件并在其中添加要排除的文件的名称。