8

I have a Git repository with two branches: master and redesign. The redesign branch was created from master, and master has not been touched since then:

master
...|--m50--\
            \--m51--|--m52--|--m53-- redesign

The redesign branch has evolved so much that I would like to create a new whole repository from it, taking the first commit of redesign as the initial commit of the new repository and forgetting the previous history inherited from master:

master
...|--m50--

redesign
--r1--|--r2--|--r3--

Is this possible with Git? There is a related question to this, but its goal is to use a directory, not a branch.

Thanks!

Community
  • 1
  • 1
elitalon
  • 9,191
  • 10
  • 50
  • 86

4 Answers4

9

You could:

  • checkout the commit you want for the "root" of your new repo
  • copy all the files to your new directory (except the .git directory)
  • check out your redesign branch
  • git format-patch master..redesign
  • move all the generated patch files somewhere handy

Then go to your new directory and:

$ git init
$ git add .           # make sure your .gitignore is in place though
$ git commit -m"..."
$ git am /path/to/patches/*.patch
Mat
  • 202,337
  • 40
  • 393
  • 406
  • When executing `git am ...` several error messages like these appeared: `/Users/elitalon/Sites/exigo/.git/rebase-apply/patch:447: trailing whitespace` and `error: cake/console/cake.bat: patch does not apply`. What can be happening? – elitalon Nov 28 '11 at 16:39
  • Use a separate directory where you store *only* the patch files generated by `git format-patch`. What you are trying to apply there doesn't look like anything you should be applying. – Mat Nov 28 '11 at 16:47
  • Forget it. I used the flag `--whitespace=fix` and everything worked like a charm. Thank you! – elitalon Nov 28 '11 at 16:52
1

Create your new repository, then:

$ git add remote newrepo <path to newrepo>
$ git checkout redesign
$ git checkout --orphan clean
# The branch is empty, but the files from the original branch are staged for commit.
$ git commit -m"clean"
$ git push newrepo clean:master

Note: If you want to preserve history (or don't care), omit the creation of the "clean"

Daniel
  • 3,243
  • 2
  • 32
  • 31
  • This was a very simple, straightforward solution; one I hadn't thought of at all, and completely obvious in retrospect (like most simple clever things). Thank you (9 years later, no less). I would only add that, regarding the part about preserving history, this answer does not address the part in the original question about "starting from a specific commit", since the history-preserving variant of this approach will in fact copy all nodes from the very beginning. Nothing that squashing commits later via an interactive rebase can't fix though :) – Tasos Papastylianou Sep 10 '22 at 23:19
1

First, copy or clone your git repository.

Then, find out the hash of your new root commit (m51 in your diagram). Put its commit hash into the file .git/info/grafts, then run git filter-branch --all. After you have verified the successful operation, you can remove the original/* backup refs (or re-clone the repository).

Please note that this will create new commit hashes for all commits, so you have to be careful with already published history.

knittl
  • 246,190
  • 53
  • 318
  • 364
0

My method is a bit different. I tested this on my Ubuntu Linux machine. The steps are:

  1. Checkout branch / hash that you want to clone or create root repository

    $ git checkout redesign
    
  2. Make git archive for current branch by using command

    $ git archive HEAD --format zip -o redesign-repository.zip
    

    Above command will create a new file called redesign-repository.zip in current repository with zip format. We can also change HEAD to name / hash of commit, e.g. git archive redesign --format zip -o redesign-repository.zip

  3. Create a new directory to extract the file to

    $ mkdir ../redesign-repo
    
  4. Extract the archive to that directory

    $ unzip redesign-repository.zip -d ../redesign-repo
    
  5. Move to new repository and initialize the new repository with git init

    $ cd ../redesign-repo
    $ git init .
    

This method will rely on the availability of zip / unzip command. I am not sure if its available on every machine.

Raynal Gobel
  • 991
  • 15
  • 25