Skip to content

Instantly share code, notes, and snippets.

@davisford
Last active February 9, 2022 13:39
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save davisford/5039064 to your computer and use it in GitHub Desktop.
Save davisford/5039064 to your computer and use it in GitHub Desktop.
git clone into non-empty directory

Let's say you start a project locally, and do some editing.

$ mkdir -p ~/git/foo && cd ~/git/foo
$ touch NEWFILE

Now you decide you want to create a new github repo and track it, but the directory is non-empty so git won't let you clone into it. You can fix this, thusly:

$ cd ~/git/foo
$ git init
$ git remote add origin git@github.com:yourname/foo.git
$ git fetch
$ git branch master origin/master
$ git checkout master
$ git add -A
$ git commit -m 'first commit'
$ git push
@kyo00710
Copy link

Hi I used it, but not success

After I use git remote command, It tells me that the origin is existed, and I can't do anything like you said.

Please help me.

Thanks,

kyo00710

@figa12
Copy link

figa12 commented Nov 2, 2013

Exactly what I needed, works like a charm.

Thanks!

@TwisterMc
Copy link

This helped a lot. For my repositories, I didn't have to do get brach master origin/master or git checkout master as no branches existed. So I skipped those two steps and it worked great.

@wmgodyak
Copy link

thanks!

@michael-o
Copy link

Awesome magic!

@nanusdad
Copy link

Thanks !

@evanlalo
Copy link

Works like a charm!

@glinders
Copy link

If files that are tracked in the source location, already exist in the target directory, git will complain:

$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
        file1
        file2
        ...
Please move or remove them before you switch branches.
Aborting 

To force git to overwrite these files, use git checkout -f master

@ericksuryadinata
Copy link

thanks for this

@lakostin
Copy link

THNX!

@samjross
Copy link

samjross commented Feb 18, 2020

If files that are tracked in the source location, already exist in the target directory, git will complain:
To force git to overwrite these files, use git checkout -f master

Thanks dude, @glinders , you helped me develop my workflow for cloning an existing Laravel repo into a new Laravel project on a new server:

assuming your Laravel directory is in ~/laravel:

cd ~/laravel
rm -rf .git
git init
git remote add origin https://username@bitbucket.org/user/project.git
git fetch
git checkout -f master

@xniti3x
Copy link

xniti3x commented Aug 23, 2020

If files that are tracked in the source location, already exist in the target directory, git will complain:
To force git to overwrite these files, use git checkout -f master

Thanks dude, @glinders , you helped me develop my workflow for cloning an existing Laravel repo into a new Laravel project on a new server:

assuming your Laravel directory is in ~/laravel:

cd ~/laravel
rm -rf .git
git init
git remote add origin https://username@bitbucket.org/user/project.git
git fetch
git checkout -f master

puuh this works thx a lot !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment