简体   繁体   中英

Git: copying a local repo to another folder

Say that I have a local git repo ("A") with a bunch of commits that I don't want to publish.

If I copy everything that is in this local repo "A" except for the git directory into a new folder (let's call it "B") and git init another repo there, is there any risk of this somehow messing up the first named local repo ("A") and losing the commit history for that repo?

The .git sub-directory turns a directory into a git repository. If you remove it (or copy everything but the .git subdirectory) then you have a project that is not a git repository and it's not linked to any git repository in any way.

Running git init in the "B" directory creates a new git repository that is not linked in any way to the original repository ("A") or any other repository.

But keep in mind that after git init , the "B" repository is a different project (with a fresh, empty history) than the old "A" repository and linking them (even possible) will generate duplicate commits and conflicts. (Thanks @NickVolynkin for pointing that out).

The usual solution when you have some commits you don't want to publish is to put them on a new branch and remember to not push it.

Copy with Archive Settings

From a parent directory:

cp -a repo-A repo-B

will make a duplicate repository that won't modify anything in A with the possible exception of atime (depending on your filesystem), which Git doesn't track anyway.

Both repositories will be functionally identical, including history and uncommitted work. You can then make whatever changes you like independently to either repository.

Caveats

If you've cloned with the --shared flag, or set .git/objects/info/alternates you may have shared files between repositories even if you've created an archive copy. This is not the general case, but is an exception you must be aware of and take special precautions with if you've done this. Caveat emptor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM