23

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?

bato
  • 547
  • 1
  • 3
  • 12
  • 1
    You mean add it? What do you mean by associate? – Collin Oct 24 '12 at 15:19
  • You can set up relationships between git repositories. You can't set up a relationship with unmanaged code and a remote repository - you'd need to put it into a local repository. – itsbruce Oct 24 '12 at 15:38
  • A little more detail would help, do you mean new code or existing code that you are overwriting? Give us an example to explain your situation, we are most of the time not mind readers. – JPM Oct 24 '12 at 15:38
  • @JPM sorry, I added some detail at the description – bato Oct 29 '12 at 23:32

4 Answers4

35

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.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • ok, I understand and it could be the solution but my local code is outdated, I need to track my old code to not restore many ignored files that I should do if I clone the repository. But you have suggested the right solution with the option `--hard`. It is the one for me :) – bato Oct 29 '12 at 22:50
  • yes.. add the --hard option to throw your changes. It would be better to `git stash -u` them just in case prior to issuing the reset with --hard. – Adam Dymitruk Oct 29 '12 at 22:56
  • Do you mean to save any files that are different from those in the repository? – bato Oct 29 '12 at 23:14
  • What i don't understand is that if you're planning on doing "reset --hard" why not to clone the repo and be done with it? Especially if there is nothing to commit as you stated in the question? – Eugene Sajine Nov 01 '12 at 19:39
  • @EugeneSajine I have to maintain complex configuration files that are in .gitignore and I would avoid to clone and then restore all configuration files. – bato Nov 07 '12 at 13:43
  • You can clone your repository to any other folder and then add your configuration files and .gitignore to it. To me the situation is overcomplicated for no reason. Git doesn't care where the repo is located, it also does a very good job in keeping files that are not tracked by git intact. So if those configuration files are not tracked by git, then git reset --hard will not touch them. – Eugene Sajine Nov 09 '12 at 03:29
  • I know it seems overcomplicated but I have many and many instances on different servers that have to be ported from svn repository to new git repository and I would write a bash script to automate the transition without that instances stop working. Clone in another folder then copy configurations file then remove old folder and rename cloned folder to old name don't work well instead the @AdamDymitruk solution with `--hard` doesn't interrupt any service – bato Nov 09 '12 at 09:34
  • Thanks for posting this! This actually turned out to be just what I needed in a weird scenario today. – Alan Shutko Jun 10 '14 at 16:31
15

Ok, I post the right answer for my question based on Adam Dymitruk and Eugene Sajine answers. Thanks everyone for the support :)

  • First of all, remove all .svn directories in working copy directory;
  • initialize new git repository in the working copy directory

    cd /path/to/my/project
    git init
    
  • add git remote repository and fetch it

    git remote add origin url-to-your-remote
    git fetch origin
    
  • reset working copy to remote repository

    git reset --hard origin/master
    

At the end local repository will be synchronized with remote repository.

Community
  • 1
  • 1
bato
  • 547
  • 1
  • 3
  • 12
3

If you really mean what you're saying then the sequence will be:

cd {your-project}
git init
git remote add {remote_name} {address}
git fetch {remote_name}

Then you can create a local branch from one of the branches that the remote has for example:

git branch master {remote_name}/master

and then you can add your code:

git add {files}

or

git add .
git commit

UPDATE:

The OP specifies that the code in question is old and there is nothing to commit, therefore it seems that the whole situation is overcomplicated for no reason. OP just should clone the new repository and be done with it. Ignore configurations should be redone in accordance to the project policies (either .gitingore is stored in the repo or not and etc.)

Eugene Sajine
  • 8,104
  • 3
  • 23
  • 28
  • After following the procedure, `git pull` fails with *`fatal: No remote repository specified....`* – jww Sep 09 '16 at 22:32
0

Would the following work? I tried it on one of my projects and it worked.

1) Clone the git repository to a location different from the directory that contains the unversioned code.

2) Copy the .git folder from the directory created as a result of (1) to the directory that contains the unversioned code.

This effectively achieved for me what Adam Dymitruk steps in his answer above.

erase.ego
  • 83
  • 1
  • 3
  • 7