Skip to content

OscarBennich/git-tips-and-tricks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

Git Tips & Tricks

How to split code from a repository into a new one and only keep relevant history

Expanded from these instructions.

The Git-filter-repo tool.

  1. Follow these instructions and download the git-filter-repo script and put it somewhere on your machine - also make sure you have Python installed.

  2. Create a new branch in the repo you want to move

  3. Make a fresh clone of the repo from this branch (a fresh clone is required for the git-filter-repo tool):

    git clone --single-branch --branch {{BRANCHNAME}} {{ORIGINAL-REPO-ADDRESS}}
    
  4. Figure out what folders & files that need to be moved to the new repo

    Note: If you move a specific file, any folder(s) it is part of will be automatically added

  5. Open the script in the root of this repo and add the path(s) from the root of the cloned repo to the files and folders

    Note: Windows users should use '/' to delimit folders (not '\')

  6. CD into the cloned repo and run the script from inside it:

    .\{{RELATIVE-PATH-TO-UTILITY-SCRIPT}} {{PATH-TO-GIT-FILTER-REPO-SCRIPT}}
    

    For example:

    .\..\GitMove-MultiPath.ps1 C:\Users\my-user\source\repos\test\git-filter-repo
    
  7. Create a new repository

  8. Push these changes to the new remote:

    git push {{NEW-REPO-ADDRESS}} {{BRANCH-NAME}}
    
  9. Create a main branch from this branch in the new repo, delete the old branch in the new remote

  10. Set up build validation, policies, etc.

This new repository will contain only the relevant commit history for the files you chose to include

Keep in mind that the commit hashes will be changed

Also note that renames are NOT followed, meaning that if a file has moved into the folder you are filtering out, then the commit history before the move will be gone (unless you include the previous file location too)

image https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html

Optional:

  • Delete the files in the old repo and push these changes to the trunk if you do not want to keep the files in both repos

How to duplicate a repository without forking it

If you want to create a new repository from the contents of an existing repository but don't want to merge your changes to the upstream in the future, you shouldn't fork it but instead duplicate it.

Go here or here for specific instructions on how to do this.