Skip to content

Instantly share code, notes, and snippets.

@sephraim
Last active March 16, 2024 15:17
Show Gist options
  • Save sephraim/7d014392db6d8a00a950f95645413890 to your computer and use it in GitHub Desktop.
Save sephraim/7d014392db6d8a00a950f95645413890 to your computer and use it in GitHub Desktop.
[Checkout 1 file / commit from branch] Use git `format-patch`, `checkout`, or `cherry-pick`
# STEP 1: Find the latest commit in the branch you want to grab a file from
git log <other-branch>
# STEP 2:
# Option 1: Get 1 file from another branch (preserves author info)
git format-patch -o patches ..<other-branch-latest-commit> -- <path-to-file>
git am patches/*.patch
# This will automatically add commit(s) onto your branch. Squash those commits with:
git rebase -i HEAD~3 # e.g. for 3 commits
# If multiple authors then the author of the earliest commit will become the new author
# Option 2: Get 1 file from another branch (does NOT preserve author info)
git checkout <other-branch> -- <path-to-file>
# Option 3: Get 1 single commit from branch (preserves author info)
git cherry-pick -x <other-branch-commit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment