1

Imagine I have master branch. Once new branch was created. Now I want to move exactly this branch to the new repository without comments that was commited before this branch was created.

Picture: 1. Old repository

newbranch    C - D - E
            /
master A - B

Picture: 2. New repository

newbranch     C - D - E
             /
master      Z

I don't need to move newbranch, I tried it but it took all the previous history. I also familiar with rebase that can help me to delete commits. But the point is there are more than 1000 commits I don't want to see in my new repo. I can't delete it manually in rebase mode.

All I need to show people only this newbranch - what was made during this branch, but not the previouses commits.

Big thanks for reply!

vladimir
  • 23
  • 4
  • Can you elaborate on this new repository? Are you creating it from scratch or how is it related to your current repo? Squashing might work here. – Tim Biegeleisen Dec 28 '17 at 08:51
  • I have big repo in which I made my own branch. Now I want to show what I did to other people. I don't want them to see anything else. That's why I making my own repo on bitbucket, want to push there only my branch(what exactly I did) an make permission to particilary people to watch it. – vladimir Dec 28 '17 at 09:01
  • @vladimir If you can reset and redo your new repo, I have edited the answer to propose an alternative to the rebase (which had many merge conflicts): that alternative is much faster and easier. – VonC Dec 29 '17 at 16:31

1 Answers1

0

You could at least create an orphan branch in your new repo. (git checkout --orphan), and make one dummy commit in it.
Or simply create a newbranch from Z (still in your new repo)

This assume that you have your old repo cloned locally.
And that you have created a new repo, also locally.

cd c:\a\path
git init create newrepo
cd newrepo.

First, make sure your old repo is checked out at B (ie the SHA1 representing the commit before your branch)

cd C:\path\to\old\repo
git checkout B

Then import that content into your new empty repo

cd C:\a\path\newrepo
git --work-tree=C:\path\to\old\repo add .
git commit -m "Import old repo as Z"

You can fetch your old repo in that new repo, and rebase your commits on that new branch.

cd C:\a\path\newrepo
git remote add oldrepo C:\path\to\old\repo
git fetch oldrepo

git checkout -b newbranch

git rebase --onto newbranch C~ oldrepo/newbranch

That will recreate only the commits you want in your new repo.


But: the rebase can be problematic, and with many merge conflicts to be resolved manually.

In your case, you could avoid that entirely with git replace or, in your case git graft points, as seen in this example.
Follow the past instructions up to the git fetch oldrepo step, then:

git fetch oldrepo
git checkout -b newbranch oldrepo/newbranch
echo SHA1(C) SHA1(Z) > .git/info/grafts

(Replace SHA1(xxx) by the SHA1 of the commit xxx. Z being the new commit done in the new repo, C being the first commit of your newbranch in the old repo)

That will declare Z as parent commit of C (no merge/rebase required here).
To make that permanent, use git filter-branch:

git filter-branch --tag-name-filter cat -- newbranch
rm .git/info/grafts

You can then push that new rebased branch.

git remote add origin /url/of/new/remote/repo

git push -u origin master
git push -u origin newbranch
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250