How can I safely commit a .git directory

3.6k Views Asked by At

I am working on a git-related software and I have included unit tests in my code.

I have created a test git project (under a subdirectory) to be used by the tests: resources/test-git-project
This test git project is not associated to any remote, but it contains commits/tags/branches.

What is the safest way to commit the contents of resources/test-git-project in git?

3

There are 3 best solutions below

0
On BEST ANSWER

Thanks to Thibault D. answer I found the solution I was looking for:

I have renamed .git to gitdir and it is now commitable:

When I want to perform updates or see the status of the test repo I do the following:

git --git-dir=resources/test-git-project/gitdir --work-tree=resources/test-git-project anycommand

Before performing the unit tests I copy the directory resources/test-git-project under /tmp and rename /tmp/test-git-project/gitdir to /tmp/test-git-project/.git, so that I don't have to mess with --git-dir option on my test code.

1
On

You should not have nested git repositories.

Have one unique repo for the code and the test (kind of common), or have two separate repos, but at the same hierachical level in your filesystem, not nested on into another.

1
On

You could either zip it or add it as a git submodule, depending on how often you want to change the contents and what parts of the .git folder are important for your tests.

Adding it as a submodule will make it easier to access the .git directory but will not really commit the content of the directory and manage revisions of it. You will even have different contents on different clones because of when you perform git pull (if you add/remove branches, if you rebase branches, if you locally create dangling objects, etc...)

You could also probably do something with the --git-dir option .