LoginSignup
0

More than 1 year has passed since last update.

git-filter-repoでフォルダー移動をするという歴史改竄をする

Last updated at Posted at 2022-06-07

元ネタ

git-filter-repoというのを知らなかったので、使ってみます。

git-filter-repoとはgit filter-branchの代替ツールで高速に安全に操作できます。

やってみた

移動するときに同名のファイルが有って衝突するケース

以下簡単のために、git filter-repo --forceしていますが実際には--forceせずに新たにcloneして実施してください。

$ cd filter_test/
$ git init
$ echo "aaa" > a.txt
$ ls
$ mkdir foo
$ echo "aaaa" > foo/a.txt
$ echo "aaa" > foo/b.txt
$ echo "aaa" > foo/c.txt
$ ls
$ ls foo/
$ git add a.txt
$ git status
$ git commit -m "init"
$ git add foo/
$ git commit -m "1"
$ echo "bbb" >> foo/b.txt
$ echo "bbb" >> foo/c.txt
$ git status
$ git add foo/
$ git commit -m "2"
$ echo "ccc" >> foo/d.txt
$ echo "ccc" >> foo/c.txt
$ git status
$ git add foo//
$ git status
$ git commit -m "3"
$ echo "ddd" >> foo/d.txt
$ git status
$ git add foo/
$ git commit -m "4"
$ git log --oneline --graph --all
* b26d203 (HEAD -> main, tmp) 4
* ba31090 3
* f44398b 2
* c248b60 1
* 56f1c22 (tmp2) init
$ ls
a.txt  foo/
$ ls foo/
a.txt  b.txt  c.txt  d.txt
$ git filter-repo --force --path-rename foo/:
Parsed 5 commits
New history written in 0.46 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 4196cda 4
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 12 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (16/16), done.
Total 16 (delta 3), reused 11 (delta 2), pack-reused 0
Completely finished after 1.32 seconds.
$ ls
a.txt  b.txt  c.txt  d.txt
$ cat a.txt
aaaa
$ git log --oneline --graph --remotes --all
* 4196cda (HEAD -> main) 4
* 84e7d81 3
* b793236 2
* 4684caf 1
* 9e1d3cf init
$ echo "aaa" > a.txt
$ echo "aaaa" > foo/a.txt

していて、結果が

$ cat a.txt
aaaa

ですから、移動によって上書きされるようです。なので上書きされたくない場合は予め

$git filter-repo --force --path foo/a.txt --invert-paths
Parsed 5 commits
New history written in 0.34 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at f075883 4
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 12 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (19/19), done.
Total 19 (delta 2), reused 0 (delta 0), pack-reused 0
Completely finished after 1.30 seconds.

とかしておくと良いでしょう。

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