How do I change a file's path in git's history?

Here is what I have – a git repo of my code:

       |-proj1 (no git repo here yet)
             |-subproj1 <- current git repo here

Here is what I want – a git repo which is now tracking a new project that uses my code:

  • how to end git difftool session (beyond compare )?
  • Creating pull requests in gitlab
  • Git: How to rebase many branches (with the same base commit) at once?
  • OSX: git: remote: Permission to user/repo denied to otheruser
  • integrating Git Bash with Visual Studio
  • SVN checkout forbidden with all the tools
  • projects
           |-proj1 <-git repo moved to here, but still tracking files in subproj1
                 |-subproj1 (no git repo here)

    I’d like to keep the history intact and therefore the new repository will be referring to files that are one level deeper than the original. What is the most pain free way to do this?

  • Git 4-way merge
  • In Jenkins, how to checkout a project into a specific directory (using GIT)
  • Git delete commit but don't delete the changes
  • Gitlab - Can not pull from remote after merge requests accepted
  • Git: getting the ref of a commit by day?
  • Using .gitignore to not commit bin, debug and config folders
  • 2 Solutions collect form web for “How do I change a file's path in git's history?”

    Rewriting history can be done with the git filter-branch command. In fact, moving a directory tree into a subdirectory is one of the cut&paste-ready examples given in the git filter-branch manpage:

    git filter-branch --index-filter '
      git ls-files -s |
      sed "s-\t\"*-&subproj1/-" |
      GIT_INDEX_FILE=$ git update-index --index-info &&
      mv $ $GIT_INDEX_FILE
    ' HEAD

    Just create the directory structure you want inside the repo – i.e. move all files and folders to “subproj1” folder.

    Then stage all added and deleted files and git will work out that they are in fact renames:

    git add .
    git add -u .
    git commit -m "Moved to a subfolder"
    Git Baby is a git and github fan, let's start git clone.