English 中文(简体)
如何只将一部分新档案编成 g子?
原标题:How to stage only part of a new file with git?
  • 时间:2011-06-22 07:58:17
  •  标签:
  • git

www.un.org/spanish/ga/president 现在是我日常工作的一部分。

问题似乎是没有处理未跟踪的档案。 我想要做的是追踪新的档案,但只增加一部分,即新档案的某些部分尚未准备就绪。

例如,用 g子添加——i,我可以选择派系办法,甚至ed子,以形成新法典的某些部分,使解冻法评论失去作用。 我热爱以这种方式开展工作,因为它使我目前从事的特大派所在地显然仍然需要工作。

不幸的是,我似乎无法用一个无法追踪的档案这样做。 要么是整个档案,要么是零。 我一直在使用的工作,正在或甚至投入一个新档案,而新档案是空洞的,然后按常规进行个人变化。 但是,这一解决办法感到 like卑,当我忘记或改变我的想法时,它造成的麻烦比应该多。

因此,问题在于:如何只准备一部分新档案,以便跟踪这一新档案,使其全部或部分内容没有被利用?

最佳回答

谁,所有<代码>更新-index和hash-object的业务似乎过于复杂。 相反的:

git add -N new_file
git add -i  # or  git add -p  if you prefer

http://www.ohchr.org。

-N, --intent-to-add
    Record only the fact that the path will be added later.  An entry
    for the path is placed in the index with no content.  This is useful
    for, among other things, showing the unstaged content of such files
    with git diff and committing them with git commit -a.
问题回答
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
git add --interactive newfile

简明扼要:

mkdir /tmp/demo
cd /tmp/demo
git init .

echo hello > newfile
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
  • Hint If you re sure the empty blob already exists in your git object database, you could hardcode the hash e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 instead. I don t recommend doing that_
  • Hint If you re on Windows, you probably can just use NUL: instead of /dev/null. Otherwise, use something like echo -n | git hash-object --stdin -w

现在,该指数将包含newfile,作为空档,而且空档已经输入目标数据库,如果还没有:

$ find .git/objects/ -type f 
.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   newfile
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   newfile
#

$ git diff
diff --git a/newfile b/newfile
index e69de29..ce01362 100644
--- a/newfile
+++ b/newfile
@@ -0,0 +1 @@
+hello

This should be precisely what you want. May I also recommend the vim fugitive plugin for very intelligent index management (see Better git add -p?)

这样做的最容易的方法(一般而言是非互动性的循环)是<条码>git gui。 它带有 g,应当在所有得到 g支持的平台上开展工作。

简而言之,<代码>git gui和i gui将开放,允许固定和分离 h,甚至允许单一线跟踪和未跟踪档案。

“Git

Edit: this doesn t seem to be working now. I m sure it was before (on git 1.7.1). In case it doesn t work, I suggest staging /dev/null as sehe suggests above:

git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile

如果您在Windows(不含/dev/null)上重读。 然后,你可以用通往空洞的路子来取代。

Original answer

你们想要

git add -p # (or --patch)

这给我增加了无法追踪的档案。 单页:

Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.

This effectively runs add --interactive, but bypasses the initial command menu and directly jumps to the patch subcommand. See “Interactive mode” for details.

Just for the record of another possibility: I use

git add -N file
git add -p file

那么,你就能够打上ks子,或洗.。





相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...

热门标签