Tob*_*ler 525 git github cherry-pick
如果我想要合并到Git分支中,那么只对某个特定提交中更改的某些文件进行了更改,其中包括对多个文件的更改,如何实现?
假设git的承诺称为stuff
具有对文件的改变A
,B
,C
,和D
,但我想只合并stuff
的对文件的更改A
和B
.这听起来像是一项工作,git cherry-pick
但cherry-pick
只知道如何合并整个提交,而不是文件的子集.
Cas*_*bel 631
我会用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
Run Code Online (Sandbox Code Playgroud)
如果绝大多数修改都是您不想要的,而不是检查单个路径(中间步骤),您可以重置所有内容,然后添加您想要的内容:
# 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 .
Run Code Online (Sandbox Code Playgroud)
Mic*_*son 124
其他方法对我不起作用,因为提交对很多其他文件有很多更改和冲突.我想出的只是简单
git show SHA -- file1.txt file2.txt | git apply -
Run Code Online (Sandbox Code Playgroud)
它实际上不是add
文件或为您提交,因此您可能需要跟进它
git add file1.txt file2.txt
git commit -c SHA
Run Code Online (Sandbox Code Playgroud)
或者如果你想跳过添加,你可以使用--cached
参数来git apply
git show SHA -- file1.txt file2.txt | git apply --cached -
Run Code Online (Sandbox Code Playgroud)
Tyr*_*son 73
我通常使用-p
带有来自另一个分支的git checkout 的标志,我发现它比我遇到的大多数其他方法更容易和更细化.
原则上:
git checkout <other_branch_name> <files/to/grab in/list/separated/by/spaces> -p
Run Code Online (Sandbox Code Playgroud)
例:
git checkout mybranch config/important.yml app/models/important.rb -p
Run Code Online (Sandbox Code Playgroud)
然后,您会看到一个对话框,询问您在"blob"中需要哪些更改,这几乎可以解决每一段连续代码更改,然后您可以为每个代码块发出信号y
(是)n
(否)等信号.
该-p
或patch
选项可用于各种Git中包括的命令git stash save -p
,它允许你选择你想从你目前的工作来隐藏什么
我有时会使用这种技术,当我做了很多工作并希望将其分离并提交更多基于主题的提交时使用git add -p
并选择我想要的每个提交:)
Mar*_*air 42
也许这种方法优于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
Run Code Online (Sandbox Code Playgroud)
cmi*_*tti 27
Cherry pick是从特定的"提交"中选择更改.最简单的解决方案是选择要使用的某些文件的所有更改
git checkout source_branch <paths>...
Run Code Online (Sandbox Code Playgroud)
例如:
$ 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
Run Code Online (Sandbox Code Playgroud)
来源和完整解释http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/
更新:
使用此方法,git将不会合并文件,它将覆盖目标分支上执行的任何其他更改.您需要手动合并更改:
$ git diff HEAD文件名
eli*_*stg 22
有时,使用签出从提交中获取特定文件可能会更容易。在我看来,它给了你更多的控制权。
我会这样做:
git checkout <branch|hash> -- path/to/file1 path/to/filen
Run Code Online (Sandbox Code Playgroud)
然后取消暂存必要的更改以适应代码并在提交之前对其进行测试。如果一切按预期进行,则提交。
kub*_*zyk 14
为了完整起见,最适合我的是:
git show YOURHASH --no-color -- file1.txt file2.txt dir3 dir4 | git apply -3 --index -
Run Code Online (Sandbox Code Playgroud)
它正是 OP 想要的。它在需要时进行冲突解决,类似如何解决merge
。它确实add
但不是commit
您的新更改,请参阅status
。
fun*_*oll 11
我会挑选一切,然后这样做:
git reset --soft HEAD^
Run Code Online (Sandbox Code Playgroud)
然后我会恢复我不想要的更改,然后进行新的提交.
tec*_*ams 11
情况:
你在你的分支上,让我们说master
你在任何其他分支机构都有你的承诺.您必须从该特定提交中仅选择一个文件.
该方法:
第1步:签出所需的分支机构.
git checkout master
Run Code Online (Sandbox Code Playgroud)
第2步:确保已复制所需的提交哈希.
git checkout commit_hash path\to\file
Run Code Online (Sandbox Code Playgroud)
第3步:您现在可以在所需分支上更改所需文件.您只需要添加并提交它们.
git add path\to\file
git commit -m "Your commit message"
Run Code Online (Sandbox Code Playgroud)
小智 7
我找到了另一种方法,可以防止在挑选樱桃时发生任何冲突的合并,IMO 有点容易记住和理解。由于您实际上不是挑选提交,而是挑选其中的一部分,因此您需要先拆分它,然后创建一个适合您需求的提交并挑选它。
首先从要拆分的提交创建一个分支并签出它:
$ git checkout COMMIT-TO-SPLIT-SHA -b temp
Run Code Online (Sandbox Code Playgroud)
然后恢复之前的提交:
$ git reset HEAD~1
Run Code Online (Sandbox Code Playgroud)
然后添加您想要挑选的文件/更改:
$ git add FILE
Run Code Online (Sandbox Code Playgroud)
并提交它:
$ git commit -m "pick me"
Run Code Online (Sandbox Code Playgroud)
注意提交哈希,我们称之为 PICK-SHA 并返回到你的主分支,例如强制结帐:
$ git checkout -f master
Run Code Online (Sandbox Code Playgroud)
并挑选提交:
$ git cherry-pick PICK-SHA
Run Code Online (Sandbox Code Playgroud)
现在您可以删除临时分支:
$ git branch -d temp -f
Run Code Online (Sandbox Code Playgroud)
您可以使用:
git diff <commit>^ <commit> -- <path> | git apply
Run Code Online (Sandbox Code Playgroud)
该符号<commit>^
指定 的(第一个)父级<commit>
。<path>
因此,此 diff 命令选择在 commit 中所做的更改<commit>
。
请注意,这还不会提交任何内容(确实如此git cherry-pick
)。所以如果你想要这样,你必须这样做:
git add <path>
git commit
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
198880 次 |
最近记录: |