Sobes.tech
Middle

How do you fetch specific commits from another branch in Git?

sobes.tech AI

Answer from AI

To cherry-pick specific commits from someone else's branch in Git, you typically use the git cherry-pick command. It allows you to apply changes from selected commits to your current branch.

Example:

# First, switch to the branch where you want to add the commits
git checkout my-branch

# Then, apply a specific commit from the other branch by its hash
git cherry-pick <commit-hash>

If you need to pick several consecutive commits, you can specify a range:

git cherry-pick <start-commit>^..<end-commit>

It's important that you have access to the remote repository where the other branch is located, and that it is available locally or through fetch. If the branch is not local, first do:

git fetch origin other-branch

After that, you can cherry-pick the desired commits.