How can I associate local unversioned code to git remote repository?

bato picture bato · Oct 24, 2012 · Viewed 12k times · Source

I need to associate a clean unversioned code to an existing git remote repository.

I explain better my situation. My project is moved from svn to git. I have a svn local working copy with old code (nothing new to commit) that has to be syncronized with the new git repository. I could clone the repo but I should restore many ignored configuration files by hand and I would avoid it.

So I removed all .svn directories and I would track my code with the git remote repository.

Is there a way to do it?

Answer

Adam Dymitruk picture Adam Dymitruk · Oct 24, 2012

I would commit your working directory to ensure that it exists in history before fetching. (ensure you are in the projects root folder)

git init
git add -A
git commit -m "my latest'

now we have it

git remote add origin url-to-your-remote
git fetch origin

now we have the history from the remote

git reset origin/master

now our current commit is the latest from the remote

git add -A
git commit -m "Committed my state"

now we use our current working directory as the snapshot for the next commit.

git push -u origin master

push up your changes and track the branch.

the key is the reset command with no --hard option. It keeps your working folder the same while pointing the branch to what you got from the remote.