2

I'm not sure how I should do that. I read threads on stack but I don't want to break anything on the old (existing) repo so I think its better to ask...

I want to create new repo from existing one, keep the history of the existing repo and of course keep the existing repo unchanged. I just want my new repository to be a totally separate one but with the commits from the existing one.

How can I achieve that purely using git commands?

Thanks.

pasteam
  • 43
  • 7
  • In Git, commits *are* the history. If you have the commits, you have the history. When you clone, you get all the commits (well, there are a few caveats to the "all" here but close enough) so you have the same history. Note, however, that `git clone` does *not* copy *branches:* instead, it takes the original branch names from the original repository and changes those into your own new remote-tracking names. Then it makes *one* branch name in your clone. So you have the same history, but you *find* these commits using remote-tracking names, not branch names. – torek Jun 14 '22 at 09:34

1 Answers1

1

Simply clone the repository, change its origin and push to your own remote empty repository.

git clone url/original/repo
cd repo

Track all remote branches as local ones:

for i in $(git for-each-ref --format=%(refname:short) \
  --no-merged=origin/HEAD refs/remotes/origin); do \
    git switch --track $i; \
done

(replace origin with upstream if you already had renamed origin as upstream)

Finally:

git remote rename origin upstream
git remote add origin url/new/repo
git push --set-upstream origin main
git remote remove upstream
git push --mirror
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • pushing onto my new repository will not affect the old (existing) one, right? – pasteam Jun 14 '22 at 09:02
  • @pasteam It will not, indeed. – VonC Jun 14 '22 at 09:02
  • @pasteam For GitHub repository, you could also fork it (but that makes sense if you want to contribute back to the original repository): [`gh repo fork`](https://cli.github.com/manual/gh_repo_fork) – VonC Jun 14 '22 at 09:04
  • I did that as you said and it deleted all my branches except for master from the origin (existing one).... how can i revert that? I said i want to keep everything the same on the existing one. – pasteam Jun 14 '22 at 09:15
  • @pasteam Nothing is deleted: a `git branch -avv` should list all remote branches from the original upstream repository. – VonC Jun 14 '22 at 09:16
  • yeah but on the origin repo i have only one branch which is master but there were much more... i need those branches there – pasteam Jun 14 '22 at 09:20
  • @pasteam What you call "origin", is it the original remote repository where there where all those branches, or is it the new created repository that you own? Anyway, I have edited the answer to add the missing step. – VonC Jun 14 '22 at 09:21
  • Ok let me ask this. Can I revert the whole thing? Right now on my original project (repo) i have one branch which is master but I had more there. On my new project i have nothing. Literally empy repo. – pasteam Jun 14 '22 at 09:25
  • this is what i see when i type git remove -v origin (new repo url) (fetch) origin (new repo url) (push) upstream (old repo url) (fetch) upstream (old repo url) (push) – pasteam Jun 14 '22 at 09:32
  • @pasteam You can create local branches from upstream, as mentioned in the answer. and push --mirror from your local clone. – VonC Jun 14 '22 at 18:28
  • Some steps seem to be missing. After adding the new url (`git remote add origin url/new/repo`) I still had to assign the branch to it: `git push --set-upstream origin main` and remove upstream: `git remote remove upstream` – user1380653 Apr 13 '23 at 14:54
  • @user1380653 Thank you for your feedback. I have updated the answer accordingly. Let me know if anything would still be missing in it. – VonC Apr 13 '23 at 17:18