How to keep branch hierarchy in Git?
git
Solution
Rebasing means changing the SHA1, so your branch A in the clone is no longer valid.
You can follow "How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?" to restore the cloned branch A (inspired from the "RECOVERING FROM UPSTREAM REBASE" section).
But if:
- those rebases are a frequent occurrence,
- you don't have any new commit on the clone for branch A and branch B
You might want to 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
Problem
Simplyfiing I have a created a git branch structure like this: 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 `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 source hierarchy: Now I return to source 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 ``` The problem comes when I return to the cloned repository and try to keep this structure. If I just `pull` the `master` branch, I get this: I can go to every branch and update: ``` git checkout A git pull ``` but I get branch trees like this: The question is, how to keep a clean clone of the repository? It is, I want to obtain this (manipulated graphic) in the cloned repository: Bonus: If possible, I would like to find a way to keep also commits in the `A` or `B` branches like this: 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 that helps, cloned repository NEVER commits NOTE 2: You can assume that there is only 1 user commiting to source repository