Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you sync existing code existing repository using git

Tags:

git

Here's my situation:

  1. I have an existing git repository
  2. I have code on an active staging server that has been manually kept in sync with what's in the repository, but is not currently under version control.
  3. I want to put that code under version control, WITHOUT blowing it away and doing a git clone, as there would be significant re-configuration that would be needed. I want to start being able to just do git pull to start updating it from that moment forward.

I know from having done testing of this locally (git init, git remote add origin) that it will see all files in that folder as untracked. If I were to add and commit them, then merging origin/master would cause a conflict with all of them.

Is there a way to seamlessly "insert" an existing repository alongside an existing installation to put it under version control? Note that any upload folders and configs are NOT under version control, so there is no worry about conflicts with these.

like image 946
AgmLauncher Avatar asked May 20 '16 20:05

AgmLauncher


2 Answers

I have a submodule in node_modules, and my approach to attach it to the repo after npm i is slightly different:

git init .
git remote add origin [url of the repo]
git fetch origin
git branch -f master origin/master
git reset .

(without the push step, as it gives error: failed to push some refs error and a git reset . call in the end)

like image 42
Klesun Avatar answered Nov 15 '22 12:11

Klesun


This is how I would do it:

$ git init .
$ git remote add origin [url of the repo]
$ git fetch origin
$ git push --set-upstream origin master
$ git branch -f master origin/master

(concerning the last command, see also Git: move branch pointer to different commit without checkout and https://www.kernel.org/pub/software/scm/git/docs/git-branch.html which describes the idea).

This works, as git is comparing the SHA1 value of the files in your working directory, which is based on the filename and the content. To prove that it worked, you may run git diff which will not show any difference (assuming that you really kept the directory in sync with the repo). We can also prove this:

$ git hash-object testfile.txt
77356c31404cfbc88ab78de4025ebd609c022927

$ ls -a .git/objects/77/*
.git/objects/77/356c31404cfbc88ab78de4025ebd609c022927

Note that we now have

  • a proper working directory and
  • a proper branch configuration, which points to the same content.

However, the staging area still is out of sync. That is why another

$ git add -A 

is required (Keep in mind that this operation may become costly if you have many or large files, as all files will be added to the repositories' index). This will synchronize your staging area as well. A git status then will yield:

On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
like image 122
EagleRainbow Avatar answered Nov 15 '22 12:11

EagleRainbow