Tuesday, June 21, 2016

Git: How to move a folder to a new empty repository, with history included



http://cronicasdelula.blogspot.nl/2016/08/kotlin-incluye-codigo-muerto-en-el.html


I will not write an introduction of why we need to do this. This is how I did it, not sure if it is the best way but worked 100%:

I used the git commands described in this great article https://help.github.com/articles/remove-sensitive-data/

  1.  Two repos: REPO_OLD with the folder I want to move, REPO_NEW an empty repository where I will move the folder
     
  2. Copy everything (.git folder included) from REPO_OLD to REPO_NEW (another way is to clone REPO_OLD into a new folder REPO_NEW)
     
  3. DONT FORGET THIS: Remove the origin in REPO_NEW: git remote rm origin
     
  4. Delete in REPO_NEW all folders you DONT need. E.g. folder name is FOLDER then

    git filter-branch --force --index-filter ' git rm --cached --ignore-unmatch -r  FOLDER/ ' --prune-empty --tag-name-filter cat -- --all

    do it with all the folders one by one, at the end only the folder you need should be in REPO_NEW. If some folders still exist it is due to some files were not added to git, you can delete the folders manually, they are not in the git history anymore.
     
  5. Now set origin in REPO_NEW to the remote repository where you want to push this new repo.
     
    E.g. git remote add origin git@bitbucket.org:someuser/REPO_NEW.git 
     
  6. Push everything forcing overwrite history: git push origin --force --all
     
  7. Ok, it is copied, but not moved. Just delete the folder in REPO_OLD using the same command you used in step 4.
     
  8. Done



No comments:

Post a Comment