185

I have an account of a Gitlab installation where I created the repository "ffki-startseite"

Now I want to clone the repository git://freifunk.in-kiel.de/ffki-startseite.git into that repository with all commits and branches, so I can start working on it in my own scope.

How can I import it?

rubo77
  • 19,527
  • 31
  • 134
  • 226
  • 3
    A new feature of gitlab, create project and then go to the project-page. Here you will see basic instructions. – Martijn van Wezel Mar 19 '17 at 19:00
  • Possible duplicate of [Gitlab repository mirroring](https://stackoverflow.com/questions/14288288/gitlab-repository-mirroring) – kelvin Dec 31 '18 at 16:54
  • The basic instructions are gone in the newer versions of Gitlab, could anyone that had copied them copy them here? – jdevora Apr 30 '19 at 11:51

11 Answers11

189

I was able to fully export my project along with all commits, branches and tags to gitlab via following commands run locally on my computer:

To illustrate my example, I will be using https://github.com/raveren/kint as the source repository that I want to import into gitlab. I created an empty project named Kint (under namespace raveren) in gitlab beforehand and it told me the http git url of the newly created project there is http://gitlab.example.com/raveren/kint.git

The commands are OS agnostic.

In a new directory:

git clone --mirror https://github.com/raveren/kint
cd kint.git
git remote add gitlab http://gitlab.example.com/raveren/kint.git
git push gitlab --mirror

Now if you have a locally cloned repository that you want to keep using with the new remote, just run the following commands* there:

git remote remove origin
git remote add origin http://gitlab.example.com/raveren/kint.git
git fetch --all

*This assumes that you did not rename your remote master from origin, otherwise, change the first two lines to reflect it.

Community
  • 1
  • 1
raveren
  • 17,799
  • 12
  • 70
  • 83
112

Add the new gitlab remote to your existing repository and push:

git remote add gitlab url-to-gitlab-repo
git push gitlab master
  • I had to push it with force, cause I fiddled around in gitLab already: `git push gitlab master -f`. Now I have the whole repo with all commits in my new GitLab ;) thanks – rubo77 Dec 03 '13 at 19:46
  • 20
    Wouldn't you need to do a mirror? What if you had other branches than master? – slhck Jan 18 '14 at 15:37
  • 7
    yes, by doing as OP suggests, you **lose all branches (except master) and tags** – raveren May 27 '15 at 13:21
  • The Gitlab Project Import will load the remote repo from a URL when you create the project. Includes instructions for importing svn repos. – rickfoosusa Jul 28 '15 at 15:48
  • @rickfoosusa, unless your server can only access your local machine and not github. – raveren Nov 04 '15 at 13:09
  • 1
    git remote add gitlab url-to-gitlab-repo git push gitlab master --mirror – Saurabh May 24 '16 at 09:53
  • For the first time you may need git commit -m 'initial commit' before push. – Umit Kaya Apr 06 '17 at 12:58
  • I had to use `git push -u gitlab master` to handle branches that aren't already in the repo (as is the case for a newly initialized repo). Also, requiring 6 characters to be changed in edits on a programming web site is ridiculous. – dhj May 26 '18 at 07:51
26

To keep ALL TAGS AND BRANCHES

Just simply run this command in an existing Git repository

cd existing_repo
git remote rename origin previous-hosts
git remote add gitlab git@git.hutber.com:hutber/kindred.com.git
git push -u gitlab --all
git push -u gitlab --tags
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
22

This is a basic move one repo to new location. I use this sequence all te time. With --bare no source files will be seen.

Open Git Bash.
Create a bare clone of the repository.

git clone --bare https://github.com/exampleuser/old-repository.git

Mirror-push to the new repository.

cd old-repository.git

git push --mirror https://gitlab.com/exampleuser/new-repository.git

Remove the temporary local repository you created in step 1.

cd ../
rm -rf old-repository.git

Why mirror? See documentation of git: https://git-scm.com/docs/git-push

--all Push all branches (i.e. refs under refs/heads/); cannot be used with other .

--mirror Instead of naming each ref to push, specifies that all refs under refs/ (which includes but is not limited to refs/heads/, refs/remotes/, and refs/tags/) be mirrored to the remote repository. Newly created local refs will be pushed to the remote end, locally updated refs will be force updated on the remote end, and deleted refs will be removed from the remote end. This is the default if the configuration option remote..mirror is set.

Martijn van Wezel
  • 1,120
  • 14
  • 25
20

Here are the steps provided by the Gitlab:

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.example.com/rmishra/demoapp.git
git push -u origin --all
git push -u origin --tags
Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61
13

rake gitlab:import:repos might be a more suitable method for mass importing:

  • copy the bare repository under repos_path (/home/git/repositories/group/repo.git). Directory name must end in .git and be under a group or user namespace.
  • run bundle exec rake gitlab:import:repos

The owner will the first admin, and a group will get created if not already existent.

See also: How to import an existing bare git repository into Gitlab?

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • For those who may be confused, the bare repository is the repository usually in place in the centralized location (e.g. github) that does NOT contain the .git folder at the root of the repository. This is only a convenient option if you have access to the server where the bare repository is stored. Otherwise the answer by @Raveren is the best option. – TinkerTenorSoftwareGuy Nov 29 '16 at 19:27
9

You create an empty project in gitlab then on your local terminal follow one of these:

Push an existing folder

cd existing_folder
git init
git remote add origin git@gitlab.com:GITLABUSERNAME/YOURGITPROJECTNAME.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.com:GITLABUSERNAME/YOURGITPROJECTNAME.git
git push -u origin --all
git push -u origin --tags
Richard Torcato
  • 2,504
  • 25
  • 26
4
git clone --mirror git@github.com:username/repo-name.git

git remote add gitlab ssh://git@servername.com/username/repo.git

git push -f --tags gitlab refs/heads/*:refs/heads/*

It is better to do it over ssh, the https might won't work

rubo77
  • 19,527
  • 31
  • 134
  • 226
kazerm
  • 499
  • 1
  • 4
  • 11
1

Gitlab is a little bit bugged on this feature. You can lose a lot of time doing troubleshooting specially if your project is any big.

The best solution would be using the create/import tool, do not forget put your user name and password, otherwise it won't import anything at all.

Follow my screenshots

enter image description here

JBarros35
  • 976
  • 1
  • 12
  • 18
0

Moving a project from GitHub to GitLab including issues, pull requests Wiki, Milestones, Labels, Release notes and comments

There is a thorough instruction on GitLab Docs:

https://docs.gitlab.com/ee/user/project/import/github.html

tl;dr

  • Ensure that any GitHub users who you want to map to GitLab users have either:

    • A GitLab account that has logged in using the GitHub icon - or -
    • A GitLab account with an email address that matches the public email address of the GitHub user
  • From the top navigation bar, click + and select New project.

  • Select the Import project tab and then select GitHub.
  • Select the first button to List your GitHub repositories. You are redirected to a page on github.com to authorize the GitLab application.
  • Click Authorize gitlabhq. You are redirected back to GitLab's Import page and all of your GitHub repositories are listed.
  • Continue on to selecting which repositories to import.

But Please read the GitLab Docs page for details and hooks!

(it's not much)

rubo77
  • 19,527
  • 31
  • 134
  • 226
-1

I found the best and simplest method here, works like a charm for me, it will push all the tags and branches from origin to the new remote:

git remote add newremote git@git-cloud.z.com:test/kubespray.git

git push newremote --tags refs/remotes/origin/*:refs/heads/*

I used 'git push --all -u newremote', but it only push the checkouted branches to the newremote.

Git: Push All Branches to a New Remote

by Keith Dechant , Software Architect

Here's a scenario some of you might have encountered with your Git repositories. You have a working copy of a Git repo, say from an old server. But you only have the working copy, and the origin is not accessible. So you can't just fork it. But you want to push the whole repo and all the branch history to your new remote.

This is possible if your working copy contains the tracking branches from the old remote (origin/branch1, origin/branch1, etc.). If you do, you have the entire repo and history.

However, in my case there were dozens of branches, and some or all of them I had never checked out locally. Pushing them all seemed like a heavy lift. So, how to proceed?

I identified two options:

Option 1: Checkout every branch and push I could do this, and I could even write a Bash script to help. However, doing this would change my working files with each checkout, and would create a local branch for each of the remote tracking branches. This would be slow with a large repo.

Option 2: Push without changing your working copy There is a second alternative, which doesn't require a checkout of each branch, doesn't create extraneous branches in the working copy, and doesn't even modify the files in the working copy.

If your old, no-longer-active remote is called "oldremote" and your new remote is called "newremote", you can push just the remote tracking branches with this command:

git push newremote refs/remotes/oldremote/*:refs/heads/*

In some cases, it's also possible to push just a subset of the branches. If the branch names are namespaced with a slash (e.g., oldremote/features/branch3, oldremote/features/branch4, etc.), you can push only the remote tracking branches with names beginning with "oldremote/features":

git push newremote refs/remotes/oldremote/features/*:refs/heads/features/*

Whether you push all the branches or just some of them, Git will perform the entire operation without creating any new local branches, and without making changes to your working files. Every tracking branch that matches your pattern will be pushed to the new remote.

For more information on the topic, check out this thread on Stack Overflow.

Date posted: October 9, 2017

tom
  • 317
  • 3
  • 9
  • In your answer you indicate your method incompletely pushed to the new repo. It also requires more work to accomplish and tedium. For repos with numerous branches, this could take a very long time. This is a good answer for users to know what not to do! You could update it with "warning" markings. – dan Apr 27 '22 at 13:24