Cloning a git repo into a copied directory (adding git to a non-empty directory)

Say you have a copy of a git repo, for example :

cp -R /path/to-git-repo-folder /somefolder

cd /somefolder

If the files are different in your case, this will still work, just read the checkout step.

Set up git for the directory :

git init
git remote add origin
git config --global credential.helper cache # this saves your https password for 15mins
git fetch
git reset origin/master

This is now ready to go - any extra files will be 'untracked', which is fine, but there will probably also be file differences. To see these do a :

git status

1) to discard changes that are different than on the remote repo you can do a checkout. This will reset the file to the version on the remote repo.

git checkout -- readme.txt
git status

2) to keep the change and update the remote repo, do a git add/commit/push :

git add <filename>
git commit -m "updated file"
git push -u origin master

Note:  from now onwards, as you've set the 'upstream' with that push, you can just do a "git push" or "git pull".

Once this is done, the directory will be in-sync with the repo! All ready for updates and whatever else.

1) To make any edits, just follow the usual git standard practice of add/commit/push as usual.

vi readme.txt
git add readme.txt
git commit -m "updated readme.txt with more extra txt via somefolder"
git push -u origin master

2) To obtain any changes that have been made to remote - do a fetch/pull :

git fetch
git pull


Leave a Reply