0

i want to remove a certain range of commits from a repository and put them into a new repository. for example, i have repo "oldRepo": A-B-C-D-E-F-G-H-I -> "newRepo" B-C-D-E-F

I had an idea with a rebase to solve it, but I got stuck on an error (here is the question about it: Stackoverflow-question) i can't really find anything helpful about this, can anyone help me with a specific command or procedure with this?

  • When you say "extract a commit", do you mean "extract the project state at a commit" or do you mean "extract the changes introduced by a commit"? – j6t Feb 24 '23 at 11:25
  • I want to extract a specific range of commits and the complete data in them. The most important thing is that the history between these commits is present, but no other commits. The repo data should be complete. – MobxLimited Feb 24 '23 at 16:19

4 Answers4

0

Another approach would be to use git cherry-pick, since you can cherry-pick a range of commits (git cherry-pick FIRST^..LAST).

Go to your new repository local clone (with a least one commit in its main branch) and do:

cd /path/to/new/repository
git remote add old /path/to/old/repository
git fetch old

git switch main
git cherry-pick B^..F

You can add a merge strategy option (-Xtheirs for instance) if needed.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

A quick/simple option would be using git replace:

git init new-repo
cd new-repo
git fetch /path/to/old/repo hash-f:branchname
git replace --graft hash-b
git filter-branch -- --all
rm -rf .git/refs/original/
git gc

This pulls in history up to commit F from your old repo and then deletes everything before commit B.

Note that this must assign new commit ids to all your commits (just like using rebase or cherry-pick)

knittl
  • 246,190
  • 53
  • 318
  • 364
0

I found another solution, I use a git fast-export COMMIT_ID..BRANCH and import it again after that, this works very well.

git fast-export commit_ID..BRANCH > export.txt
//switch in new Repo
git fast-import < export.txt
blackgreen
  • 34,072
  • 23
  • 111
  • 129
0

Here's one approach:

  1. Checkout the source branch
  2. Switch to a new branch in the existing repo, e.g. git switch -c beautiful_history_1
  3. git rebase --interactive
  4. squash, remove, etc. shape the history to be what is desired
  5. Confirm the history and the code look the way you want it. If not just repeat steps 1-3.
  6. Change the directory and:
    git clone <original_repo> --single-branch --branch beautiful_history_1
    (you may also want --no-tags)

Clone flags (from git-clone):

--no-tags

Don’t clone any tags, and set remote..tagOpt=--no-tags in the config, ensuring that future git pull and git fetch operations won’t follow any tags. Subsequent explicit tag fetches will still work, (see git-fetch[1]).

Can be used in conjunction with --single-branch to clone and maintain a branch with no references other than a single cloned branch. This is useful e.g. to maintain minimal clones of the default branch of some repository for search indexing.

--[no-]single-branch

Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at. Further fetches into the resulting repository will only update the remote-tracking branch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when --single-branch clone was made, no remote-tracking branch is created.

tymtam
  • 31,798
  • 8
  • 86
  • 126