5

I'm just starting to use Git, and I have a question:

When I create a new repository I get a folder in which I should put all the files (this folder contains .git folder). Is this folder completely self-contained?

Let's say I've created a new repository and worked on the project for a while (doing commits). If I would take that folder and move it to another location (say another disk) would it still work? What if I move it to another computer? Could I have the repository/folder on an external device (external HDD or USB stick) and work with it on several computers?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
haagel
  • 2,646
  • 10
  • 36
  • 53
  • Your working files and folders go side by side with the `.git` folder. The `.git` folder itself then contains all your committed data, logs, status etc. If you have a `--bare` repository then it is for storage only and has no working files. The bare repos are normally `remote`s. – Philip Oakley Jan 06 '13 at 16:40

6 Answers6

7

Yes. That is the local repository, and it contains all the information about the repository.

The only thing that's not included is the global configuration.

See this free Git documentation for more details. Quoting from there:

Git creates the .git directory, which is where almost everything that Git stores and manipulates is located. If you want to back up or clone your repository, copying this single directory elsewhere gives you nearly everything you need.

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
  • 1
    One configuration setting requires special consideration: remote URLs. You can have remote URLs like `git@localhost:foobar.git`, which would obviously not work on other computers. – DCoder Jan 06 '13 at 10:59
  • 1
    True, but then the whole remote repository might be out of reach no matter what the URL is (the original computer remains behind a firewal). The answer is still valid: __this__ repository is self contained, with all its data; external stuff like configured remotes may or may not be reachable from a different location. Similarly, other configurations may reference files specific to the local machine, like a global `~/.gitignore`, custom `diff` tool, and so on. – Sergiu Dumitriu Jan 06 '13 at 11:03
2

The folder itself is self-contained.

Actually, if you have committed everything and don't have local changes, the .git folder itself should be enough. The .git folder is your repository. The rest of the contents of the folder is the checked out working directory. You may not copy those ( especially if there are lots of files ) to other machines.

manojlds
  • 290,304
  • 63
  • 469
  • 417
1

Yes, it contains everything git needs.

And it does work with usb sticks.

aragaer
  • 17,238
  • 6
  • 47
  • 49
1

Yes the folder is self contained, but you do need to have git installed if you want to do git operations on it from a different machine

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
1

Almost.

  • Serge already mentioned the global config.
  • hooks, for obvious security reason, aren't part of the repo (they are not replicated by a push or clone or a bundle), even though they are store within the .git folder.
    In your case (duplication of the .git directory), a hook working a local machine might not work on another one (because it is, for instance, a perl script, and perl isn't installed on the other machine).
  • submodules reference external repos (which might not be accessible once you copy your git repo over the other machine)
  • git attributes can reference scripts only stored on one machine, and not accessible on the other.
  • git remote address (stored in the local config) can reference repos no longer accessible once you copy your repo elsewhere.

Git will recognized the moved repo as a git repo, so you can say it is self-contained in that regard.
I prefer moving my repo as a git bundle (only one file to move).

But if you depend on any other (script/tools/other repos) for your repo to be complete and actually allowing you to work with it, you might need to consider moving other external elements as well.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Git folder may be sel contained but doesn't always act like it.

For example, today I struggled trying to copy a recovered .git folder and files to a new Virtual Box system after a hard drive corruption of repo stored on external usb drive inside Virtual Box/Lubuntu got disconnected accidentally before proper shutdown (fixed with chkdsk /f /x) .

As far as I could tell, all files seemed intact, and git log appeared normal except headings were in red. But all attempts to push to the newly created remote repo were met with various errors such as "cannot traverse parent" for various commit #'s and "are you sure you have permission?" Also, there were some messages about "symlinks" when files were originally copied from my recovered drive to the newly created VM. Ithought it might be a permissions issue but all files were read/write all. Maybe .git has internal checks? End result I only succeeded in push to remote by deleting .git and initializing the local repo (basically a hard reset)...all history lost (was only a class project so was local only beforehand, now Bitbucket will prevent recurrence I hope.
But I thought .git folder was self contained.

Dell Anderson
  • 143
  • 2
  • 7