git - create new repo from existing one, keep history

Viewed 102

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.

1 Answers

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 --mirror
Related