soad
soad

Reputation: 71

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

I have three branches first, second, and master:

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

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

I have two repositories:

       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?

Upvotes: 0

Views: 47

Answers (1)

LeGEC
LeGEC

Reputation: 51780

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 ...

Upvotes: 1

Related Questions