如何追踪但不是舞台,如何取消演出而不是未演奏?

时间:2013-03-27 06:30:58

标签: git

我有两个简明的问题:

  • 我如何跟踪文件但没有暂存它们?
  • 如何在不删除文件的情况下取消暂存文件?

注意: 我知道我可以进行初始提交来跟踪文件,并从那里跟踪我的文件。但是有可能专门做我上面要求的吗?

我尝试使用git add -N <expr>但它会跟踪该文件并将其添加到commit:

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
#

如果我git reset HEAD fileAgit rm --cached fileA取消暂存,则还要解开文件。 这个命令git rm fileA建议我使用标志-f来删除文件。

那么,有可能只跟踪而不是阶段,并且只能展开但不能跟踪文件吗?

2 个答案:

答案 0 :(得分:33)

更新(2015年5月)

  

我尝试使用git add -N <expr>但它会跟踪该文件并将其添加到commit:

即将推出的Git 2.5(2015年第二季度)已不再是这种情况 请参阅“File doesn′t get into the commit after using git add -N


原始答案(2013年3月)

  

如何在不删除文件的情况下取消暂存文件?

这是官方的方式:

git reset HEAD fileA

但是因为它是一个新文件,你也可以解开它(从索引中删除它,而不会引用任何先前提交它)。

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

我建议为这些文件创建一个分支,以便在那里添加/提交它们 请参阅“What is Tracked files and Untracked files in the context of GIT?

  
      
  • 跟踪文件是上次快照中的文件;它们可以不被修改,修改或分阶段。
  •   
  • 未经跟踪的文件是其他所有内容 - 工作目录中的任何文件都不在您的上一个快照中且不在您的暂存区域(索引)
  •   

这意味着,对于一个新文件,unstaged它意味着要跟踪它。

tracked files in git
(来源:Pro Git Book,2.2 Git Basics - Recording Changes to the Repository
(感谢您louisfischer,更新/修复in the comments

另请参阅“git - how to tell if a file is git tracked (by shell exit code)?”。

答案 1 :(得分:1)

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