14

I started using Git/SourceTree/Bitbucket lately. Can I move a local Git repository to elsewhere within my hard drive safely?

Because some of the large files are ignored by Git, I cannot really pull the repository from a remote repository to get the exact copy of all the files.

If I simply move or copy the home directory that contains .git to elsewhere, would it cause any mess to Git? If the absolute paths matters to Git, I guess it would, but I'm not sure.

Kouichi C. Nakamura
  • 850
  • 3
  • 10
  • 25
  • There are two questions here -- "can I move?" and "will moving cause a problem?". The answers are yes and no respectively. Try asking just one question rather than the same question with inverse answers. – patthoyts Feb 18 '14 at 11:02
  • Possible duplicate of http://stackoverflow.com/q/7949036/291641 – patthoyts Feb 18 '14 at 11:07

1 Answers1

25

No, moving or copying the .git/ folder will not cause problems for git; git is not connected to your repo in any way, and git doesn't care about absolute paths, only relative paths inside the project folder.

Git is just an executable on your machine. It can run anywhere, as long as it's in your path. I often drag my various .git/ folders to different places, or even onto my thumb drive (usually I drag the whole repository folder, but I've also just brought the .git/ folder by itself). The nice thing about git is that literally everything about your repo is in that folder. You don't even need to bring the working tree files with you. If you drop the .git/ folder - or a copy - into another folder, you can git checkout master in that folder, and git will rebuild the working tree there.

This is the great thing about everything being data. Git is not attached to your repository at all. You just call git to do work on your repository to create or change the data that exists there, and then it lets go entirely again.

Keep in mind you can clone your own local repo:

$ git clone oldrepo newrepo

Git will copy the oldrepo to the newrepo (newrepo can be a path to anywhere on your local filesystem), creating that folder in the process. Also, newrepo will have a remote added automatically - origin - that points back to oldrepo, so you could do work in oldrepo, and fetch it in over in the newrepo folder. If you don't want that remote (it's harmless) you can just do git remote rm origin in the new repo. If you want to hook the copied repo back up to a server online, just do git remote add origin <url>. If you're not sure what the URL was, do a git remote -v in the oldrepo and copy the URL from there.

Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
  • 2
    This could be confusing -- is the "no" answering the question in the title? or the question in the last but one sentence. – patthoyts Feb 18 '14 at 11:00
  • Thanks. I cleaned up the confusing bits, hopefully. – Gary Fixler Feb 18 '14 at 11:04
  • Thank you for the immediate and great answer. Just to confirm things, what about the files set to be ignored by a `.gitignore` file? I guess `git clone` is meant to ignore those files, so `newrepo` won't have them. Am I right? – Kouichi C. Nakamura Feb 18 '14 at 11:36
  • Untracked files won't come over. That includes files that aren't in your repo because they've been ignored. If the ignored file exists in earlier commits, those earlier, hashed versions would come over, but any version in your working tree that isn't committed, ignored or not, will not come over. Also, your objects folder can hold onto many things that are no longer referenced by any commits for awhile, waiting to be garbage collected. None of those will come over either. You'd be able to get to them, tag them, or branch at them in the old repo, but not in the clone. – Gary Fixler Feb 18 '14 at 21:26