Home > Mobile >  Can I imitate fast-forwarded pull if my local branch diverges with remote in Git?
Can I imitate fast-forwarded pull if my local branch diverges with remote in Git?

Time:11-18

I have three branches first, second, and master:

  D---E---F first
 /
A---B---...---C---... master
               \
                G---H second
  • first is a final version and is waiting to be merged into master as a single squashed commit.
  • second is still under development.

I want to regularly check if second merged with first will still work properly after new commits appear in second.

I have two repositories:

  • At repo1 I simply add new commits I and J to second and push them to origin.
  • At repo2 my working branch is second, which has been rebased onto first. I am never going to push it at all, repo2 is needed only for local build checks.
       repo1
  D---E---F first
 /
A---B---...---C---... master
               \
                G---H---I---J second, origin/second

       repo2
$ git checkout second
$ git rebase first

  D---E---F first
 /
A---B---...---C---... master
 \
  D'--E'--F'--B'--...---C'--G---H second

The important thing is that commit E(E') causes a long-time rebuild of the whole project, so I want it to never be reapplied again and stay where it is.

That's why I can't use:

git pull -r

on second because it would rearrange all commits and result in this

         repo2
A---B---...---C---G---H---I---J---D'--E'--F' second

Also, I can't use fast-forwarded pull because second in repo2 and origin/second diverge. But my guess is that something like fast-forwarded pull is indeed possible. It should result in the following:

         repo2
  D---E---F first
 /
A---B---...---C---... master
 \
  D'--E'--F'--B'--...---C'--G---H---I---J second

Can I somehow imitate it with some sequence of Git commands?

CodePudding user response:

From your repo2 : you can check the state of origin1/second, then run git fetch, check the new state and try to cherry-pick the delta :

before=$(git rev-parse orgin1/second)
git fetch origin1
after=$(git rev-parse orgin1/second)

if [ "$before" != "$after" ]; then
    git checkout second
    git cherry-pick ${before}..${after}
fi

If you need this to work not just with a hard coded second branch but for any active branch you may have :

branch=$(git branch --show-current)

before=$(git rev-parse origin1/$branch)
git fetch
...

or if you want to pass the branch to fetch as an argument :

branch=$1
if [ -z "$branch" ]; then
    branch=$(git branch --show-current)
fi

or ...

  • Related