はじめに
WindowsのPowershellを利用してGithubのリポジトリをcloneし、ローカルで編集後にpushする流れを紹介します。
Gitの流れは分かっている方が自前のGithubリポジトリに一通りハンズオンしたいケースを想定しました。
環境情報
> git version git version 2.25.0.windows.1
クローン(手元にコピーする)
Github上で既に作成したファイル群をcloneで手元に持ってきます。
ディレクトリAにいると仮定し、そこでBというリポジトリに対してgit cloneを行うと以下のような構成になります。
├── A ├── B ....
Githubにて「clone/download」という箇所にて「Use an SSH key and passphrase from account. 」を選択してURLを取得します。
SSH鍵登録がまだの方は先にそちらを済ませてください。
取得したURLに対して以下のようにクローンします。
> git clone git@github.xx/B.git Cloning into 'B'... remote: Enumerating objects: 51, done. remote: Counting objects: 100% (51/51), done. remote: Compressing objects: 100% (44/44), done. remote: Total 51 (delta 9), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (51/51), 9.27 KiB | 1.16 MiB/s, done. Resolving deltas: 100% (9/9), done.
構造を確認する
例えばリポジトリにhoge.txtしか配置していなかったとしてもcloneしたディレクトリを見ると以下のように大量のファイル群が格納されています。
$ tree -a b/ b/ ├── .git │ ├── config │ ├── description │ ├── HEAD │ ├── hooks │ │ ├── applypatch-msg.sample │ │ ├── commit-msg.sample │ │ ├── fsmonitor-watchman.sample │ │ ├── post-update.sample │ │ ├── pre-applypatch.sample │ │ ├── pre-commit.sample │ │ ├── pre-merge-commit.sample │ │ ├── prepare-commit-msg.sample │ │ ├── pre-push.sample │ │ ├── pre-rebase.sample │ │ ├── pre-receive.sample │ │ └── update.sample │ ├── index │ ├── info │ │ └── exclude │ ├── logs │ │ ├── HEAD │ │ └── refs │ │ ├── heads │ │ │ └── master │ │ └── remotes │ │ └── origin │ │ └── HEAD │ ├── objects │ │ ├── info │ │ └── pack │ │ ├── pack-d1c7480dae....idx │ │ └── pack-d1c7480dae....pack │ ├── packed-refs │ └── refs │ ├── heads │ │ └── master │ ├── remotes │ │ └── origin │ │ └── HEAD │ └── tags ├── hogehoge.txt └── README.md
特に.git/configは重要で基本的にはこのconfigに書かれている内容でgitは動作します。
例えばgitの状況を確認しようと以下コマンドを押下した際に
> git branch fatal: not a git repository (or any of the parent directories): .git
と表示される場合がありますが、それはカレントディレクトリから探しに行っているからです。
ディレクトリaにいる場合はリポジトリbに移動してから同コマンドを行えばきちんと認証されます。
> cd .\b\ > git branch * master
ファイルを修正&ステージング&コミット&プッシュ
ファイルhoge.txtを修正した場合、git statusコマンドで以下のように表示されます。
> git status
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hoge.txt
no changes added to commit (use "git add" and/or "git commit -a")
Your branch is up to date with 'origin/master'.
(use "git restore --staged <file>..." to unstage)
modified: hoge.txtあとは以下コマンドでリモートのリポジトリに反映されます。
> git add -A > git commit -m "write commit message" > git push origin master
ちなみになぜorigin masterだけで反映できるのかというと.git/configの中を見れば分かります。
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = git@github.xx/b.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master上記の中でブランチは「master」でリポジトリが「origin」という名のgithubのURLが登録されているからです。
終わりに
以下記事ではinitコマンドを利用してローカルリポジトリを利用していましたが
本記事ではcloneを利用したリモートリポジトリへのpushの流れを足してみました。
コマンドだけで言えば clone , add , commit ,push ですが肝は.git配下にあるconfigにて良しなににリモートのGithubリポジトリの情報を持っている点は覚えておくとどこかで役立つと思います。
以上、ご参考になれば幸いです。