Clone only the .git directory of a git repo

送分小仙女□ 提交于 2021-01-19 22:22:05

问题


I have a repo in sync with google Drive, but I had the .git directory ignored so it is now uploaded to Google Drive.

Recently I formatted my Gentoo machine and after I had all Google Drive files synced again I realized the .git directory was not there.

The problem is I do not remember if I had some unstagged/uncommited changes in local not pushed to github.

I have been searching but I only found answers for the opposite question (Cloning without the .git directory)

  • How do I clone a subdirectory only of a Git repository?
  • Cloning only a subdirectory with git
  • How to clone git repository only some directories?
  • Is it possible to clone only part of a git project?
  • Git Clone: Just the files, please?
  • Git clone without .git directory

I do not want to make a git clone of my repo until I am sure that possible local changes are not going to be loss.

Is there any way of cloning only the .git folder and then push any local changes that I may have in my machine?


回答1:


The one-step solution would be:

git clone --no-checkout <repo_url>

or if you already have an empty dir for it,

cd myrepo
git clone --no-checkout <repo_url> .



回答2:


I solve it. It was an easy process:

  1. I've cloned the repo to a different location eg (in /tmp)
  2. I've copied the .git folder into my original repo folder
  3. I did git status on my original repo folder and all the local changes were there.

Hope it helps others




回答3:


  1. Do a git clone to a different folder on your machine from your online repo
  2. Checkout the branch that you're interested in comparing your local files against.
  3. Then copy/paste your folders contents over top the new clone.
  4. Check to see whats changed (if anything and commit as you would).



回答4:


Basically what you want is a fresh .git directory without any changes to files, so you can do git status and see if anything was changed.

Expecting your pwd is your project directory with an old .git directory that has the origin for the repository set up, you could run the following command:

mkdir -p /var/www/tmp/_delme \
    && git clone --no-checkout `cat .git/config | grep url | awk -F' = ' '{print $2}'` /var/www/tmp/_delme \
    && rm -rf .git \
    && mv /var/www/tmp/_delme/.git . \
    && git add -A
    && rm -rf /var/www/tmp/_delme


来源:https://stackoverflow.com/questions/38999901/clone-only-the-git-directory-of-a-git-repo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!