How can I locally preserve as much git history as I can when the reference repository won't?

35 Views Asked by At

I recently realised the reference repository that our team pushes to was not configured to reject non-fast-forward updates.

Given the propensity of some team members to use history rewriting to clean up their contributions, without concern for whether these commits have been shared, I want to preserve every commit I will ever fetch from the reference repository in case I need to make an archeological investigation.

Of course, I can't afford to just reject these myself, or I'll be stuck in the past: I need to both keep the pre-rewrite history and get on board the post-rewrite one.

1

There are 1 best solutions below

0
Pierre Lebeaupin On

This procedure seems to work:

  1. Never ever prune as part of fetching so you won't be fooled by branches being deleted and recreated either
  2. Regularly fetch from the reference repository, even when not ready to integrate upstream contributions or not currently working on that repository.
  3. If the output from that git fetch does not reveal any forced update, resume a regular activity.
  4. Otherwise, rewind each of the forced updates with git update-ref -m "reverting forced update"
  5. git remote rename origin origin$COUNTER
  6. git remote --set-url origin$COUNTER https://unreachable.example.com/origin$COUNTER/.git
  7. git remote add origin $REFURL
  8. git fetch origin
  9. git branch -u origin/"$(git branch --show-current)"
  10. Similarly update the upstream for the other local branches that warrant it.

This works for me because I only have one local branch tracking any remote one at any given time: the current branch. Maybe two when I am made to make a quick fix before returning to my ongoing work, and only for the duration of that fix: I religiously delete local branches that track a remote one as soon as I have pushed that work and have switched to another branch. As a result, work at step 9 is very limited for me.