linkyndy
linkyndy

Reputation: 17928

Move all my commits from one repo to another

I committed a few times to a common repository, together with several other authors. Now I want to build my own repository, out of only my own commits. I am not really sure how to start, since I need to keep the original history intact.

Upvotes: 4

Views: 5702

Answers (3)

Rohit Poudel
Rohit Poudel

Reputation: 1887

Make a file ready to Move:

Make a copy of repository A so you can mess with it without worrying about mistakes too much. It’s also a good idea to delete the link to the original repository to avoid accidentally making any remote changes (line 3). Line 4 is the critical step here. It goes through your history and files, removing anything that is not in directory 1. The result is the contents of directory 1 spewed out into to the base of repository A. You probably want to import these files into repository B within a directory, so move them into one now (lines 5/6). Commit your changes and we’re ready to merge these files into the new repository.

 1. git clone <git repository A url>
 2. cd <git repository A directory>
 3. git remote rm origin
 4. git filter-branch --subdirectory-filter <directory 1> -- --all
 5. mkdir <directory 1>
 6. mv * <directory 1>
 7. git add .
 8. git commit

Merge file into new repository:

Make a copy of repository B if you don’t have one already. On line 3, you’ll create a remote connection to repository A as a branch in repository B. Then simply pull from this branch (containing only the directory you want to move) into repository B. The pull copies both files and history. Note: You can use a merge instead of a pull, but pull worked better for me. Finally, you probably want to clean up a bit by removing the remote connection to repository A. Commit and you’re all set.

 1. git clone <git repository B url>
 2. cd <git repository B directory>
 3. git remote add repo-A-branch <git repository A directory>
 4. git pull repo-A-branch master
 5. git remote rm repo-A-branch

Moving the commits from A to B repository

  1. Check the whole tree.

    git log --all --oneline --graph --decorate --abbrev-commit

  2. copy (cherry-pick) the commits from the A repo into B repo.

    git cherry-pick sha-of-commit-one git cherry-pick sha-of-commit-two git cherry-pick sha-of-commit-three

  3. check your local repo is correct.

    git log

  4. send your new tree (repo state) to new repository.

    git push origin master

  5. remove the now-unneeded reference to oldrepo A.

    git remote remove oldrepo A

for particular author moving comments from A to B Repo

  1. Find all commits by author and save their hash to a file:

    git log --author=<author> --format=%H > /tmp/commit-by-author

  2. Create a new branch that does not contain this particular's author commit . For this, you can create a new empty branch:

    git checkout --orphan commits-by-author

  3. Cherry-pick all commits of that author (from A to B Repo):

    tac /tmp/commit-by-x | while read sha; do git cherry-pick ${sha}; done

Upvotes: 0

Mateusz Chrzaszcz
Mateusz Chrzaszcz

Reputation: 1280

This is quite unorthodox issue.

Is this common repository some sort of a project or loose set of unrelated files?

If the files are unrelated (by that I mean you were not working on someone else's code/files) then you can filter and gather your commits using combination of git filter-branch and git cherry-pick followed by git format-patch that you can later apply on top of your new repo.

Roughly it might look like this:

  1. Find out hashes of your commits with for instance with git log. Let's say one commit has SHA equal to b33787c
  2. Cherry pick it with git cherry-pick b33787c
  3. Create patch for this commit: git format-patch -1 b33787c. This will result in a creation of 0001-COMMIT-NAME.patch that you can later reuse.

If, however, the files you were working one belong to one cohesive project I do not think it can be done in a way this code would be useful/functional after such operation.

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 142642

You can create a new repo and than filter out the commits where you was the author with the git filter-branch

something like:

git filter-branch --commit-filter '
    if [ "$GIT_COMMITTER_NAME" = "<Your name>" ];
    then
        # use this commit 
    fi' HEAD `

Upvotes: 0

Related Questions