簡體   English   中英

為特定的git commit創建diff / patch

[英]Create diff/patch for specific git commits

假設我有類似的git歷史記錄:

sha_001 'Some commit description'
sha_002 'Some other commit description (without regularity)'
sha_003 'Some other commit description (without regularity)'
sha_004 'Some other commit description (without regularity)'
sha_005 'Some other commit description (without regularity)'
sha_006 'Some other commit description (without regularity)'

如何為指定的提交創建補丁?

我知道:

git diff sha_003..sha_001

但這是針對提交范圍的。 我需要一些東西,例如:

git diff sha_002,sha_005,sha_006

問題規格

在RubyMine中,我可以這樣做:

在此處輸入圖片說明

  • 在RubyMine中,我無法指定提交(只能通過鼠標選擇它們)
  • 我想在沒有RubyMine的情況下使用純cli git工具和Atom作為補丁查看器來執行此操作。

另外,我寫了這個腳本:

#!/usr/bin/env ruby

# frozen_string_literal: true
require 'tempfile'

abort "Usage:\ndiffco hash1,hash2,hash3" if ARGV.empty?

commits = ARGV.first.split ','

patch = ''
commits.each do |commit|
  commit_data = <<-SEPARATOR
----------------------------------------------------------------------
----------------------------------------------------------------------
------------------ PATCH FOR COMMIT: #{commit} -----------------------
----------------------------------------------------------------------
----------------------------------------------------------------------
SEPARATOR
  commit_data += `git show #{commit} --stat`
  commit_data += `git show #{commit} --unified=20`
  patch += commit_data
end

path = File.join Dir::Tmpname.tmpdir, Dir::Tmpname.make_tmpname('commits', '.diff')

File.open(path, 'w') { |f| f.write patch }
`atom -a #{path}`

它允許我在Atom編輯器中看到單個diff / patch文件,但我不想看到中間的更改。

我不認為,僅使用git-diff就可以直接實現。 但是您可以進行交互式的rebase來將指定的提交git rebase -i sha_006^在一起git rebase -i sha_006^ 然后,您可以在rebase創建的新提交上使用git-diff。 如果您不想丟失最初的提交,只需在新的臨時分支中運行rebase。

git format-patch -o patches sha_006..sha_001將為從sha_006到sha_001的每次提交創建補丁文件,並將所有這些補丁文件保存到patch文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM