'How to add add existing directory to version control in existing repo without modifying files?

I have to put a pretty big project under version control, which has two versions (dev and prod). These two were kept in "mostly sync" by copying files manually over the years, but - obviously - they diverged from each other pretty heavily.

The dev version is already in git, and now I need to put the production version under version control, too, but without modifying any existing files to avoid loss of code. My plan is to first gitignore all files in the production directory and as I work on them to remove any differences manually, I slowly add them back until all files are added and I can just normally git pull to get any code changes.

The method I tried is*:

  1. go to production directory
  2. git init
  3. git fetch --all
  4. git checkout origin:release -- .gitignore to get normal gitignore file
  5. add the line **/* to .gitignore
  6. git checkout release

If I don't check out the existing gitignore and just use a clean .gitignore with **/* as the only line, git still updates the files.

I thought that because of the newly added line in .gitignore my working directory would survive the checkout unharmed, but I was wrong.

Is there a way to fetch everything and go to the release branch without changing my working directory?

* I used a copy of the production directory to avoid accidentally destroying anything



Solution 1:[1]

The dev version is already in git, and now I need to put the production version under version control, too,

So:

cd /path/to/dev/repository
git switch -c prod dev
git --work-tree=/path/to/current/prod add .
git status

That way, you can quickly see what is modified (added/removed) by the prod file in a working tree initialized from dev.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC