20

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?

Community
  • 1
  • 1
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79

6 Answers6

31

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> .
rfay
  • 9,963
  • 1
  • 47
  • 89
  • 2
    Or `git clone --no-checkout myrepo` =) – Xupypr MV Oct 22 '17 at 13:55
  • 2
    what if your directory is not empty? – Allan Bowe Dec 05 '17 at 12:07
  • 1
    If your directory is not empty, git won't do the clone. But if you want what's in the directory, just clone into an empty directory and then copy the files you had into the directory where you cloned. – rfay Dec 06 '17 at 16:34
  • 1
    It doesn't restore the index, so if you just move the `.git` directory into the working tree `git status` output will be incorrect. – EvgenKo423 Apr 24 '21 at 16:05
7

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

Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
  • 1
    the only issue w/ this method is that you haven't had a chance to checkout a specific branch and a default clone will have the 'master' branch checked out which isn't what you might be wanting to compare your local file set against – g19fanatic Aug 17 '16 at 16:20
  • Since you just need the .git folder and not the whole working directory, make sure that you clone it using --bare option. – Eissa N. Oct 08 '16 at 17:39
  • 1
    @EissaN. With `--bare` option remote-tracking branches are not created, so you can't push and compare against them. – EvgenKo423 Mar 15 '21 at 18:25
3

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>
EvgenKo423
  • 2,256
  • 2
  • 16
  • 23
2
  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).
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
g19fanatic
  • 10,567
  • 6
  • 33
  • 63
  • Actually I solve it right now xD. I've cloned the repo to a diiferent directoty and copied the `.git` directory. Everything is ok now! – Alejandro Alcalde Aug 17 '16 at 14:54
  • Should I remove my question or post an answer to my Question? – Alejandro Alcalde Aug 17 '16 at 14:56
  • 4
    @algui91: Because there is an up-voted answer, you can't delete the question. You may choose to post your self-answer though there will be a delay (a couple of days, I think) before you can mark it accepted. OTOH, what you say you did closely matches what this answer suggests; you should probably just accept this answer. – Jonathan Leffler Aug 17 '16 at 14:59
  • Yes, two days. Thanks! – Alejandro Alcalde Aug 17 '16 at 15:00
1

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
Redsandro
  • 11,060
  • 13
  • 76
  • 106
1

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.

Yao Yuan
  • 350
  • 3
  • 11