Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danmiser/c84aad24421cb4b937bbf0543b848b85 to your computer and use it in GitHub Desktop.
Save danmiser/c84aad24421cb4b937bbf0543b848b85 to your computer and use it in GitHub Desktop.
Git clone into non-empty directory

Thanks to cmcginty from https://stackoverflow.com/questions/2411031/how-do-i-clone-into-a-non-empty-directory

Useful for if you have a local project directory that you want to re-sync with your git repo

git init
git remote add origin PATH/TO/REPO
git fetch
git reset origin/master  # Required when the versioned files existed in path before "git init" of this repo.
# git checkout -t origin/master # Original line, but gave me an error
git branch --set-upstream-to=origin/master   
git ls-files -z --deleted | xargs -0 git checkout --
git add .
git status
git commit -m "Initial checkin"
git push
@danmiser
Copy link
Author

danmiser commented Nov 23, 2020

Or this more simple approach:

git init
git remote add origin PATH/TO/REPO
git pull origin master
git branch --set-upstream-to=origin/master master
git add .
git status
git commit -m "Initial checkin"
git push
# You may have to do a: git reset --hard HEAD

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