Restore an entire git branch to an orphan branch while maintaining the integrity of the commit tree

I have a repo in which I have two branches, masterand master-oldwhich was created as an orphan branch.

Now I want to completely reinstall masteron master-old, but the tree of each commit should remain unchanged, that is, working copies of each commit on masterand master-oldshould look exactly the same before and after the reboot.

Current state
-------------
A - B - C - D     <--- master

E - F - G - H     <--- master-old

Desired state
-------------
E'- F'- G'- H'- A'- B'- C'- D' <--- master

I tried to accomplish this with git rebase --onto master-old --root. The problem is that in both cases, the initial commit to masterand the whole commit history master-old, many identical files were created, so I get a huge amount of conflicts to solve.

, ?

0
1

, , A--B--C--D, . Rebasing , diffs (changeets), , , , , , A, commit A' H, , B, commit B', A' ..

git filter-branch . :

git filter-branch <filter-list> <branch-name>

Git , <branch-name>, . , , , commit , <filter-list>, . Git , .. " ", .

commit ( , , , , , , , , , , , , ..) 100% -, , commit . " " . " " - , .

( , , , , , , - commit . , : < old-hash, new-hash > . - .)

, - , root commit. , --parent-filter. , --parent-filter. git filter-branch:

- <command>

  • .   stdin   stdout. ,  
git -commit-tree (1): , "-p parent"  normal commit "-p parent1 -p parent2 -p parent3..."  merge commit.

, , stdin , , -p <hash-of-H>. :

E--F--G--H--A'-B'-C'-D'   <-- master

( , , , , ).

( E-F-G-H, master-old , -- , , E, , , timemstamp .)

. - --commit-filter: , . - , ; , , .

git replace

, git replace. , git replace , , , refs/replace/ -. , Git , Git , refs/replace/<hash-id>. , Git , .

, Git, commit A, . , commit -, . - commit H. ( , , A.)

, , A' - refs/replace/<big-ugly-hash>:

A--B--C--D   <-- master

E--F--G--H   <-- master-old
          \
           A'   <-- refs/replace/deadcabf001...

( A , , , deadcabf001..., ID).

git log , commit D, commit D, D C, C, B ID B, A ... whoa, , refs/replace/ ! A ! A'! A' B parent, A' parent H, G ..

git replace, - . , "" " " - ", . Git :

  • , , ;
  • -, refs/replace/hash ;
  • , git --no-replace-objects.

3 () , . 2 , git clone . ( , ).

- 2 , , , , git filter-branch. git --no-replace-objects filter-branch, Git commit A' commit A. A' A. --parent-filter. E H, -, , . , git filter-branch .

+2

All Articles