1

My question is a direct followup to how to create a new git repository from an existing one. I have followed the procedure there to "git --bare init" and then "git push ssh://my_host/new_repo +topic1:master". The output from the second step looks like it is copying appropriately, but the new repository does not contain any of the source files and "git status" gives the error "fatal: This operation must be run in a work tree". The anonymized log is below. What am I missing? (In case it is not obvious, I am a git newbie.)

> mkdir foo
> cd foo
> setenv GIT_DIR ./.git
> git --bare init
Initialized empty Git repository in /xxx/foo/.git/
> ls -A
.git
> cd ../bar
> git push ssh://127.0.0.1/xxx/foo +for-pr:master
Counting objects: 10171, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6647/6647), done.
Writing objects: 100% (10171/10171), 7.05 MiB | 9.62 MiB/s, done.
Total 10171 (delta 5461), reused 4992 (delta 2529)
remote: Resolving deltas: 100% (5461/5461), done.
To ssh://127.0.0.1/xxx/foo
 * [new branch]      for-pr -> master
> cd ../foo/
> ls
> git status
fatal: This operation must be run in a work tree
slcasner
  • 13
  • 4

1 Answers1

0

You have created a "bare" repository which contains only the versioning information in a .git subfolder. You can see this folder with ls -a. (File and folder names starting with . are considered "hidden" in the Linux world.)

The bare repo does not contain any local copy of the files. This is the whole point of a bare repo. This blog article has more detailed information about bare git repos.

Note that the command git status cannot be run with a bare repo because it compares the working directory with the current branch.

If you want a local working directory, then remove the --bare when you initialize the repo:

git init

my question is how to make a new project based on (part of) an old one, keeping its history but then building independently from there.

I 100% disagree with the accepted answer in the link you gave in your original answer. A bare repository is not the correct tool for your purpose, nor for that of the person who asked that question.

Instead, the intended usage of Git is to create a new branch in your existing repo for continued development.:

git checkout -b my-new-branch

You can delete all of the other branches if you no longer wish to keep their history.

If you want a completely new repo to work in, then you should clone the existing one:

cd <parent folder where you want the new repo>
git clone <URL or path to original repo> foo
cd foo

Now foo will contain the copy and you can make a new branch as explainedearlier.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks, the blog article is helpful (man git-init wasn't). But the answers to the earlier question to which mine is a followup explained that the --bare is required to allow the push to work. Indeed I tried just "git init" and saw the long, explanatory failure message when attempting to do the push. So my question is how to make a new project based on (part of) an old one, keeping its history but then building independently from there. – slcasner Feb 17 '18 at 00:23
  • @slcasner See my edited answer for a more appropriate solution to that question. – Code-Apprentice Feb 17 '18 at 02:11
  • 1
    You definitely answered my "what am I missing" question. I am familiar with using branches for staging new development, but that does not fit my use case here. You are right that I should clone the existing repo, then I can prune out the parts that are not relevant for the new project. Answers for related questions in https://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository and https://stackoverflow.com/questions/3448796/how-to-copy-git-repository-including-only-one-subfolder tell about pruning approaches. – slcasner Feb 17 '18 at 03:17
  • @slcasner For your purposes, the question that you linked in your OP is going the wrong direction by pushing code from the old repo into the new one. Instead, you should work in the opposite direction and pull code from the old into the new. There are several ways to do this. The most straightforward is with `git clone`. – Code-Apprentice Feb 17 '18 at 03:20