1

I have a repo with some history and would like to take that repo and move it to another github account.

What is a good way to accomplish this without taking any history with me as I would like to set this new location up as a brand new repo (without any history) with the current code from the original repo.

Also, I have couple branches in the original repo which I would also like to move to the new repo without any history associated with them either.

So my new repo would have the master plus the two other branches and it would not have any history associated with any branches.

u9kV-6J
  • 58
  • 5
  • In the most general form, there is no answer. But if your branches are all rebased on top of master, it's pretty easy: just squash all of your branches history and squash all of master's history. If your branches are based on different locations, you might still be able to do it, though it might not "make sense"... – Mateen Ulhaq Apr 27 '20 at 01:22
  • You cannot have branches without history, unless they are all orphans. If you have multiple branches, then by definition some of them have at least two commits; otherwise they would not be branches _of_ anything. – matt Apr 27 '20 at 01:26
  • @matt in my scenario, both branches would be orphans. – u9kV-6J Apr 27 '20 at 01:38
  • Does this answer your question? [Make the current commit the only (initial) commit in a Git repository?](https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository) – phd Apr 27 '20 at 09:16

2 Answers2

3

First create an orphan branch and make your first commit on it

git checkout --orphan name_of_the_orphan_branch
git commit

The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

Then add a new remote to your repository

git remote add remote_name remote_url

Then push your new branches to your new remote

git push remote_name name_of_the_orphan_branch
Mickael B.
  • 4,755
  • 4
  • 24
  • 48
1

The TL;DR of all of this is that you've set out conflicting requirements. You cannot have what you've asked for.

History, in any Git repository, is simply the set of commits in the repository, with their embedded relationships.

Branch names, in any Git repository, are names for the last commits in long chains of commits. That's all they are. They do not really exist on their own: they depend on the commits. They need commits just to exist. You cannot have a branch name without a commit.

Commits should in general relate to each other. If you wish to have three different branch names pointing to three different commits, these three commits should relate somehow. For them to relate, you must have one of one or more of these properties:

  • the commits are direct parent/child of each other, so that this is their history; or
  • the commits have common ancestors, i.e., history.

Mickael B.'s answer shows you how to take any one existing commit and make a new commit from it, with the new commit having no parent at all. That is, the new commit is a root commit. You could do this for each of your three branch names, but the result will be three unrelated commits. This means you will not be able to merge work done on any of these three branches with work done on any of the other branches. Each branch has a history: it's just that its history consist of a single commit (i.e., that's how long the "long chain" is).

If that's OK, just repeat his answer three times (three git checkout --orphan operations). If not, find some suitable ancestor commit and make that first, as your new --orphan branch, then add your three other commits atop this, perhaps like this:

           o--o   <-- original-branch2
          /
...--o--...--o--o   <-- original-master
      \
       o--...--o   <-- original-branch1

o   <-- new-orphan
 \
  o   <-- new-branch1
   \
    o   <-- new-branch2
     \
      o   <-- new-master

or like this:

  o   <-- new-branch1
 /
o   <-- new-orphan
 \___
  \  `o   <-- new-master
   \
    o   <-- new-branch2

for instance.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Its kind of unusual situation i suppose but the two branches are meant to be completely 'standalone'. They will never be merged to anything. It is a situation where `master` only has a readme file which say look at `branchA` for an example of this and look at `branchB` for an example of something else (the two branches just demonstrate a certain difference in some code and are not meant to be merged at any time). – u9kV-6J Apr 27 '20 at 01:48
  • OK - then just do three `git checkout ; git checkout --orphan ; git commit`s to make the three new branch names exist and point to the three independent commits. Then you can `git push second-remote new-branch1 new-branch2 new-branch3`. Note that each branch has a 1-element-long history (the one commit) though. – torek Apr 27 '20 at 06:06