Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

Git clone + composer create project

Is it possible to use composer create-project for directory that is already a Git repository?

I have such a situation - I want to create a Laravel project and I want to have this directory under Git version control on my local machine.

If I first clone my local repository (which is empty) and then run composer create-project ... I will have error that directory already exists. The same if I first create project and then try to git clone to this repository.

At the moment the only workaround I found is cloning local repo and running composer create-project for not existing directory and then manually copying files from this new directory to the directory where I cloned the repo. Is this the correct and the only way?

Upvotes: 4

Views: 2436

Answers (2)

SteeveDroz
SteeveDroz

Reputation: 6136

The things I usually do when I have a remote git repository with at least one commit is the following:

  1. Create a composer project
  2. Initiate a new git repository inside
  3. Make a first commit
  4. Add a remote server to my local git
  5. Fetch commits from the remote git server
  6. Link the local branch to the remote one
  7. Put that new commit after the existing ones

Example:

composer create-project acme/framework project          # Creates composer project
cd project                                              # Go to project
git init                                                # Create new git repo
git add --all                                           # Add all for commit
git commit -m "Set up Acme Framework"                   # Commit
git remote add origin https://gitlab.com/Me/project.git # Add remote server
git fetch                                               # Fetch data from server
git branch -u origin/main                             # Link local and remote branches
git rebase                                              # Move local branch after remote

If you don't have any commit in your remote repository, don't do

git fetch
git branch -u origin/main
git rebase

and replace your next git push by

git push --set-upstream origin main

Edit

GitLab allows you to push a project that doesn't exist, so you can:

composer create-project acme/framework project          # Creates composer project
cd project                                              # Go to project
git init                                                # Create new git repo
git add --all                                           # Add all for commit
git commit -m "Set up Acme Framework"                   # Commit
git remote add origin https://gitlab.com/Me/project.git # Add remote server with non existing project
git push                                                # Create remote project

Please check with other services if they have an identical feature.

Upvotes: 2

KingCrunch
KingCrunch

Reputation: 131891

Actually thats the way to go. create-project creates a project from a remote location. If the directory already exists and even further if it is not empty neither composer, nor git can and want to decide, how to handle conflicts.

However, your said you created an empty repository and you want to clone the project via create-project into this folder. Thats pretty straight forward

composer create-project foo/bar
cd bar
rm -Rf .git
git init

Additionally you can keep the origin remote, so you can fetch changes from upstream easier. Just add your own remote and only push there.

Upvotes: 2

Related Questions