如何git-cherry-pick仅更改某些文件?

| 如果我想合并到Git分支中,而只对某些提交中的某些文件所做的更改(包括对多个文件的更改)进行更改,那么如何实现呢? 假设名为
stuff
的Git提交对文件
A
B
C
D
进行了更改,但是我只想将
stuff
\的更改合并到文件
A
B
。这听起来像是a8ѭ的工作,但
cherry-pick
只知道如何合并整个提交,而不是文件的子集。
已邀请:
我会用
cherry-pick -n
--no-commit
)来做,让您在提交之前检查(和修改)结果:
git cherry-pick -n <commit>

# unstage modifications you don\'t want to keep, and remove the
# modifications from the work tree as well.
# this does work recursively!
git checkout HEAD <path>

# commit; the message will have been stored for you by cherry-pick
git commit
如果绝大多数修改是您不想要的,那么您可以将所有内容重置为零,然后添加所需的内容,而不用检查单个路径(中间步骤):
# unstage everything
git reset HEAD

# stage the modifications you do want
git add <path>

# make the work tree match the index
# (do this from the top level of the repo)
git checkout .
其他方法对我不起作用,因为提交有很多更改,并且与许多其他文件存在冲突。我想出的只是
git show SHA -- file1.txt file2.txt | git apply -
它实际上并不会“ѭ15”个文件或为您执行一次提交,因此您可能需要跟进
git add file1.txt file2.txt
git commit -c SHA
或者,如果要跳过添加,则可以将
--cached
参数用于
git apply
git show SHA -- file1.txt file2.txt | git apply --cached -
您还可以对整个目录执行相同的操作
git show SHA -- dir1 dir2 | git apply -
我通常将
-p
标志与其他分支的git checkout一起使用,与我遇到的大多数其他方法相比,我发现它更容易且更精细。 原则上:
git checkout <other_branch_name> <files/to/grab in/list/separated/by/spaces> -p
例:
git checkout mybranch config/important.yml app/models/important.rb -p
然后,您会看到一个对话框,询问您要在\“ blobs \”中进行哪些更改,这几乎可以解决连续代码更改的每个块,然后您可以为每个代码块发出
y
(是)
n
(否)等信号。
-p
patch
选项可用于git中的各种命令,包括
git stash save -p
,可让您从当前工作中选择要隐藏的内容 当我做了很多工作时,有时我会使用这种技术,想将其分离出来并使用
git add -p
提交更多基于主题的提交,并为每次提交选择想要的内容:)
也许这种方法优于Jefromi的答案的优点是您不必记住git reset的正确行为是正确的:)
 # Create a branch to throw away, on which we\'ll do the cherry-pick:
 git checkout -b to-discard

 # Do the cherry-pick:
 git cherry-pick stuff

 # Switch back to the branch you were previously on:
 git checkout -

 # Update the working tree and the index with the versions of A and B
 # from the to-discard branch:
 git checkout to-discard -- A B

 # Commit those changes:
 git commit -m \"Cherry-picked changes to A and B from [stuff]\"

 # Delete the temporary branch:
 git branch -D to-discard
Cherry Pick是从特定的“提交”中选择更改。最简单的解决方案是选择要使用的某些文件的所有更改
 git checkout source_branch <paths>...
例如:
$ git branch
* master
  twitter_integration
$ git checkout twitter_integration app/models/avatar.rb db/migrate/20090223104419_create_avatars.rb test/unit/models/avatar_test.rb test/functional/models/avatar_test.rb
$ git status
# On branch master
# Changes to be committed:
#   (use \"git reset HEAD <file>...\" to unstage)
#
#   new file:   app/models/avatar.rb
#   new file:   db/migrate/20090223104419_create_avatars.rb
#   new file:   test/functional/models/avatar_test.rb
#   new file:   test/unit/models/avatar_test.rb
#
$ git commit -m \"\'Merge\' avatar code from \'twitter_integration\' branch\"
[master]: created 4d3e37b: \"\'Merge\' avatar code from \'twitter_integration\' branch\"
4 files changed, 72 insertions(+), 0 deletions(-)
create mode 100644 app/models/avatar.rb
create mode 100644 db/migrate/20090223104419_create_avatars.rb
create mode 100644 test/functional/models/avatar_test.rb
create mode 100644 test/unit/models/avatar_test.rb
来源和完整说明http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/ 更新: 使用这种方法,git将不会合并文件,它将覆盖目标分支上所做的任何其他更改。您将需要手动合并更改: $ git diff HEAD文件名
情况: 您在分支上,假设
master
,并且在其他任何分支上都有提交。您只需要从该特定提交中选择一个文件。 该方法: 步骤1:在所需分支上签出。
git checkout master
步骤2:确保已复制所需的提交哈希。
git checkout commit_hash path\\to\\file
步骤3:现在,您可以在所需分支上更改所需文件。您只需要添加并提交它们。
git add path\\to\\file
git commit -m \"Your commit message\"
我只是挑选所有内容,然后执行以下操作:
git reset --soft HEAD^
然后,我将还原不需要的更改,然后进行新的提交。
使用
git merge --squash branch_name
,它将从另一个分支获取所有更改,并为您准备一个提交。 现在,删除所有不需要的更改,然后保留所需的更改。 git不会知道有合并。
我发现了另一种方法,可以防止在进行樱桃选择时发生任何冲突合并,而IMO则很容易记住和理解。由于实际上您不是在挑选承诺,而是一部分承诺,因此您需要先将其拆分,然后再创建一个满足您需求的承诺并选择承诺。 首先从要拆分的提交中创建一个分支,并检出它:
$ git checkout COMMIT-TO-SPLIT-SHA -b temp
然后还原先前的提交:
$ git reset HEAD~1
然后添加您想要选择的文件/更改:
$ git add FILE
并提交:
$ git commit -m \"pick me\"
注意提交哈希,我们将其称为PICK-SHA并返回到您的主分支,例如master强制结帐:
$ git checkout -f master
然后选择提交:
$ git cherry-pick PICK-SHA
现在您可以删除temp分支:
$ git branch -d temp -f
将一个分支合并到一个新的(南瓜)中,并删除不需要的文件:
git checkout master
git checkout -b <branch>
git merge --squash <source-branch-with-many-commits>
git reset HEAD <not-needed-file-1>
git checkout -- <not-needed-file-1>
git reset HEAD <not-needed-file-2>
git checkout -- <not-needed-file-2>
git commit
为了完善起见,最适合我的是:
git show YOURHASH --no-color -- file1.txt file2.txt dir3 dir4 | git apply -3 --index -

git status
它正是OP想要的。必要时它会解决冲突,类似
merge
的处理方式。您所做的新更改不会
add
,但不会not50 not。
您可以使用:
git diff <commit>^ <commit> -- <path> | git apply
标记“ 52”指定了“ 53”的(第一个)父代。因此,此diff命令在提交
<commit>
中选择对
<path>
所做的更改。 请注意,它不会提交任何内容(如
git cherry-pick
那样)。因此,如果您需要这样做,则必须执行以下操作:
git add <path>
git commit

要回复问题请先登录注册