Skip to content

Instantly share code, notes, and snippets.

@mmikhan
Created October 11, 2025 19:58
Show Gist options
  • Save mmikhan/78fec4f7fbdad938692be2a1475c9e1e to your computer and use it in GitHub Desktop.
Save mmikhan/78fec4f7fbdad938692be2a1475c9e1e to your computer and use it in GitHub Desktop.
A chat history between me and Copilot AI model to find out how to merge from the specific upstream main commit with the rest to the origin or local branch

mmikhan: I fetched the upstream branch first, git fetch upstream and then merged, git merge upstream/main --allow-unrelated-histories. It had some conflicts that I resolved and merged eventually but that brought all the upstream commit history. all I wanted was the last commit instead of pulling the entire upstream commit history. how could have I done better?

GitHub Copilot: You're right! Merging with --allow-unrelated-histories pulled in the entire upstream commit history, which is often not what you want. Here are better approaches:

Option 1: Cherry-pick the specific commit

git fetch upstream
git cherry-pick <commit-hash>

This applies only the specific commit you want without bringing the entire history.

Option 2: Create a patch and apply it

git fetch upstream
git format-patch -1 upstream/main --stdout > upstream-changes.patch
git apply upstream-changes.patch
git add .
git commit -m "Apply upstream changes: [description]"

Option 3: Manual diff and apply

git fetch upstream
git diff HEAD upstream/main > changes.diff
# Review the diff, then apply manually or:
git apply changes.diff
git add .
git commit -m "Integrate upstream changes: [description]"

To fix your current situation:

You can clean up the history by doing an interactive rebase to squash the upstream commits:

git rebase -i HEAD~[number-of-commits-to-squash]

For future reference:

  • Use git log --oneline upstream/main first to see what commits you're about to merge
  • If you only want specific changes, cherry-pick individual commits
  • If the repositories have completely different histories, consider whether you really need to merge or if copying specific files/changes manually might be cleaner

The cherry-pick approach would have been perfect for your use case!

mmikhan: while I want to cherry pick one, I want to get all the changes from the selected cherry pick to the rest of the recent but not all the old ones before from the cherry picked

GitHub Copilot: You want to cherry-pick a range of commits from a specific commit to the latest, not just one commit. Here's how to do that:

Option 1: Cherry-pick a range of commits

# Cherry-pick from a specific commit hash to the latest
git cherry-pick <starting-commit-hash>..upstream/main

Option 2: Find the commit you want to start from

# First, see the commit history to identify where you want to start
git log --oneline upstream/main

# Then cherry-pick from that point forward
git cherry-pick <commit-where-you-want-to-start>^..upstream/main

The ^ symbol means "starting from the commit before this one" so it includes your selected commit.

Option 3: Interactive cherry-pick (more control)

# Get the commit hashes you want
git log --oneline upstream/main | head -10

# Cherry-pick them one by one or in ranges
git cherry-pick <hash1> <hash2> <hash3>
# or
git cherry-pick <hash1>..<hash3>

To clean up your current situation:

Since you already merged everything, you can:

  1. Reset to before the merge:
git reset --hard HEAD~1  # Go back one commit (before merge)
  1. Then do the selective cherry-pick:
git cherry-pick <starting-commit>..upstream/main

This way you'll get only the commits from your chosen starting point to the latest, without all the old history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment