1

I have a repository where a branch (admin) has been branched out from develop at some point. It looks roughly like this

develop            c2        c4        c5
      *------------*---------*---------*------------...
      c1            \ 
                     \____________________*c3
                          admin

I want the develop branch to stay the same and admin branch should have it's own repository. For the record, I'm using sourcetree+bitbucket.

I tried the method mentioned in this answer and tried git push url://to/new/repository.git admin:develop. What it did in my new repo is that the history started from the first commit of develop (c1). it looks like this in the new repository.

c1      c2                           c3
*-------*----------------------------*
             develop

I wanted to start this new repository from c2 however. Is it normal behavior or have I done somthing wrong? What's the correct way to achieve desired result?

Whip
  • 1,891
  • 22
  • 43

2 Answers2

1
git checkout -b sliced-admin admin                   # start with admin
git rev-list develop.. | tail -1 >.git/info/grafts   # slice out the develop history
git filter-branch                                    # .
rm .git/info/grafts                                  # .

Now push the sliced-admin branch wherever you want

git push u://r/l sliced-admin:itsnamethere
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Does this process affect the original repository? I don't want anything to happen to the rest of the branches or history of this repo. I don't mind if `admin` branch gets deleted though. – Whip Nov 01 '16 at 11:02
  • No, as it stands it affects only the sliced-admin branch. – jthill Nov 01 '16 at 15:32
  • I don't understand your second line, and it's not described in the [documentation](https://git-scm.com/docs/git-rev-list) either. I entered this line **exactly** and got `bash: .git/info/grafts: No such file or directory` – Whip Nov 07 '16 at 08:39
  • make the directory first, `mkdir .git/info`. – jthill Nov 07 '16 at 11:58
  • Could you explain this line please? I have to enter the hash of c1 in place of "develop"? – Whip Nov 07 '16 at 12:13
  • No, run it as it is. The rev-list shows you your current branch's history since it split off from develop, the tail picks the first commit in that history, the redirect puts that line in `.git/info/grafts`, where git sees the bare commit id and interprets it as "treat this commit as having no parents". – jthill Nov 07 '16 at 12:22
0

Because c1 is the parent of c2 then there is no way to accomplish this without rewriting history and changing the commit hashes. But since you're creating a new repository anyway, then this might be acceptable and so you've got a couple options.

Consider this history:

* cd857c2 (HEAD -> develop) c5
* eda39a7 c4
| * 49671f2 (admin) c3
|/  
* 53b169b c2
* 42bf35f c1

I would suggest creating an orphan branch from c2

git checkout --orphan newDev 53b169b

This will create a branch with a new root unrelated to the history of the rest of your repository, and stage all the files in the repository at that point in time to the index, so now you can modify that index as you see fit with reset and then make the initial commit.

git commit -m "c1/c2"

Lastly, cherry-pick the range of commits from the starting commit to the admin branch tip.

git cherry-pick 53b169b..admin

Now you've got an all new branch with completely different commit hashes for the same content starting at your desired point.

* d19cdd1 (HEAD -> newDev) c3
* f886282 c1/c2

Finally push that new branch.

git push url://to/new/repository.git newDev:develop
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167