Skip to content

Instantly share code, notes, and snippets.

@leoxlin
Created April 22, 2021 15:33
Show Gist options
  • Save leoxlin/1361722f18fa1ee62c219bf813033da2 to your computer and use it in GitHub Desktop.
Save leoxlin/1361722f18fa1ee62c219bf813033da2 to your computer and use it in GitHub Desktop.

Git Diff + Patch

Using a diff patch is often times overlooked as a merge strategy because not many people know it exists. This is really useful when you have two branches that have diverged commit wise or if you only want to merge/revert diff from specific files but not cherry-pick the entire commit.

How it works

You want can use git diff of the exact things you want to merge/revert.

Apply branch_beta changes on file_or_file_i_actually_want_to_merge on branch_alpha

git checkout branch_alpha
git diff branch_alpha..branch_beta file_or_file_i_actually_want_to_merge > ~/merge_from_branch_beta.diff
git apply ~/merge_from_branch_beta.diff

Revert changes made on branch_beta back into the branch_alpha version

git checkout branch_beta
git diff branch_beta..branch_alpha file_or_file_i_actually_want_to_merge > ~/revert_to_branch_alpha.diff
git apply ~/revert_to_branch_alpha.diff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment