目次

Git - 分散型バージョン管理システム

リポジトリを複製

フルクローン (full clone) [git clone]

$ git clone https://github.com/SoftEtherVPN/SoftEtherVPN.git

Cloning into 'SoftEtherVPN'...
remote: Enumerating objects: 171, done.
remote: Counting objects: 100% (171/171), done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 24562 (delta 82), reused 103 (delta 48), pack-reused 24391
Receiving objects: 100% (24562/24562), 526.67 MiB | 1.91 MiB/s, done.
Resolving deltas: 100% (21043/21043), done.

シャロークローン (shallow clone) [git clone --depth 1]

開発リポジトリなどをビルド用に最新履歴のみ高速に複製する🤤

$ git clone --depth 1 https://github.com/SoftEtherVPN/SoftEtherVPN.git

Cloning into 'SoftEtherVPN'...
remote: Enumerating objects: 1820, done.
remote: Counting objects: 100% (1820/1820), done.
remote: Compressing objects: 100% (884/884), done.
remote: Total 1820 (delta 1149), reused 1276 (delta 912), pack-reused 0
Receiving objects: 100% (1820/1820), 9.37 MiB | 2.86 MiB/s, done.
Resolving deltas: 100% (1149/1149), done.

–depth 1: コミット履歴を指定の深さ(最新のみ)に切り捨てることを指示する。Git - git-clone Documentation

とても高速に clone 出来るが master ブランチの fetch しか出来なくなる😅

$ cat SoftEtherVPN/.git/config 

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/SoftEtherVPN/SoftEtherVPN.git
	fetch = +refs/heads/master:refs/remotes/origin/master
[branch "master"]
	remote = origin
	merge = refs/heads/master

通常の clone と同様に fetch できるように直すことは可能🤤

$ cd SoftEtherVPN
$ git config remote.origin.fetch 

+refs/heads/master:refs/remotes/origin/master

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git config remote.origin.fetch 

+refs/heads/*:refs/remotes/origin/*

通常の clone では fetch = +refs/heads/*:refs/remotes/origin/* である🤔

$ git clone https://github.com/SoftEtherVPN/SoftEtherVPN.git SoftEtherVPN-all

Cloning into 'SoftEtherVPN-all'...
remote: Enumerating objects: 26193, done.
remote: Counting objects: 100% (72/72), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 26193 (delta 32), reused 38 (delta 17), pack-reused 26121
Receiving objects: 100% (26193/26193), 527.27 MiB | 2.16 MiB/s, done.
Resolving deltas: 100% (22303/22303), done.

$ cat SoftEtherVPN-all/.git/config 

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/SoftEtherVPN/SoftEtherVPN.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

サブモジュールのシャロークローン [--recommend --depth 1]

$ git submodule update --init --recursive --recommend-shallow --depth 1

Submodule '3rdparty/BLAKE2' (https://github.com/BLAKE2/BLAKE2.git) registered for path '3rdparty/BLAKE2'
Submodule '3rdparty/tinydir' (https://github.com/cxong/tinydir.git) registered for path '3rdparty/tinydir'
Submodule 'src/Mayaqua/3rdparty/cpu_features' (https://github.com/google/cpu_features.git) registered for path 'src/Mayaqua/3rdparty/cpu_features'
Submodule 'src/libhamcore' (https://github.com/SoftEtherVPN/libhamcore.git) registered for path 'src/libhamcore'
Cloning into '/home/tomoyan/work/SoftEtherVPN/3rdparty/BLAKE2'...
Cloning into '/home/tomoyan/work/SoftEtherVPN/3rdparty/tinydir'...
Cloning into '/home/tomoyan/work/SoftEtherVPN/src/Mayaqua/3rdparty/cpu_features'...
Cloning into '/home/tomoyan/work/SoftEtherVPN/src/libhamcore'...
Submodule path '3rdparty/BLAKE2': checked out 'b52178a376ca85a8ffe50492263c2a5bc0fa4f46'
Submodule path '3rdparty/tinydir': checked out 'ec6bff2043eaac3ad25423705e63a781762a0dfd'
remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Enumerating objects: 105, done.
remote: Counting objects: 100% (105/105), done.
remote: Compressing objects: 100% (54/54), done.
remote: Total 57 (delta 42), reused 7 (delta 1), pack-reused 0
Unpacking objects: 100% (57/57), 19.71 KiB | 420.00 KiB/s, done.
From https://github.com/google/cpu_features
 * branch            26133d3b620c2c27f31d571efd27371100f891e9 -> FETCH_HEAD
Submodule path 'src/Mayaqua/3rdparty/cpu_features': checked out '26133d3b620c2c27f31d571efd27371100f891e9'
remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 2), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), 663 bytes | 110.00 KiB/s, done.
From https://github.com/SoftEtherVPN/libhamcore
 * branch            b7f5d8d735b280fc77673c4a29f0436a92ca6660 -> FETCH_HEAD
Submodule path 'src/libhamcore': checked out 'b7f5d8d735b280fc77673c4a29f0436a92ca6660'

リモートリポジトリ

ローカルリポジトリのリモート URL を表示/変更

クローンしたローカルリポジトリのリモート URL を表示する…🤔

$ cd jupyter-binder
$ git remote get-url origin

https://github.com/tomoyan596/jupyter-binder.git

ローカルリポジトリのリモート URL を SSH の URL に変更する😊

$ git remote set-url origin git@github.com:tomoyan596/jupyter-binder.git
$ git remote get-url origin

git@github.com:tomoyan596/jupyter-binder.git

ローカルリポジトリのリモート URL を HTTPS の URL に変更する😊

$ git remote set-url origin https://github.com/tomoyan596/jupyter-binder.git
$ git remote get-url origin

https://github.com/tomoyan596/jupyter-binder.git

リモートブランチの最新に合わせる

$ git pull

Already up to date.

ローカルのファイルやディレクトリへの変更を破棄する

$ git checkout --force

強制的にリモートブランチの最新に合わせる

リモートの最新を取ってくる。

$ git fetch origin master

From git://git.tomoyan.net/project_name
 * branch              master     -> FETCH_HEAD

ローカルの master を、リモート追跡の master にリセットする。

$ git reset --hard origin/master

Untracked files を(-d ディレクトリを含めて)削除する。

$ git clean -df

submodulenew commits, modified content をリセットする。

$ git submodule foreach --recursive git reset --hard

submodulenew commits をリセットする。

$ git submodule update --init

ローカルの状態を確認する。

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

タグ/ブランチの使い方

タグ

リモートのタグを取得

$ git pull --tags

Already up to date.

タグの一覧表示

$ git tag

wxPy-2.8.0.1
wxPy-2.8.0.2
...
wxPy-3.0.1.1
wxPy-3.0.2.0

現在のブランチの直前のコミットに対してタグを付ける

$ git tag wxPy-3.0.2.0

タグをチェックアウトする

$ git checkout wxPy-3.0.2.0

Updating files: 100% (12896/12896), done.
Note: switching to 'wxPy-3.0.2.0'.

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.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 67db99581e set revisions for externals

$ git branch

* (HEAD detached at wxPy-3.0.2.0)
  master

タグからブランチを作成する

$ git checkout -b wxPyhton-3.0.2.0

Switched to a new branch 'wxPyhton-3.0.2.0'
 

$ git branch
  master
* wxPyhton-3.0.2.0

ブランチ

現在のブランチを表示

$ git branch

* master

リモートブランチの一覧表示

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/SOC2007_UNITTEST
  ...
  remotes/origin/wxPy-2.9.3.x
  remotes/origin/wxPyhton-2.9.2

タグ(wxPy-3.0.2.0)からブランチ(wxPyhton-3.0.2.0)を作成する

$ git checkout -b wxPyhton-3.0.2.0 wxPy-3.0.2.0

トラブルシューティング

編集もしていないのにステータスで確認すると modified: が出続ける

$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   .eslintignore
	modified:   .eslintrc
	modified:   .github/PULL_REQUEST_TEMPLATE.md
	modified:   tool/unicode.js
	modified:   tool/update_deps.js
	modified:   tool/wrap_keyword_regexp.js

no changes added to commit (use "git add" and/or "git commit -a")

編集を破棄する為に以下を実行してもステータスで確認すると modified: が出続ける。

$ git checkout .
$ git checkout -- *
$ git reset --hard origin/master

差分を確認するとファイルパーミッションの変更まで検知している。

$ git diff .eslintignore

diff --git a/.eslintignore b/.eslintignore
old mode 100644
new mode 100755

解決策はファイルパーミッションの変更を無視する。

$ git config core.filemode false
$ git config -l

.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
remote.origin.url=git://github.com/ajaxorg/ace.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

$ git status

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

参考文献

【 git clone 】コマンド――Gitのリポジトリを複製する:Linux基本コマンドTips(381) - @IT
git pull を強制し、リモートでローカルを上書きする方法 | WWWクリエイターズ
How do I revert my changes to a git submodule? - Stack Overflow
How to make git clone faster with multiple threads? - Stack Overflow
shallow clone
git リポジトリの最新の履歴だけを取得する shallow clone - Qiita
git で shallow clone - Qiita
git: shallow cloneしてすぐunshallowする - pockestrap
Git - git-clone Documentation

パーシャルクローンとシャロークローンを活用しよう - GitHubブログ
git のクローンを最小量にする(サブモジュールを含めたshallow clone) - Qiita
Git で shallow clone するときに全ブランチの最新履歴を取得する
Git サーバーを CentOS 7 で構築する方法と設定手順 - サーバー構築と設定 ~初心者にも分かりやすく解説~
ベアリポジトリとノンベアリポジトリ:理論編〜GitでWordpressのテーマを管理 - トリコロールな猫
ベアリポジトリとノンベアリポジトリ:実践編〜GitでWordPressのテーマを管理 - トリコロールな猫
これでもう怖くない、Git/GitHubにおけるリモートリポジトリの作成、確認、変更、更新時の基本5コマンド
git reset —hardしてしまった時に元に戻す方法

付録

git の shallow clone についてまとめておこうかなぁ🤔
Gitのベアリポジトリとノンベアリポジトリについて🤔