194

What happens to an existing git repository when you issue git init again?

I created a repository with git init. Created a file, add, commit. Check the status (nothing to commit). Then created another file, check the status and I can see it's untracked as expected.

Then, say by mistake, I run git init again and I get Reinitialise existing Git repository message.

Tried git status, but it shows the same. So what really happens?

Can reinitialising an existing git repository this way be harmful or helpful? Why can we git init inside an existing repository?

Mr. L
  • 3,098
  • 4
  • 26
  • 26
  • FWIW using Xcode I first created the **local** repository for the *first* time, then when I went to Github.com to create a new remote repository. Which I did, then to link my local repository to my remote...I was following its steps (provided on Github.com), its first step is `git init` which I did. Presumably it was the 2nd time *after* Xcode automatically had created it – mfaani Feb 07 '17 at 22:35

3 Answers3

248

It seems safe and should not overwrite anything important.

From the git docs:

Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates.

mit
  • 11,083
  • 11
  • 50
  • 74
coreyward
  • 77,547
  • 20
  • 137
  • 166
118

It seems safe and should not overwrite anything important.

Quoted from the git init documentation:

Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates.

mit
  • 11,083
  • 11
  • 50
  • 74
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
54

Since v1.7.5 (b57fb80a7), git init in an existing repo has also allowed moving the .git directory:

The primary reason for rerunning 'git init' is to pick up newly added templates (or to move the repository to another place if --separate-git-dir is given).

'Picking up newly-added templates' means that any templates which have not already been copied from the template directory will now be copied into the existing git directory.

'Moving the repository to another place' means that, if --separate-git-dir points to somewhere else, the existing .git directory will be moved there and replaced by a link.

Joe
  • 29,416
  • 12
  • 68
  • 88
  • I didn't understand "pick up newly added templates" part. Could you tell me what this mean? – dixhom Nov 06 '21 at 11:19