How to maintain branch hierarchy in Git?

I just created the created git branch structure as follows:

enter image description here

Generated from:

git init echo "Hello World" > file1.txt git add file1.txt git commit -m "Hello world" git checkout -b A echo "This is from A branch" > file2.txt git add file2.txt git commit -m "from A branch" git checkout -b B echo "This is from B branch" >> file2.txt git commit -a -m "from B branch" 

Now I clone this structure and synchronize the master , A and B branches:

 git clone /path-to-source/ git checkout -b master remotes/origin/master git checkout -b A remotes/origin/A 

and the cloned repository reflects the original hierarchy:

enter image description here

Now I go back to the original folder and add something to master and rebase A and B:

 cd /path-to-source/ git checkout master echo "more files" > file3.txt git add file3.txt git commit -m "Improved master" git checkout A git rebase master git checkout B git rebase A 

enter image description here

The problem occurs when I return to the cloned repository and try to keep this structure. If I only pull the master branch, I get the following:

enter image description here

I can go to each branch and update:

 git checkout A git pull 

but I get tree branches like this:

enter image description here

The question is how to keep a clean repository clone? I want to get this (manipulated graphics) in a cloned repository:

enter image description here

Bonus If possible, I would like to find a way to keep the commits in branches A or B as well, like this:

enter image description here

generated from these commands in the source repository:

 git checkout A echo "something" > other.txt git add other.txt git commit -m "Other A commit" git checkout B git rebase A 

NOTE 1 If this helps, the cloned repository NEVER completes NOTE 2 . You can assume that only 1 user is sent to the original repository

+4
source share
1 answer

Rebasing means changing SHA1, so your branch A in the clone is no longer valid.

You can follow How to restore / resynchronize after someone clicked on rebase or reset on a published branch? "to restore cloned branch A (inspired by" UPSTREAM RESTORE ").

But if:

  • these permutations are common,
  • you do not have a new clone commit for branch A and branch B

You can simply reset those branches (on the clone) based on their origin/A and origin/B : you reset branch A HEAD to origin/A

 git branch -f origin/A 
+5
source

All Articles