1

I would like to know, how you would proceed to make a full local copy of a remote git repository, that can be used in-place of the original repository.

All guides I found so far, describe how to copy a remote git repository to another remote one.

One such guide would be:

How to move a full Git repository:

$ git clone https://github.com/Sample/sample.git
$ cd sample.git
$ git checkout remotes/origin/HEAD
$ git checkout remotes/origin/develop
$ git checkout remotes/origin/master
$ git checkout remotes/origin/micros-integration
$ git checkout remotes/origin/release/0.4.8
$ git checkout remotes/origin/release/0.6.0
$ git checkout remotes/origin/remove-keygen
$ git fetch --tags
$ git remote rm origin
$ git remote add origin https://github.com/Target/target.git
$ git push origin --all
$ git push --tags

Yet another one:

Mirroring a git repository without a local copy:

$ git clone --mirror git@github.com/Sample/sample.git
$ cd upstream-repository.git
$ git push --mirror git@github.com/Target/target.git

So how would you proceed to create a local copy without being linked to the original repository? And what should be done, if you want to only maintain specific branches and remove everything else including all history (pull-requests, code changes,...)?

user1861174
  • 467
  • 1
  • 4
  • 13
  • Some web searches I ran that didn't include this question in its results: "git clone absolutely everything branches tags notes". – Kenny Evitt Oct 03 '18 at 14:10
  • Possible duplicate: [Fully backup a git repo?](https://stackoverflow.com/questions/5578270/fully-backup-a-git-repo) – Kenny Evitt Oct 03 '18 at 14:11

1 Answers1

2

To copy a repo including all branches and tags (and other refs), use

git clone --mirror <repo-url>

To unlink your copy from the original, cd into the repo directory and

git remote remove origin

If you only want to preserve a subset of branches, the best procedure depends on specifics; you might be able to restrict what you clone, but exactly how to do it depends on what you want to keep.

As a general solution, after cloning you can delete the branches (and other refs) you don't want from your clone, and then run gc

git branch -D unwanted-branch
git tag -D unwanted-tag
git gc --aggressive --prune=all

gc will not remove anything reachable from any ref (branch, tag, etc) - or any reflog, but a fresh clone shouldn't have any reflog baggage. You can verify what refs exist with

git for-each-ref

If gc is giving you trouble, another option is to clone the clone

git clone --mirror file://localhost/path/to/first/clone

which should not be expected to transfer unnecessary objects. (But again, anything reachable will be copied unless your clone command limits that copying, such as through --single-branch or shallow history options.)

With either method, an object is kept if it is reachable, which could include the commits that make up a branch you've deleted (e.g. if that branch has been merged into a branch you haven't deleted). So if you have specific requirements to limit history beyond what is done by the above procedures, you may need more sophisticated tools (like shallow histories) and the procedure may depend on a lot of specifics.

Also note that a pull request is not a git concept; PR's are an invention of environments that host git remoets. You might have merge commits resulting from PR's, and those are treated just like any other history.

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41