how to fetch new commits to pr-37?
- User
how to fetch new commits to pr-37?
- Assistant
When you want to fetch new commits into an existing PR branch (say pr-37), the process depends on how you originally checked it out. Here are the common cases:
Case 1: You cloned it via git fetch origin pull/37/head:pr-37 In this setup, pr-37 is a local branch that is not automatically tracking the PR on GitHub. To update it: bashCopyEdit# Fetch latest changes from the PR git fetch origin pull/37/head:pr-37
Checkout the branch
git checkout pr-37
This updates your local branch with the latest commits from PR 37.
Case 2: You added a remote for the contributor’s fork If the PR comes from a fork, and you added a remote (say contrib): bashCopyEdit# Fetch from the contributor’s fork git fetch contrib branchname:pr-37
Checkout the branch
git checkout pr-37
Case 3: You want to keep it up-to-date continuously Instead of re-fetching with the full command each time, set it to track the PR directly: bashCopyEdit# Checkout your local pr-37 git checkout pr-37
Set the upstream to point at the PR
git branch --set-upstream-to=origin/pull/37/head
Now you can just do: bashCopyEditgit pull
👉 Do you want me to check which case applies to you (based on how you originally fetched pr-37), so I can give you the exact command for your setup?
- User
git fetch origin pull/37/head:pr-37 fatal: refusing to fetch into branch 'refs/heads/pr-37' checked out at '/Users/shachar.don/repo/share-lm'
- Assistant
Ah, I see exactly what happened 🙂 That error means: Git refuses to fetch directly into a branch you currently have checked out (in this case pr-37). That’s why it fails.
✅ Two ways to fix: Option 1: Fetch into a temporary ref, then merge bashCopyEdit# Stay on pr-37 git fetch origin pull/37/head:pr-37-tmp git merge pr-37-tmp git branch -d pr-37-tmp # cleanup after merge (optional)
This way you get the new commits without overwriting the branch while it’s checked out.
Option 2: Checkout another branch, then fetch directly bashCopyEdit# Move to another branch (e.g. main) git checkout main
Now safe to fetch directly into pr-37
git fetch origin pull/37/head:pr-37
Switch back to pr-37
git checkout pr-37
Option 3: Set up remote-tracking for easier pulls Once you have it checked out: bashCopyEditgit branch --set-upstream-to=origin/pull/37/head pr-37
Then in the future you can just do: bashCopyEditgit pull
👉 Do you prefer to keep your pr-37 branch always in sync with the PR (tracking), or do you want to just grab a snapshot of the latest commits when needed?
- User
git fetch origin pull/37/head:pr-37
From https://github.com/shachardon/share-lm ! [rejected] refs/pull/37/head -> pr-37 (non-fast-forward)
- Assistant
Got it 👍 That error means your local branch pr-37 has diverged from the remote PR branch (non-fast-forward), so Git refuses to overwrite it. You now have 3 choices depending on what you want:
🔹 Option 1: Overwrite your local pr-37 with the PR branch (discard local changes) If you don’t care about the local history and just want the latest PR commits: bashCopyEditgit fetch origin pull/37/head:pr-37 --force
That will reset your local pr-37 to match the current PR on GitHub.
🔹 Option 2: Merge new commits into your local branch If you want to keep your local work and just add the new commits from the PR: bashCopyEditgit fetch origin pull/37/head:pr-37-tmp git checkout pr-37 git merge pr-37-tmp git branch -d pr-37-tmp # cleanup
🔹 Option 3: Rebase your work on top of the new PR commits If you want a linear history: bashCopyEditgit fetch origin pull/37/head:pr-37-tmp git checkout pr-37 git rebase pr-37-tmp git branch -d pr-37-tmp # cleanup
👉 Do you want your pr-37 branch to exactly mirror the GitHub PR (overwrite), or do you want to preserve your local commits and add the new ones on top?