gpt4 book ai didi

git:重命名后如何交互添加(-p)

转载 作者:太空狗 更新时间:2023-10-29 13:38:15 24 4
gpt4 key购买 nike

假设我更改了一个版本控制的文件并使用 GIT 以外的东西重命名它,但我只想包含该文件的一些差异?我该怎么做?

问题是 GIT 只能添加完整的新文件。

通过示例可能会更好地理解该场景:

$ mkdir temp
$ cd temp
$ git init
$ nano 1.txt
$ cat 1.txt
I am file 1.txt
2
3
4
5

$ git add 1.txt
$ git commit -m "1st commit"
$ #edit some stuff and rename using something else than git
$ cat 2.txt #note, 1.txt is renamed to 2.txt
1 I am file 1.txt
2 I am now file 2.txt
3
4
5
6

$ #note the line with '6 'is added

我现在有什么选择,只提交重命名和将“2”行更改为“2 I am now file 2.txt”,而不是添加带有“6”的行。

在我的情况下,外部重命名并使用 git mv 并不是真正的解决方案,因为在 IDE 中重构可能会发生许多重命名。

基本上,我想进入您通常通过完成 git mv 1.txt 2.txt 获得的情况,在这种情况下,您可以交互式地选择 2.txt 中的哪些行想要上台。

最佳答案

git add命令有漂亮的 -N标志:

-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 add -p .


不需要使用git mv .正如下面@Ajedi32 正确指出的那样,git mv 没有做任何特别的事情。索引条目重命名( rename_index_entry_atread-cache.c )归结为:

remove_index_entry_at(istate, nr);
add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);

edit.(基于上面@Math 的评论)。由于 git 跟踪文件名,它只是两个命令:

mv 2.txt 1.txt                # Rename it back to the first name
git mv 1.txt git 2.txt # Tell git you've moved it

git add -p -- 2.txt # You can now stage the hunks

<罢工>

关于git:重命名后如何交互添加(-p),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com