Reputation: 6220
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
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:
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:
$ git clone -n <repo_url> tmp
$ mv tmp/.git/ . && rm -d tmp
$ git read-tree HEAD
$ 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
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
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
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
Reputation: 6220
I solve it. It was an easy process:
/tmp
).git
folder into my original repo foldergit status
on my original repo folder and all the local changes were there.Hope it helps others
Upvotes: 8
Reputation: 10931
git clone
to a different folder on your machine from your online repoUpvotes: 2