Alejandro Alcalde
Alejandro Alcalde

Reputation: 6220

Clone only the .git directory of a git repo

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)

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?

Upvotes: 23

Views: 26421

Answers (6)

EvgenKo423
EvgenKo423

Reputation: 3010

As has been said in another answer, there is a --no-checkout option of git clone command, but it's not a direct or complete solution to the problem in question:

  1. it refuses to clone into non-empty directory;
  2. it doesn't restore the index (as there should be no files in a directory).

So if you'll just move the .git directory into your working tree and run git status you'll get an incorrect result:

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
        deleted:    Home.rest

Untracked files:
        Home.rest

With that being said, there are two ways to solve the problem:

  • Clone using temporary directory:
    $ git clone -n <repo_url> tmp
    $ mv tmp/.git/ . && rm -d tmp
    $ git read-tree HEAD
    
  • Imitate clone in existing directory with a working tree:
    $ git init
    $ git remote add -m master -f origin <repo_url>
    $ git reset refs/remotes/origin/HEAD
    $ git branch -u origin
    

If there are more people working with the same repository (there are some remote changes as well), you'll be having a hard time figuring out which state your local repository was in.

On the other hand, if you're sure there are no local changes it's easier to simply delete your working tree and make a full clone:

$ rm -rf <local_repo_dir>
$ git clone <repo_url> <local_repo_dir>

Upvotes: 4

Yuan
Yuan

Reputation: 370

if I have a directory named local_directory, I create a new repository in github named remote_local_directory. I want to push local_directory to remote_directory.

I use these commands

git clone --bare ${remote_directory}.git local_directory/.git
cd local_directory
git init
git add .
git commit
git push --set-upstream origin master

git clone --bare only clone .git directory and git init will reinitialize local repository

use other git commands like local repository is related to the remote.

Upvotes: 1

Redsandro
Redsandro

Reputation: 11356

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

Upvotes: 0

rfay
rfay

Reputation: 12795

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> .

Upvotes: 36

Alejandro Alcalde
Alejandro Alcalde

Reputation: 6220

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

Upvotes: 8

g19fanatic
g19fanatic

Reputation: 10931

  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).

Upvotes: 2

Related Questions