4

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 this 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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

17 Comments

It sounds like the OP just needs to rewrite all branches except master, so that might be doable in the current repository. Still, I'd probably make a separate clone, put master back where it was (from refs/originals), do filter-branch --index-filter ... -- --all to rewrite all refs, and once it all looks okay, grab the commits made after the filter-branch (should be possible with a rebase --onto).
@poke: Except the OP has work on the post-filter master, so it's a little more complicated.
@Jefromi: It doesn't sound to me that way? @James Dunne: Read my comment again, basically all those are direct commands you can do with git (just add the git before); but make sure to backup first and read about the commands...
@poke: It's a little ambiguous, but he said: "I have a backup of the .git folder before I did ran the filter-branch but I have commits after this backup was made" - so I assumed the backup was immediately before, and therefore the commits were after. Could be mistaken.
filter-branch isn't a friendly command-- if you're not comfortable poking around with git's innards, rearranging the file hierarchy might be best done by simply moving things around and committing that as a change in each branch. Still, as mentioned, git reset --hard refs/original/master when you have master checked out should undo the filter-branch command. Then you can take a backup and try filtering again with the --branches option to migrate all the branches at once. Or you can redo the filter-branch naming the other branches to migrate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.