# GitHub / GitLab / Git オペレーション

**URL:** https://forum.ficusonline.com/t/topic/59
**Category:** Linux
**Created:** [2016 年 12 月 20 日午前 1:29 UTC](https://forum.ficusonline.com/t/topic/59 "2016-12-20T01:29:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2016 年 12 月 20 日午前 1:29 UTC](https://forum.ficusonline.com/t/topic/59/1 "2016-12-20T01:29:08Z")

</div>

gitは非常に便利な履歴管理ソフトですが、コマンド操作がメインなので理解を深めるために都度文章や図に纏めて復習することをお勧めします。

[http://stackoverflow.com/questions/1123344/merging-between-forks-in-github](http://stackoverflow.com/questions/1123344/merging-between-forks-in-github)

フォークしたレポジトリに最新の本家レポジトリの内容をマージする方法について。

まず本家レポジトリmain-repoのリモートアドレスを追加します。リモート名はbase-repo（任意）としました。

> git remote add base-repo git://github.com/main-repo/repo.git

リモートが追加されたかどうか確認します。

> git remote -v

本家レポジトリのfetchフェッチ（ファイル郡のロード）とmergeマージ（ファイル群の上書き）をpullコマンドで実行します。（masterブランチ上で実行していますが、新規ブランチを作成し、そこでpullコマンドを実行、masterブランチにcheckoutチェックアウトしmergeマージした方がいいかもしれません。）

> git pull base-repo master

フォークしたサーバに変更内容をpushプッシュ（アップロード）します。

> git push origin

 ![](https://forum.ficusonline.com/uploads/default/original/1X/dc057293f0c871872168204b398fd5f8361c0467.png)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2017 年 2 月 1 日午前 7:01 UTC](https://forum.ficusonline.com/t/topic/59/2 "2017-02-01T07:01:34Z")

</div>

**大まかなレポジトリの種類**

① githab上の本家レポジトリ  
[git@github.com](mailto:git@github.com):main\_repo/test.git

② github上で①からフォーク(folk)したレポジトリ(repositry) —\>フォークリモート：通常originという名称  
[git@github.com](mailto:git@github.com):my\_repo/test.git

③ ②を自分のPCへクローン(clone)したローカルレポジトリ  
git clone [git@github.com](mailto:git@github.com):my\_repo/test.git

**フォークレポジトリとローカルレポジトリ**

②と③は各々変更してもpushやfetchまたはpushコマンドで双方の内容を容易に一致させることができます。

**本家レポジトリとローカルレポジトリ**

①と②または③との整合性を取るためには、

1. ローカルレポジトリ内で①をリモートとして追加登録
2. fetch,mergeコマンドでローカルレポジトリに本家レポジトリの内容を上書き
3. フォークリモートへpushします。

**＜補足説明＞**  
**１．リモートの追加と確認は以下の手順で行います。**  
[https://help.github.com/articles/configuring-a-remote-for-a-fork/](https://help.github.com/articles/configuring-a-remote-for-a-fork/)

**リモートの確認**

> $ git remote -v  
> origin [https://github.com/YOUR\_USERNAME/YOUR\_FORK.git](https://github.com/YOUR_USERNAME/YOUR_FORK.git) (fetch)  
> origin [https://github.com/YOUR\_USERNAME/YOUR\_FORK.git](https://github.com/YOUR_USERNAME/YOUR_FORK.git) (push)

**オリジナルリモートの追加（リモート名：upstream）**

> $ git remote add upstream [https://github.com/ORIGINAL\_OWNER/ORIGINAL\_REPOSITORY.git](https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git)

**リモートの再確認**

> $ git remote -v  
> origin [https://github.com/YOUR\_USERNAME/YOUR\_FORK.git](https://github.com/YOUR_USERNAME/YOUR_FORK.git) (fetch)  
> origin [https://github.com/YOUR\_USERNAME/YOUR\_FORK.git](https://github.com/YOUR_USERNAME/YOUR_FORK.git) (push)  
> upstream [https://github.com/ORIGINAL\_OWNER/ORIGINAL\_REPOSITORY.git](https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git) (fetch)  
> upstream [https://github.com/ORIGINAL\_OWNER/ORIGINAL\_REPOSITORY.git](https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git) (push)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2017 年 9 月 15 日午前 7:38 UTC](https://forum.ficusonline.com/t/topic/59/3 "2017-09-15T07:38:09Z")

</div>

Githubへのアクセスに２段階認証している場合には、ユーザパスワードの代わりにトークンを生成して、これを利用します。  
トークンはGithubのユーザページの **Settings—\>Deveropper settings—\>personal access tokens** で生成して下さい。

> **[個人用アクセス トークンを管理する - GitHub ドキュメント](https://docs.github.com/ja/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)**
>
> コマンド ラインまたは API を使用して GitHub への認証を行うときに、パスワードの代わりに personal access token を使用することができます。

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2017 年 9 月 15 日午前 7:47 UTC](https://forum.ficusonline.com/t/topic/59/4 "2017-09-15T07:47:46Z")

</div>

リモートURLの変更（sshからhttpsへの変更、またはその逆）

> **[リモートリポジトリを管理する - GitHub ドキュメント](https://docs.github.com/ja/get-started/git-basics/managing-remote-repositories)**
>
> お使いのコンピューター上のローカル リポジトリと、GitHub 上でホストされているリモート リポジトリを操作する方法について説明します。

Switching remote URLs from SSH to HTTPS

> $ git remote set-url origin [https://github.com/USERNAME/REPOSITORY.git](https://github.com/USERNAME/REPOSITORY.git)

Switching remote URLs from HTTPS to SSH

> $ git remote set-url origin [git@github.com](mailto:git@github.com):USERNAME/REPOSITORY.git

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 9 月 15 日午後 10:57 UTC](https://forum.ficusonline.com/t/topic/59/5 "2019-09-15T22:57:43Z")

</div>

**Git Rebase**

[https://git-scm.com/docs/git-rebase](https://git-scm.com/docs/git-rebase)

**ex) topicにチェックアウトしている状態で最新masterの内容を反映**

```
      A---B---C topic
     /
D---E---F---G master

```

```auto
$ git rebase master
or
$ git rebase master topic

```

would be

```
              A'--B'--C' topic
             /
D---E---F---G master

```

**フォークレポジトリに最新のフォーク元レポジトリの内容を反映**

フォークレポのmaster上で実施

**Step 1:** リモートにフォーク元レポ：base-repo（名称任意）を追加

> `git remote add base-repo https://github.com/original-repo/goes-here.git`

**Step 2:** ブランチを含むフォーク元レポをフェッチ

> `git fetch base-repo`

**Step 3:** rebaseコマンドによるフォークレポのmasterをフォーク元レポのmasterに書換え

> `git rebase base-repo/master`

**Step 4:** オリジナルリモートorigin（フォークレポ）masterにプッシュ

> `git push origin master --force`

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 9 月 23 日午後 2:45 UTC](https://forum.ficusonline.com/t/topic/59/6 "2022-09-23T14:45:37Z")

</div>

## GitLabパブリックサーバへの登録

GitHubをMicrosoftが管理するようになってから、アップロードしたコードの取り扱いなどについて色々と条件が変更になっているようで、GitLabへ移行するユーザが増えているようです。GitLabはGitHub同様、パプリックサーバが用意されていることに加え、ローカルで専用サーバを構築することもできるためプライベートなコードの管理についてはこちらの方が便利です。今後はGitLabへシフトした方がいいかもしれません。

> **[Download and install GitLab](https://about.gitlab.com/install/)**
>
> Download, install and maintain your own GitLab instance with various installation packages and downloads for Linux, Kubernetes, Docker, Google Cloud and more.

### SSHキーペアを利用してリモートサーバへアクセス

> **[Use SSH keys with GitLab | GitLab Docs](https://docs.gitlab.com/user/ssh/)**
>
> Use SSH keys for secure authentication and communication with GitLab repositories.

| Algorithm | Public key | Private key |
| --- | --- | --- |
| ED25519 (preferred) | id\_ed25519.pub | id\_ed25519 |
| ED25519\_SK | id\_ed25519\_sk.pub | id\_ed25519\_sk |
| ECDSA\_SK | id\_ecdsa\_sk.pub | id\_ecdsa\_sk |
| RSA (at least 2048-bit key size) | id\_rsa.pub | id\_rsa |
| DSA (deprecated) | id\_dsa.pub | id\_dsa |
| ECDSA | id\_ecdsa.pub | id\_ecdsa |

For example, for **ED25519** :

```auto
$ ssh-keygen -t ed25519 -C "<comment>"

```

For 2048-bit **RSA** :

```auto
$ ssh-keygen -t rsa -b 2048 -C "<comment>"

```

**Ed25519: high-speed high-security signatures**

> **[Introduction](https://ed25519.cr.yp.to/)**

作成したキーペアのパブリックキーの中身を、GitLabのアカウントメニューの **“SSH Keys”** から登録後アクセスできるか確認

```auto
$ ssh -T git@gitlab.com
Welcome to GitLab, @ficusonline!

```

ユーザとパスワードの入力を省くため、リモートをhttpsではなくSSHで登録

```auto
$ git remote add origin git@gitlab.com:<User_Name>/<Project_Name>.git

```

* * *

**Error: “you are not allowed to push code to protected branches on this project”**

プロジェクトメニューから `"Settings" → "Repository" → "Protected branches"` の `Expand` ボタンをクリックして設定事項を展開します。`"Unprotect"` をクリックするか、 `"Developers can push"` を選択して下さい。

> <https://stackoverflow.com/questions/32246503/fix-gitlab-error-you-are-not-allowed-to-push-code-to-protected-branches-on-thi>

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2026 年 10 月 24 日午前 2:10 UTC](https://forum.ficusonline.com/t/topic/59/7 "2026-10-24T02:10:57Z")

</div>

## GitHub CLI : gh

`gh` は GitHub 公式のコマンドラインツール(GitHub CLI) です。ブラウザで行う GitHub の操作を、ターミナルから実行できるようにします。

インストール

```auto
$ sudo apt install gh

```

### 主な機能

| 用途 | コマンド例 |
| --- | --- |
| 認証(ログイン) | `gh auth login` / `gh auth status` |
| リポジトリ作成 | `gh repo create my-project --public --source=. --push` |
| リポジトリ設定 | `gh repo edit --add-topic lora --description "..."` |
| クローン | `gh repo clone owner/repo` |
| Issue | `gh issue create` / `gh issue list` |
| Pull Request | `gh pr create` / `gh pr checkout 12` / `gh pr merge` |
| リリース | `gh release create v0.1.0 --notes "First release"` |
| 閲覧 | `gh repo view --web`(ブラウザで開く) |

GitHubにログイン

```auto
$ gh auth login

```

質問には次のように答えます。

> What account: [GitHub.com](http://GitHub.com)  
> Preferred protocol: SSH  
> Authenticate Git with your GitHub credentials: Yes  
> How to authenticate: Login with a web browser

表示される8桁のコードをブラウザで入力すれば完了です。

これでgitコマンドによるGitHubとの連携操作でパスワードやトークンを入力する手間を省けます。

* * *

### ghコマンド使用例

> [@LoRa + MQTT + OpenRemote によるIoTシステムの構築](https://forum.ficusonline.com/t/topic/568/13):
>
> ソースコード: GitHub $ git config -l # 登録ユーザ名とメールアドレス確認 $ gh auth login # GitHub CLI GitHubへプッシュ $ cd lora-project $ git init -b main $ git add . $ git commit -m "Initial commit" $ gh repo create lora-p…
