简体   繁体   中英

New git repository from particular commit

I have a Git repository which have n commits, I want to create a new repository from some particular m commit,

Example, Repository A has 10 commits, and from 4th commit I want to create a new Repository B independent of A.

I tried multiple things, but they are not working like git clone and then in the new directory git checkout and multiple stack overflow answers,

How do I create a new git repository from a folder in an existing git repository?

how to create a new git repository from an existing one

but was not able to perform it in terms of commit. Can anyone please help.

Thanks

How it worked for me (git version 2.10.1.windows.1):

git remote add origin <server>
git fetch
git reset --hard <full sha1 of the commit>
git remote remove origin

Clone the repo A. Remove the remote 'origin' if you want it independent. And then (learn how to and) use git rebase -i to select the commits to keep, remove or squash. If you only have 10 commits and that the commits you want to remove have no incidence of later commits, it should work well...

As it often is with git , there are many ways to skin a cat:)

An alternative to the answer above is to add an empty repository as a remote and push the commit using git push other_remote commit:refs/heads/master

Here's a demonstration:

# Set up the parent repo
$ git init repo1
Initialized empty Git repository in /tmp/repo1/.git/
$ cd repo1
$ seq 10 | xargs --replace git commit -m 'commit {}' --allow-empty
[master (root-commit) 7444793] commit 1
[master 6b12c35] commit 2
[master 3743f03] commit 3
[master b4221a7] commit 4
[master f7e1009] commit 5
[master 4c8e4e9] commit 6
[master 6618f10] commit 7
[master a1c1b26] commit 8
[master 802bed2] commit 9
[master 13734f2] commit 10
# Set up the new repo
git init ../repo2
# Allow pushing to the master branch
git -C ../repo2 config receive.denyCurrentBranch ignore
# Add the other repo as a remote
git remote add other_remote ../repo2
# Push the commit 5-back to repo2's master branch
# Note you can use anything that resolves to a refish like thing (a branch, a commit, a tag, etc.)
git push other_remote HEAD^^^^^:refs/heads/master 
# Show our handywork
$ cd ../repo2
$ git log --oneline
f7e1009 commit 5
b4221a7 commit 4
3743f03 commit 3
6b12c35 commit 2
7444793 commit 1

Let's say the commit history of master in Repo A is ABCDE . The goal is to create Repo B with a branch pointing to C .

git init RepoB
cd RepoB
git fetch <path_of_RepoA> master
git checkout -b master C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM