开发者

Move .git folder up in working tree hierarchy

开发者 https://www.devze.com 2023-02-12 22:03 出处:网络
Yesterday I did one of these operations (see code snippet below) on my git repo in order to effectively move my project up a few folders in the folder hierarchy. This basically lists all files and add

Yesterday I did one of these operations (see code snippet below) on my git repo in order to effectively move my project up a few folders in the folder hierarchy. This basically lists all files and adds a prefix folder name to them and updates the index accordingly.

Old layout:

+ Root
  - Sublevel 1
    - Sublevel 2
      - .git was here

New, desired layout:

+ Root
  - .git moved to here, so prefix all index files with "Sublevel 1/Sublevel 2/"
  - Sublevel 1
    - Sublevel 2

Command:

git filter-branch --index-filter '
    git ls-files -s | sed "s-\t-&newRoot/sub1/sub2/-" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE
    ' HEAD

However, this command, unbeknownst to me, only modified the master branch. I have about 8 other branches off of master. When I checkout one of these other branches, it checks out all files (some 11,000) in my repo in their original folder structure at the new root folder level where I moved the .git folder to.

How do I repair this snafu? The damage is already done. I have a backup of the .git folder before I did ran the filter-branch but I have commits after t开发者_如何学Chis backup was made, not many but enough to warrant finding a patch-like fix on the current repo. I don't see any obvious branch-specific arguments to the filter-branch command.


filter-branch will "rewrite only the positive refs mentioned in the command line". So you need to specify "--branches" to rewrite all branches. You specified HEAD, so it just rewrote HEAD (i.e. master)

After you run filter-branch, the original refs are backed up in refs/original (i.e. refs/original/master is master before filtering): the objects are still available, and I believe the reflogs are kept intact too, so recovering from a bad filter-branch operation is fairly straightforward.

So your options are:

  • Undo the filter-branch command by checking out master and doing git reset --hard refs/original/master

  • Filter the other branches so they are transformed as well: redo the filter-branch command but specify --branches --not master instead of HEAD, or more safely by naming the branches to be modified explicity

0

精彩评论

暂无评论...
验证码 换一张
取 消