9
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Git] Colab上でGitの環境を導入してGitHubにpushするまでの手順 (23年03月時点)

Last updated at Posted at 2023-03-29

はじめに

Colab上で、一からGitHubにpushするまでの手順を記載。

(Google Colaboratory、git version 2.25.1、access tokenを用いてGitHubへアクセス)

Colab上でGitをインストール

!apt-get install git
出力
Reading package lists... Done
Building dependency tree       
Reading state information... Done
git is already the newest version (1:2.25.1-1ubuntu3.10).
0 upgraded, 0 newly installed, 0 to remove and 23 not upgraded.
!git --version
出力
git version 2.25.1
# 場所の確認
!pwd
!ls
出力
/content
sample_data

Colab上でローカルリポジトリを作成・コミット

%%bash
# Gitの最低限必要な初期設定
git config --global user.email "test1@example.com"
git config --global user.name "Test1 Name"
%%bash
# テスト用フォルダを作成
cd "/content"
mkdir "test-space"
cd "test-space"
mkdir "local-repo1"
cd "local-repo1"

# ローカルのリポジトリを作成
git init

# テスト用に変更を加えてコミット
echo "test1 in Colab" > test1.txt
git add .
git commit -m "test1 in Colab"
出力
Initialized empty Git repository in /content/test-space/local-repo1/.git/
[master (root-commit) aff0f65] test1 in Colab
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
# %呼出しで、カレントパスを永続化
%cd "/content/test-space/local-repo1"
出力
/content/test-space/local-repo1
%%bash
# フォルダ状況の確認
echo "▼/root:"
ls /root
echo "▼/content: (data-placed)"
ls /content

# Git状況の確認
echo "▼git branch:"
git branch -a -v
出力
▼/root:

▼/content: (data-placed)
sample_data
test-space

▼git branch:
* master aff0f65 test1 in Colab

履歴には、既定で作成されたmasterブランチが1つだけある状態。(前述でテスト用にコミットした履歴が1つだけある状態)

Colab上でGitHubのリモートリポジトリへアクセス・情報を取得

参照:
Google Colab上でGitHubからCloneして変更をPushするまでのまとめ
https://qiita.com/kurilab/items/f6f4374d7b1980060de7
Colab 上で GitHub を使用する
https://zenn.dev/smiyawaki0820/articles/e346ca8b522356

%%bash
# 既存内容のあるリモートリポジトリ(GitHub)を登録
git remote add origin "https://github.com/DL-from-Scratch/test1"
# https://{USER}:{ACCESS_TOKEN}@github.com/{REPOSITORY}.git
git remote set-url origin "https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git"

# 確認
git remote -v
出力
origin	https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git (fetch)
origin	https://DL-from-Scratch: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@github.com/DL-from-Scratch/test1.git (push)

ここで、リモートリポジトリ(GitHub)からmasterブランチの履歴を取得

# リモートリポジトリ(GitHub)からmasterブランチの履歴を取得
!git fetch origin master
出力
warning: no common commits
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 34 (delta 5), reused 23 (delta 1), pack-reused 0
Unpacking objects: 100% (34/34), 3.88 KiB | 568.00 KiB/s, done.
From https://github.com/DL-from-Scratch/test1
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
# 確認
!git branch -a -v
!git log --oneline --graph --all
出力
* master                aff0f65 test1 in Colab
  remotes/origin/master df46e6d test5

* aff0f65 (HEAD -> master) test1 in Colab
* df46e6d (origin/master) test5
* 10d6e72 test4
* ed03349 test3
* cddbc7f test2
* e2607a2 test1

originのmasterを取り込んだので、履歴には、2つの別々の履歴がある状態。(①ローカルmasterブランチ側、②リモートoriginのmasterブランチ側の2つ)

見方
①ローカルmasterブランチ側
* aff0f65 (HEAD -> master) test1 in Colab
---
②リモートoriginのmasterブランチ側
* df46e6d (origin/master) test5
* 10d6e72 test4
* ed03349 test3
* cddbc7f test2
* e2607a2 test1

Colab上でGitHubのリモートリポジトリの内容をcheckout・ファイルを取得

# checkoutでファイル内容を取得
!git checkout origin/master
出力
Note: switching to 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
...(略)...

HEAD is now at df46e6d test5
# 確認
!ls
出力
README.md  subfolder1  test1.txt  test2.txt

作業フォルダ内が、GitHubのファイル内容に更新された状態。

treeコマンドでフォルダ構造も表示すると、

!apt-get install tree
出力
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 23 not upgraded.
Need to get 43.0 kB of archives.
After this operation, 115 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 tree amd64 1.8.0-1 [43.0 kB]
Fetched 43.0 kB in 0s (97.1 kB/s)
Selecting previously unselected package tree.
(Reading database ... 128285 files and directories currently installed.)
Preparing to unpack .../tree_1.8.0-1_amd64.deb ...
Unpacking tree (1.8.0-1) ...
Setting up tree (1.8.0-1) ...
Processing triggers for man-db (2.9.1-1) ...
# 確認
!tree
出力
.
├── README.md
├── subfolder1
│   ├── subfolder11
│   │   ├── test1.txt
│   │   └── test2.txt
│   ├── test1.txt
│   └── test2.txt
├── test1.txt
└── test2.txt

2 directories, 7 files

Colab上でGitHubのリモートリポジトリへpush・変更内容を送信

# ローカルでの追跡用に別ブランチを用意
!git checkout -b master_for_test1
出力
Switched to a new branch 'master_for_test1'
%%bash
# テスト用に変更を加えてコミット
echo "test2 in Colab" >> test1.txt
git add .
git commit -m "test2 in Colab"
出力
[master_for_test1 311e71e] test2 in Colab
 1 file changed, 1 insertion(+)
# 確認
!git branch -a -v
!git log --oneline --graph --all
出力
  master                aff0f65 test1 in Colab
* master_for_test1      311e71e test2 in Colab
  remotes/origin/master df46e6d test5

* 311e71e (HEAD -> master_for_test1) test2 in Colab
* df46e6d (origin/master) test5
* 10d6e72 test4
* ed03349 test3
* cddbc7f test2
* e2607a2 test1
* aff0f65 (master) test1 in Colab
見方
②リモートoriginのmasterブランチ側
* 311e71e (HEAD -> master_for_test1) test2 in Colab
* df46e6d (origin/master) test5
* 10d6e72 test4
* ed03349 test3
* cddbc7f test2
* e2607a2 test1
---
①ローカルmasterブランチ側
* aff0f65 (master) test1 in Colab

origin/masterの1つ上に履歴が1つ追加された状態。

ここで、GitHubリモートリポジトリへ送信 (ローカルブランチmaster_for_test1を、リモートブランチmasterにpush)

# GitHubリモートリポジトリへ送信 (ローカルブランチmaster_for_test1を、リモートブランチmasterにpush)
!git push origin master_for_test1:master
出力
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/DL-from-Scratch/test1.git
   df46e6d..311e71e  master_for_test1 -> master
# 確認
!git branch -a -v
!git log --oneline --graph --all
出力
  master                aff0f65 test1 in Colab
* master_for_test1      311e71e test2 in Colab
  remotes/origin/master 311e71e test2 in Colab

* 311e71e (HEAD -> master_for_test1, origin/master) test2 in Colab
* df46e6d test5
* 10d6e72 test4
* ed03349 test3
* cddbc7f test2
* e2607a2 test1
* aff0f65 (master) test1 in Colab

origin/masterが、master_for_test1と同じ位置まで移動し、pushの送信が完了OK。

GitHub上でも、test1.txtに「test2 in Colab」のコミットがある状態が確認できる。
https://github.com/DL-from-Scratch/test1/tree/master
image.png
https://github.com/DL-from-Scratch/test1/commits/master
image.png

試行したファイル

環境

Google Colaboratory、git version 2.25.1、access tokenを用いてGitHubへアクセス

9
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?