0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

過去にさかのぼって git のフォルダ階層を変更する

Last updated at Posted at 2023-11-30

複数のレポジトリを1つにまとめるなどで、

  • ディレクトリを移動したい
  • 特定のディレクトリを削除したい
  • リネームしたい
    など

特定のディレクトリをルートにしたい

app ディレクトリの内容を
root
+ app
  + include
  + src

ルートに持っていきたい
root
+ include
+ src

にしたい。

Git コマンド
> git filter-branch -f --subdirectory-filter app/ -- --all

ディレクトリを掘って一階層 下にしたい

親ディレクトリの内容を
root
+ include
+ src

app フォルダを作成して、そこに置きたい
root
+ app
  + include
  + src

にしたい。

Git コマンド
> git filter-branch -f --tree-filter "mkdir app && git mv -k * app/"

特定ディレクトリを削除したい

temp ディレクトリを
root
+ include
+ src
+ temp

削除したい
root
+ include
+ src

にしたい。

Git コマンド
> git filter-branch -f --tree-filter "rm -f -r temp" HEAD

ディレクトリ名を変更したい

from ディレクトリを
root
+ from
  + include
  + src

to ディレクトリに変更したい
root
+ to
  + include
  + src

にしたい。

Windows コマンド プロンプト上の git-filter-branch ではうまいやり方を思いつかず。

Git の警告
> git filter-branch
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.

また filter-repo を使えと言われるので、git-filter-repo をインストールした。

git-filter-repo をインストール
> python -m pip install git-filter-repo

Python\version\Scripts にインストールされてしまったのでパスを通す。

パス
> PATH=%USERPROFILE%\AppData\Roaming\Python\Python310\Scripts;%PATH%
Git コマンド
> git filter-repo --force --path-rename-match from:to
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?