How to git push an existing project to GitLab
Steps to push an existing project to GitLab
Follow these steps to add and import a new project into an existing GitLab repository:
- Create a GitLab repository for the current project.
- Copy the GitLab URL for the new repository to your clipboard.
- Issue the
git initcommand in the base folder of your development project.
- Add all of your project’s files to the Git index and then perform a commit.
- Add the GitLab repository as a remote reference for the local project.
- Run a
git pushoperation and use the
- Confirm that the files in the local project have been imported into GitLab.
Git’s remote push commands for GitLab
For GitLab users who simply want a quick overview of the commands to run in the existing project to push to GitLab, here they are.
git add .
git commit -m "Push existing project to GitLab"
git remote add source https://gitlab.com/cameronmcnz/example-website.git
git push -u -f source master
These commands assume a GitLab repository named example-website and a user account named cameronmcnz.
Configure you GitLab repository first
To push a local development project to GitLab, you must first create a GitLab repository.
To do this, simply click on the “Create Repository” button in the GitLab online console and name the repository. In this tutorial, the repository is called a example-website .
Get the GitLab URL
After you create the repository, find the option to clone the repo.
Copy the GitLab URL that points to the repository you just created.
Initialize Git locally
If the current project does not yet use Git, perform a
git init operation in the existing project’s root directory:
Once the repository is set up, add all of the project’s files to the Git index and perform a commit:
git add . git commit -m "Add existing project files before sending GitLab."
Add GitLab as a remote reference
To push and pull between your existing project and GitLab, you must issue a
git remote add command. This provides your local Git repo with the GitLab URL which was obtained earlier.
git remote add source https://github.com/cameronmcnz/example-website.git
Upload your existing project to GitLab
Once you have configured the remote reference to GitLab, you are ready to push your existing GitLab project.
To do this, issue a
git push command with the name of the current branch along with the
-f switch forces Git to overwrite any files that already exist on GitLab with your existing project’s files.
-u switch makes the remote GitLab repo the default for your existing project.
git push -u -f origin master
Legacy Git repositories create a master branch by default, while newer ones use main. Use the branch name that matches your local Git repository.
Validate the GitLab import
To verify that the existing project was successfully pushed to GitLab, log into GitLab and browse the files uploaded to the repository.
Every file in your current project should now be hosted in your GitLab repo.