11

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.

2 Answers 2

17

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 main
$ git branch -f main origin/main

(concerning the last command, see also 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 main
Your branch is up-to-date with 'origin/main'.
nothing to commit, working directory clean
9
  • 1
    But there is no initial add/commit so that initial git push is not going to work. Is that necessary? Commented May 20, 2016 at 20:46
  • Note that git push --set-upstream origin master does not push anything to the remote repository. It only changes the local configuration by "glueing together" your local branch "master" with the remote tracking branch "origin/master". If you are sure that you never will push anything from this repo, or that you have set "push.default" to "simple", you may also omit that statement. Commented May 20, 2016 at 20:51
  • I will definitely never push anything, only pull, so I will omit the statement. But I still need to do an initial add/commit for the git branch -f command to work, right? Commented May 20, 2016 at 20:59
  • If at all, only add your stuff, don't commit! (this would change the local branch and you would get out of sync with the remote repo again). Commented May 20, 2016 at 21:02
  • 1
    ok, and what went wrong for you? Perhaps we all could learn something from that failure? Commented May 22, 2016 at 8:15
4

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)

2
  • 2
    Thanks! I wasn't sure if I could omit the push line on the other answer because I prefer not to touch the correct copy on remote.
    – Maarten
    Commented Oct 27, 2020 at 13:34
  • when I run git pull after that I get this error: error: Your local changes to the following files would be overwritten by merge: test.html Please, commit your changes or stash them before you can merge. Aborting
    – R0l
    Commented Jun 9, 2023 at 23:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.