0
2

More than 1 year has passed since last update.

【git】ローカルにbare repository

Posted at

はじめに

これまでGithub や Bitbucket などをremote repository としてgit push していたのですが、まぁ、そういうものが使えないときもあります。ローカルに作業を残しておくために、ローカルにremote repository を作りたい(用語は合っているかな^^;)と思って調べたら、簡単にできました。のでメモ。

内容

確認作業は、WSL2 の ubuntu20.04 で行いました。

bare repository を作る

適当なディレクトリにbare repository を使います。

$pwd
localrepo
$ git init --bare remote.git
Initialized empty Git repository in /home/user/localrepo/remote.git/
$ ls remote.git/
HEAD  branches  config  description  hooks  info  objects  refs

とすると、remote.git という名前のディレクトリができました。

push する

何か既存の作業しているlocal repository があればremote をここに書き換えるだけでよいですが、今は、適当なものを作ります。localrepo/remote.git/ とは別の所workdir で以下のようにします。

$ git init
Initialized empty Git repository in /home/usr/workdir/.git/

ここでとりあえず適当にcommit します。

$ echo "A" > a.txt
$ git checkout -b work
Switched to a new branch 'work'
$ git add -u a.txt
$ git commit -m "add a.txt"
[work (root-commit) aed953d] add a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt$ git log
$ git log
commit aed953d4f506ff315c2652f641285bb870dda89a (HEAD -> work)
Author: aiu.eo <aiu@eo>
Date:   Mon Oct 17 22:24:55 2022 +0900

    add a.txt

remote repository は登録されていません。

$ git remote -v

先程作成した bare repository へpush します。相対パスで指定して、動きました。

$ git push -u ../localrepo/remote.git work
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 194 bytes | 194.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../localrepo/remote.git
 * [new branch]      work -> work
Branch 'work' set up to track remote branch 'work' from '../localrepo/remote.git'.

origin に登録しました。ここでも相対パスで登録できました。作業レポジトリで別のディレクトリにいても、動きました。

$ git remote add origin ../localrepo/remote.git
$ git remote -v
origin  ../localrepo/remote.git (fetch)
origin  ../localrepo/remote.git (push)
$ mkdir a
$ cd a/
$ git fetch
From ../localrepo/remote
 * branch            work       -> FETCH_HEAD

参考

ググって出てきたもの:

https://blog.serverworks.co.jp/tech/2019/08/30/post-73550/
https://hirocorpblog.com/git-clone-bare/

まとめ

とりあえず、個人の作業用に、push する先としてのrepository は作れた。これで明日も生きていけるかな。

todo: 本家の解説、あれば読みたい。
(2022/10/17)

0
2
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
0
2