<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Diego Costa</title>
    <description>The latest articles on Forem by Diego Costa (@diegocoxta).</description>
    <link>https://forem.com/diegocoxta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F143228%2F81cae394-7fa6-411d-8534-f338bc745ed3.png</url>
      <title>Forem: Diego Costa</title>
      <link>https://forem.com/diegocoxta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/diegocoxta"/>
    <language>en</language>
    <item>
      <title>How to automatically migrate all your repositories from Gitlab to Github.</title>
      <dc:creator>Diego Costa</dc:creator>
      <pubDate>Sun, 12 Feb 2023 08:19:09 +0000</pubDate>
      <link>https://forem.com/diegocoxta/how-to-automatically-migrate-all-your-repositories-from-gitlab-to-github-40mn</link>
      <guid>https://forem.com/diegocoxta/how-to-automatically-migrate-all-your-repositories-from-gitlab-to-github-40mn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article was previously posted on &lt;a href="https://diegocosta.me/en/how-automatically-migrate-repositories-gitlab-github/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since GitHub announced that teams and users would be able to use unlimited private repositories I've been thinking about migrating my old projects archived on Gitlab and centralizing all my projects on Github. After postponing for a long time I decided to do this migration this Saturday night. &lt;em&gt;(is it the best use of my free time? maybe)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The first issue I faced is the migration tool provided by GitHub only imports one repository at a time and it always asks for my GitLab password to complete the migration. I had 100 or more repositories, it would take a long time to complete.&lt;/p&gt;

&lt;p&gt;So, thinking about how to make the most of my night, I decided to dive deep into GitLab and GitHub's public APIs, run this migration in 5 minutes, and now, explain how you can do that too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Migrating all your repositories from Gitlab to Github.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first step is to download all your Gitlab projects to your local machine and to do this, you need your GitLab User ID (you can find this number under your name in your profile) and a &lt;a href="https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html" rel="noopener noreferrer"&gt;private GitLab token&lt;/a&gt; with &lt;code&gt;api&lt;/code&gt; and &lt;code&gt;read_repository&lt;/code&gt; scopes.&lt;/p&gt;

&lt;p&gt;Now you have the information needed to run the following command in your terminal instance replacing your user-id and private-token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;USER_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nv"&gt;PRIVATE_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;repo &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; https://gitlab.com/api/v4/users/&lt;span class="nv"&gt;$USER_ID&lt;/span&gt;/projects&lt;span class="se"&gt;\?&lt;/span&gt;private_token&lt;span class="se"&gt;\=&lt;/span&gt;&lt;span class="nv"&gt;$PRIVATE_TOKEN&lt;/span&gt;&lt;span class="se"&gt;\&amp;amp;&lt;/span&gt;per_page&lt;span class="se"&gt;\=&lt;/span&gt;999 | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s2"&gt;".[].ssh_url_to_repo"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;git clone &lt;span class="nv"&gt;$repo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will clone all projects from your account to your local machine. I recommend running it inside a dedicated folder as it will be important in the next steps. I previously created a "gitlab" folder.&lt;/p&gt;

&lt;p&gt;The last step is to create the repositories on GitHub and upload all the content downloaded in the previous step. Let's go.&lt;/p&gt;

&lt;p&gt;To do this, you will use the official &lt;a href="https://cli.github.com/" rel="noopener noreferrer"&gt;GitHub CLI&lt;/a&gt;. After downloading and logging in using &lt;code&gt;gh auth login&lt;/code&gt;, you can use &lt;code&gt;gh repo create&lt;/code&gt; to complete the job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;project &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;/&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$project&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="nv"&gt;$project&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    gh repo create &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;project&lt;/span&gt;:0:-1&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="nt"&gt;--source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;--private&lt;/span&gt; &lt;span class="nt"&gt;--push&lt;/span&gt; &lt;span class="nt"&gt;--remote&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;upstream
    &lt;span class="nb"&gt;cd&lt;/span&gt; ..
  &lt;span class="k"&gt;fi
done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will map all folders (Now you know why it was important to create a dedicated folder to clone all projects), navigate through them, use &lt;code&gt;gh repo create&lt;/code&gt; to create a remote repository with the same folder name (you can remove the flag &lt;code&gt;--private&lt;/code&gt; to create public repositories) and push all content to the remote.&lt;/p&gt;

&lt;p&gt;Job done! &lt;strong&gt;And we can now delete all Gitlab projects programmatically (optional).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TIP: For delete repositories your Private Token needs &lt;code&gt;write_repository&lt;/code&gt; scope&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;USER_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nv"&gt;PRIVATE_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;repo &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; https://gitlab.com/api/v4/users/&lt;span class="nv"&gt;$USER_ID&lt;/span&gt;/projects&lt;span class="se"&gt;\?&lt;/span&gt;private_token&lt;span class="se"&gt;\=&lt;/span&gt;&lt;span class="nv"&gt;$PRIVATE_TOKEN&lt;/span&gt;&lt;span class="se"&gt;\&amp;amp;&lt;/span&gt;per_page&lt;span class="se"&gt;\=&lt;/span&gt;999 | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s2"&gt;".[].id"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; DELETE https://gitlab.com/api/v4/projects/&lt;span class="nv"&gt;$repo&lt;/span&gt;&lt;span class="se"&gt;\?&lt;/span&gt;private_token&lt;span class="se"&gt;\=&lt;/span&gt;&lt;span class="nv"&gt;$PRIVATE_TOKEN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will send a delete request for every project that you have.&lt;/p&gt;

&lt;p&gt;So, do you know a simpler way to do this job? Send me a tip on &lt;a href="https://twitter.com/diegocoxta" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, I'd love to know.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>github</category>
      <category>gitlab</category>
      <category>migration</category>
    </item>
  </channel>
</rss>
