Pranjal Mittal
Pranjal Mittal

Reputation: 11460

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.

How can I do this?

Upvotes: 185

Views: 313497

Answers (7)

Keith Knauber
Keith Knauber

Reputation: 802

In the end none of these answers worked for me. OP specifically wants "no loss of git history". Most of these answers blow away git history.

I found the answer on another thread, specifically the answer that says to use "git mv" command, which I was not aware of.

Move file and directory into a sub-directory along with commit history

  • To move tracked files, use "git mv".
  • To move untracked files, just move them using "mv".

This process preserves all your history, including uncommitted changes. No need for a clean working copy.

So to answer OPs question, Starting out in the original folder, make another folder with the same name!

$ /Users/me/gitrepo1> mkdir gitrepo1
$ /Users/me/gitrepo1> ls
 .git/
 .gitattributes
 .gitignore
 gitrepo1/
 exampleFile
 exampleDir/
 exampleUntrackedFile
$ /Users/me/gitrepo1> git mv exampleFile exampleDir gitrepo1
$ /Users/me/gitrepo1> git mv exampleUntrackedFile gitrepo1
 fatal: not under version control, source= exampleUntrackedFile, destination= gitrepo1/exampleUntrackedFile
$ /Users/me/gitrepo1> mv exampleUntrackedFile gitrepo1
$ /Users/me/gitrepo1> git status
    renamed:    exampleFile -> gitrepo1/exampleFile
    renamed:    exampleDir -> gitrepo1/exampleDir
Untracked files:
    gitrepo1/exampleUntrackedFile

And finally, rename to newrepo

cd ..
mv gitrepo1 newrepo

Upvotes: 2

gharel
gharel

Reputation: 573

I am no expert, but I copy the .git folder to a new folder, then invoke: git reset --hard HEAD

Upvotes: 17

samsoft
samsoft

Reputation: 56

for me on windows it works with xcopy. copy command does not copy the hidden files. from root folder run this command:

xcopy subfolderofRepo .\ /s /e /h

Upvotes: 0

Shah E Rome Wali
Shah E Rome Wali

Reputation: 77

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.

Upvotes: 0

emremrah
emremrah

Reputation: 1765

To do this without any headache:

  1. Check what's the current branch in the gitrepo1 with git status, let's say branch "development".
  2. Change directory to the newrepo, then git clone the project from repository.
  3. Switch branch in newrepo to the previous one: git checkout development.
  4. 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 with rsync of 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.

Upvotes: 7

david.pfx
david.pfx

Reputation: 10863

It's even simpler than that. Just did this (on Windows, but it should work on other OS):

  1. Create newrepo.
  2. Move gitrepo1 into newrepo.
  3. Move .git from gitrepo1 to newrepo (up one level).
  4. Commit changes (fix tracking as required).

Git just sees you added a directory and renamed a bunch of files. No biggie.

Upvotes: 54

Shahbaz
Shahbaz

Reputation: 47583

It's very simple. Git doesn't care about what's the name of its directory. It only cares what's inside. So you can simply do:

# copy the directory into newrepo dir that exists already (else create it)
$ cp -r gitrepo1 newrepo

# remove .git from old repo to delete all history and anything git from it
$ rm -rf gitrepo1/.git

Note that the copy is quite expensive if the repository is large and with a long history. You can avoid it easily too:

# move the directory instead
$ mv gitrepo1 newrepo

# make a copy of the latest version
# Either:
$ mkdir gitrepo1; cp -r newrepo/* gitrepo1/  # doesn't copy .gitignore (and other hidden files)

# Or:
$ git clone --depth 1 newrepo gitrepo1; rm -rf gitrepo1/.git

# Or (look further here: http://stackoverflow.com/q/1209999/912144)
$ git archive --format=tar --remote=<repository URL> HEAD | tar xf -

Once you create newrepo, the destination to put gitrepo1 could be anywhere, even inside newrepo if you want it. It doesn't change the procedure, just the path you are writing gitrepo1 back.

Upvotes: 177

Related Questions