How to git push an existing project to GitLab
Steps to upload an existing project to GitLab
Follow these steps to add and push 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 init
command 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 push
operation and use the-u
switch to set the upstream branch. - Confirm that the files in the local project have been uploaded to 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 init
git add .
git commit -m "Push existing project to GitLab"
git remote add origin https://gitlab.com/cameronmcnz/example-website.git
git push -u origin master
These commands will only work if the following assumptions are true:
- The remote GitLab repository is empty and has no commit history
- The local branch is named master, not main
Problems Pushing Projects to GitLab
If you are trying to push a local project to a remote repository that does have a commit history, there are two alternative approaches to take:
- Clone the GitLab repo and then copy your project files into the new repo.
- Perform the git remote add origin command and rebase your local branch onto the remote.
These two approaches are described in detail in the video above and in this recently published git remote add origin tutorial.
A third approach is to add the -f
switch to the push and force your local project’s commit history onto the remote GitLab repo.
git push -u -f origin master
However, GitLab branch protection rules typically prevent forced pushes, so you’ll need to configure your account to allow this.
Furthermore, a push with force rewrites the commit history on the server, so if others on your team use that repo, a forced push has the potential to corrupt their repositories and make it impossible for them to pull and push to that repo.
Configure your 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 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:
git init
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 origin 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-u
switch.
git push -u 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.
To push with force and overwrite an existing GitLab’s commit history with your own (a dangerous operation on a shared repository), add the -f switch:
git push -u -f origin master
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 Git or GitLab repo.
To learn more, check out this Git and GitLab tutorial for beginners.