git 如何跟踪但不登台以及如何取消登台但不退台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15653066/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:21:58  来源:igfitidea点击:

How to track but not stage and how to unstage but not untrack?

git

提问by Jeflopo

I have two concise questions:

我有两个简洁的问题:

  • How I can track files but without staging them ?
  • How I can unstage files for commit without untracking them ?
  • 我如何跟踪文件但不暂存它们?
  • 如何在不取消跟踪的情况下取消暂存文件以进行提交?

NOTE:I know that I can do an initial commit to track the files and start from there with my files tracked. But is possible to specifically do what I'm asking above ?

注意:我知道我可以做一个初始提交来跟踪文件并从那里开始跟踪我的文件。但是有可能专门做我上面问的事情吗?

I tried to use git add -N <expr>but it tracks the file and add it for commit:

我尝试使用git add -N <expr>但它会跟踪文件并将其添加以进行提交:

PS C:\> git add -N fileA
PS C:\> git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   fileA
#
# 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)
#
#       modified:   README.md
#       modified:   composer.lock
#       modified:   fileA
#

If I do git reset HEAD fileAor git rm --cached fileAunstages it but also untracks the file. This command git rm fileAsuggest me to use the flag -f that removes the file fisically.

如果我执行git reset HEAD fileAgit rm --cached fileA取消暂存,但也会取消跟踪文件。此命令git rm fileA建议我使用标志 -f 以财政方式删除文件。

So, It is possible to only track but not stage, and to only unstage but not untrack files ?

那么,是否可以只跟踪但不暂存,以及只取消暂存但不取消跟踪文件?

采纳答案by VonC

Update (May 2015)

更新(2015 年 5 月)

I tried to use git add -N <expr>but it tracks the file and add it for commit:

我尝试使用git add -N <expr>但它会跟踪文件并将其添加以进行提交:

That is no longer the case with the upcoming Git 2.5 (Q2 2015).
See "File doesn′t get into the commit after using git add -N"

即将推出的 Git 2.5(2015 年第二季度)不再是这种情况。
参见“使用后文件没有进入提交git add -N



Original answer (March 2013)

原始答案(2013 年 3 月)

How I can unstage files for commit without untracking them ?

如何在不取消跟踪的情况下取消暂存文件以进行提交?

this is the official way:

这是官方方式:

git reset HEAD fileA

But since it is a new file, you would untrack it as well (remove it from the index, without any previous commit referencing it).

但由于它是一个新文件,您也将取消跟踪它(从索引中删除它,而没有任何先前的提交引用它)。

Starting tracking a file means having it in a index (stage) or a commit.

开始跟踪文件意味着将其置于索引(阶段)或提交中。

I would recommend making a branch for those files, in order to add them/commit them there.
See "What is Tracked files and Untracked files in the context of GIT?"

我建议为这些文件创建一个分支,以便在那里添加/提交它们。
请参阅“什么是 GIT 上下文中的跟踪文件和未跟踪文件?

  • Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged.
  • Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area (index)
  • 跟踪文件是上次快照中的文件;它们可以是未修改的、修改的或暂存的。
  • 未跟踪的文件是其他所有文件 - 工作目录中不在上次快照中且不在暂存区(索引)中的任何文件

That means that, for a new file, unstaged it means untrack it.

这意味着,对于新文件,取消暂存意味着取消跟踪它。

tracked files in git
(Source: Pro Git Book, 2.2 Git Basics - Recording Changes to the Repository)
(Thank you, louisfischer, for the update/fix in the comments)

在 git 中跟踪文件
(来源:Pro Git Book,2.2 Git Basics - Recording Changes to the Repository
(感谢louisfischer在评论中更新/修复)

See also "git - how to tell if a file is git tracked (by shell exit code)?".

另请参阅“ git - 如何判断文件是否被 git 跟踪(通过 shell 退出代码)?”。

回答by lucerna

Track all of your files with git add. Commit all of the changes you want to commit and push to remote. Then stash your new file with git stash. Your file will be tracked but not committed. So in your example you would start by un-staging fileA. Then run git add README.mdand git add composer.lock. git commit -m "Committing fun stuff.". Now we need to track the new file without staging it so we simply do git add fileAfollowed by git stash. When you're ready to commit fileA you can run git stash popto commit/push to remote.

跟踪您的所有文件git add。提交您要提交的所有更改并推送到远程。然后用 .stash 存储你的新文件git stash。您的文件将被跟踪但不会提交。因此,在您的示例中,您将从取消暂存 fileA 开始。然后运行git add README.mdgit add composer.lockgit commit -m "Committing fun stuff.". 现在我们需要在不暂存的情况下跟踪新文件,因此我们只需执行git add fileA后跟git stash. 当您准备好提交 fileA 时,您可以运行git stash pop以提交/推送到远程。