Home > Back-end >  git bare repo: deletions commited after --mirror vs deletions not staged for commit after deleting a
git bare repo: deletions commited after --mirror vs deletions not staged for commit after deleting a

Time:12-09

Ultimately as of now I want a clean way to return to compact (e.g. bare) repo ready to be checked out (Ok, for pedantic ones: after additional core.bare false) to any branch. I've read top answers to How to convert a normal Git repository to a bare one?. Using clone looses config entries as mentioned in comments, below is the problem after trying to use accepted answer. Maybe there is an easy trivial fix to that and that is why I could not find it mentioned in the comments.

TL;DR

I'm trying to make sense of info related to bare git repos.

  1. To clone all branches: How to clone all remote branches in Git?:

Executed and it worked:

git clone --mirror https://github.com/vmatare/thinkfan.git path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout master # checkout devel also works with freshly cloned repo

Man git-clone:

   --mirror
       Set up a mirror of the source repository. This implies --bare.
       Compared to --bare, --mirror not only maps local branches of the
       source to local branches of the target, it maps all refs (including
       remote-tracking branches, notes etc.) and sets up a refspec
       configuration such that all these refs are overwritten by a git
       remote update in the target repository.

Now before git checkout anybranch I got .git only folder and:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    .github/workflows/ccpp.yml
    deleted:    CMakeLists.txt
    deleted:    COPYING
    deleted:    README.md
    ...

"deleted" output in green. That is deletions are in index and ready to be committed (as per output and explained in per https://unix.stackexchange.com/questions/458354/git-status-coloring-deleted-files).

  1. To convert to bare: https://stackoverflow.com/a/2200662/14557599

Executed:

cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true

That is deleting all except .git and changing core.bare config value to true.
After that

git config --bool core.bare false

BTW is

git config --bool core.bare true
git config --bool core.bare false

Amounts to nothing or some internal status is changed? Anyway, doing both means I followed the upvoted accepted answer to make bare repo. And doing clone --mirror I've also made bare repo. But now "deleted" are in red and output is "Changes not staged for commit":

git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    deleted:    .github/workflows/ccpp.yml
    deleted:    CMakeLists.txt
    deleted:    COPYING
    deleted:    README.md
    ...
    no changes added to commit (use "git add" and/or "git commit -a")

Why is there such a difference between originally cloned repo and then again converted to bare?

I've tried to read comments to the answer to make bare repo but have not noted mentioning that issue.

If now I do git add *, then apparently status becomes same as when I just cloned with --mirror:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    .github/workflows/ccpp.yml
    deleted:    CMakeLists.txt
    deleted:    COPYING
    deleted:    README.md

But checkout after than does not work.

$ git checkout devel
error: Your local changes to the following files would be overwritten by checkout:
    CMakeLists.txt
Please commit your changes or stash them before you switch branches.
Aborting

How to see the difference between repo "status" right after clone --mirror and after deleting all except .git to see why checkout works in 1st case but not the second?

CodePudding user response:

Changing core.bare from true to false is not supported. Don't expect anything to work.

If you know the innards of Git, you can make it work. (The instructions in the other SO answers were, at least in part, written by those who know how to make it work.) However, you then know why not to combine this with --mirror.

Having run git checkout, you created an index (aka staging area) in your bare repository. You'll need to remove the index (rm .git/index). That will fix things up, at least temporarily. But don't go converting bare to non-bare and back like this: it's not supported.

Consider leaving the bare repository bare: you can use git archive to get any commit you want from the bare repository. See the git archive documentation for details. The git branch and git log commands (and many others) work in a bare repository, allowing you to find hash IDs to supply to git archive.

  • Related