Skip to content

Instantly share code, notes, and snippets.

@davisford
Last active February 9, 2022 13:39
Show Gist options
  • 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
@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