Termuxで書いたコードをgithubにpushするまでの一連の流れを勉強がてらメモしたので、記事にもしてみました。Termuxで、と題してはみたものの、Termuxだからといって特別なことはほぼ無い、と思います。
環境条件
私用のスマホPixcel6aにTermuxをインストールしています(Termuxへのgitのインストールは以下のみ。Termuxではsudoも不要です。インストール時のメモはこちら。
apt install git
あとpush先のgithub側の設定もお忘れなく。
Termux特有?エラー回避
これがTermuxのみで発生する内容なのか分からないのですが、もし以下のようなメッセージで次に進まない場合は....
fatal: detected dubious ownership in repository
これを試してみてください。 project
までの部分はリポジトリのパスなので、もちろんご自身の環境条件下でのパスを指定してください。ここで留意点なのですが、リポジトリまでのパスを例え誤入力してもエラーになりません。コマンドがエラーにならないのに再度引っかかる場合はパスの部分で誤打がないか確認してください。
git config --global --add safe.directory /storage/emulated/0/project
一連の流れを順にやってみる
例えば以下のような作業フォルダがあったとします。「project」というフォルダ内に3つファイルが入っているだけの単純なものです。
project ├── main.py ├── sub.py └── readme.md
まずはprojectフォルダをリポジトリにするためにgit init
を実行する。
$ git init Initialized empty Git repository in /project/.git/
うまく行っていれば、.git
ができているハズ
project ├── main.py ├── sub.py ├── readme.md └── .git
これでとりあえずリポジトリができたことになる。続いて、ブランチも作ってみる。 ブランチ名は別にmainじゃなくても良い。git branch
だけを実行するとブランチ一覧が表示される。
$ git branch -M main $ git branch * main
ついでにgithubへのpushの準備もしておく。username/projectのところは適宜変更ください。
$ git remote add origin https://github.com/username/project.git
上記の中で、origin
はリモートリポジトリ名称で、別になんでもいい。この状態ではまだ何もadd
していないのでgit status
を実行すると。以下のようになるハズ。
$ git status On branch main No commits yet Untracked files: main.py sub.py readme.py
すべてのファイルがUntracked files
に位置づけられている。ではまず、readme.mdのみをaddしてみます。
$ git add readme.md $ git status On branch main No commits yet Changes to be committed: new file: readme.md Untracked files: main.py sub.py
Changes to be committed
のところに新規ファイルとしてreadme.mdが移動してきているのがわかります。普通はやらないとは思いますが、一旦readmeだけでコミットしてみます。
git commit -m "First commit" [main (root-commit) 6c0e5a4] First commit 1 file changed, 3 insertions(+) create mode 100644 readme.md
この状態でステータスを見てみましょう。
$ git status On branch master Untracked files: main.py sub.py
readme.mdが消えました。コミット済のファイル一覧はgit show
で確認できる。
$ git show --name-only commit d9ab44ca591cc729edbe62c2045cdfa651c04ac5 (HEAD -> main) Author: username <xxxxxxxxx@yahoo.co.jp> Date: Sat Sep 30 12:10:00 2023 +0900 First commit readme.md
最後にgithubのリポジトリに押し込んでみます。ユーザー名称とパスワードは事前に確認しておくこと。
$ git push -u origin main Username for 'https://github.com': username Password for 'https://username@github.com': Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 249 bytes | 31.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/username/project.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.
Statusを確認するとこんな感じになると思います。githubのリポジトリとローカルのリポジトリがup to date
だそうです。
$ git status On branch main Your branch is up to date with 'origin/main'. Untracked files: main.py sub.py nothing added to commit but untracked files present (use "git add" to track)
さすがにコミットされているのがreadmeだけだとあまりにもさみしいので、main.pyもプッシュまで持っていきましよう。まずはadd
します。
$ git add main.py $ git status On branch main Your branch is up to date with 'origin/main'. Changes to be committed: new file: main.py Untracked files: sub.py
コミットします。
$ git commit -m "2nd commit" [main dd81e33] upload main 1 file changed, 39 insertions(+) create mode 100644 main.py
んで、プッシュします。
$ git push -u origin main Username for 'https://github.com': username Password for 'https://username@github.com': Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 690 bytes | 345.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/username/project.git 6c0e5a4..dd81e33 main -> main branch 'main' set up to track 'origin/main'.