Pedro Braz
Pedro Braz

Reputation: 2401

How to revert git working repo initiated over bare repo?

I had a bare repo on a windows 10 folder. This bare repo has been used for some time and has dozens of commits. Accidentally, today, I ran

git status
fatal: this operation must be run in a work tree
git init
git add .
git commit -m "initial commit"

on it.

Thus I created a working repo over my previous bare repo. Now if I try to clone it I end up cloning the files from the working repo, which are the original files from the bare repo, which are not working files.

I want to delete this working repo I initiated while keeping the original bare repo untouched.

How should I do that ? I can see the files that I changed by looking at their timestamps, but should that be enough ? I'm afraid I might've overwritten something and deleting will jeopardize it all.

Thanks in advance.

Upvotes: 1

Views: 298

Answers (1)

torek
torek

Reputation: 488183

A "bare" repository looks like this:

$ git init --bare
Initialized empty Git repository in .../tmp/tbare/
$ ls -AF
HEAD            config          hooks/          objects/
branches/       description     info/           refs/

You may have a few additional files and/or directories, including index and logs, depending on what you have been doing with it.

Running git init in this bare repository adds a .git directory, so that you now have two Git repositories: the bare one in the current directory, and the .git non-bare one for which the current directory is a work-tree:

$ git init
Initialized empty Git repository in .../tmp/tbare/.git/
$ ls -AF
.git/           branches/       description     info/           refs/
HEAD            config          hooks/          objects/

Nothing has changed in the bare repository, but now git ... finds the .git directory and uses that. Should you wish to use the bare repository, you must tell Git not to look at .git but rather at .:

$ git --git-dir=. status
fatal: This operation must be run in a work tree
$ git status | head -1
On branch master

If you simply remove the .git directory, Git goes back to not being able to find .git and instead using . which gets it the bare repository:

$ rm -rf .git
$ ls -AF
HEAD            config          hooks/          objects/
branches/       description     info/           refs/
$ git status
fatal: This operation must be run in a work tree

Everything is now back the way it was before—you've deleted the additional repository (and all of its commits, since they contained nothing useful that was not also contained in its work-tree, which was the original repository).

For warm fuzzy feelings, before removing .git, run git --git-dir=. log --all --decorate --oneline --graph. You should see all the commits that the bare repository held before you did any of this.

Upvotes: 1

Related Questions