How to return to repository w/out working files (e.g. bare) for compact storage (undo a checkout in git)?

91 Views Asked by At

Note:
In the title of the question I put e.g., not i.e.: the goal is compact storage, not bare repo which is one particular case of that as I've found out seeking the answer.

I've cloned a repository from github, I want to store sources just in case. Used solution from How to clone all remote branches in Git?:

git clone --mirror "$1" "$2"/.git
cd "$2"
git config --bool core.bare false

As a result had only .git with several dozen files. I wanted to see contents, did git checkout (result - thousands of files), now I want to undo and return to compact storage. Neither answers from How do I undo a checkout in git? not web search helped. Naively deleting all checked out files resulted in git registering deletion of files, I want to return to nice initial repository. Workaround would be to backup .git before checkout, but I hope there is a "git way" to return to bare repository.

Added 1:

I followed suggestion of duplicate question (How to convert a normal Git repository to a bare one?). Also guessed

git config --bool core.bare false

needs to be run afterwards to be able to run checkout later right away as before. Answer

git clone [ --bare | --mirror ] path_to_current path_to_clone

looks like working, but loses link to original remote. The other one (moving .git contents to new empty folder and then running git config --bool core.bare true results in following issue:

Original:

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

"Restored"

$ 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
no changes added to commit (use "git add" and/or "git commit -a")

Also:

$ git checkout master
D   .github/workflows/ccpp.yml
$ ls -a
.  ..  .git

Checkout results in empty folder, I cannot see source code I'm interested in.

1

There are 1 best solutions below

4
Martian2020 On

After some reading of first suggested duplicate question and then man pages of git commands, I as of now think the following does what I want:

git switch --orphan some_name # e.g. "empty" name

As a result I get files disappear from my working folder, it is again nice and compact. I have not tested updating repo from remote, but do not see how that additional branch can add problems. BTW it is not listed in output of git branch.

TL;DR

I've read top answers from suggested duplicate question (How to convert a normal Git repository to a bare one?). Answer to clone to new empty folder:

git clone --bare | --mirror path_current path_clone

seems working (but as noted in comments does not preserve configs, e.g. remote.origin.url=), just

git config --bool core.bare false

needs to be run afterwards to move it to same state as original repo (to be ready for checkout).

The accepted one however (moving .git contents to new empty folder and then running git config --bool core.bare true) results in the issue I noted in Added 1 of the question.