'How to move a git repository into another directory and make that directory a git repository?
I have a directory gitrepo1. This directory is a git repository.
I would like to move this gitrepo1 into another directory newrepo.
Directory newrepo should be the new git repository with no loss of git history and should contain the directory gitrepo1.
Directory gitrepo1 should just be a directory now (inside newrepo), without any
.gitindex, i.e. it should NO longer be an independent git repository or a submodule.
How can I do this?
Solution 1:[1]
It's even simpler than that. Just did this (on Windows, but it should work on other OS):
- Create newrepo.
- Move gitrepo1 into newrepo.
- Move .git from gitrepo1 to newrepo (up one level).
- Commit changes (fix tracking as required).
Git just sees you added a directory and renamed a bunch of files. No biggie.
Solution 2:[2]
I am no expert, but I copy the .git folder to a new folder, then invoke: git reset --hard
Solution 3:[3]
To do this without any headache:
- Check what's the current branch in the gitrepo1 with
git status, let's say branch "development". - Change directory to the newrepo, then
git clonethe project from repository. - Switch branch in newrepo to the previous one:
git checkout development. - Syncronize newrepo with the older one, gitrepo1 using
rsync, excluding .git folder:rsync -azv --exclude '.git' gitrepo1 newrepo/gitrepo1. You don't have to do this withrsyncof course, but it does it so smooth.
The benefit:
You are good to continue exactly where you left off: your older branch, unstaged changes, etc.
Solution 4:[4]
simple way to move a git repository into another directory and make that directory a git repository using mirroring method
git clone --mirror [email protected]/mirror-repository.git
cd mirror-repository.git
push changes to new repository using below command
git push --mirror [email protected]/new-mirror.git
This will get all the branches and tags that are available in the mirror repository and will replicate those into the new location.
Don’t use git push --mirror in repositories that weren’t cloned by --mirror as well. It’ll overwrite the remote repository with your local references (and your local branches).git clone --mirror is prefered over git clone --bare because the former also clones git notes and some other attributes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | david.pfx |
| Solution 2 | Community |
| Solution 3 | |
| Solution 4 | Shah E Rome Wali |
