D
D
Derafer2015-06-22 10:47:04
git
Derafer, 2015-06-22 10:47:04

How to copy certain git branches between repositories while preserving history and changing authorship?

There are two git repos repo1 and repo2 . In the first repository there are two branches public and privat , in the second only public . What is the right way to copy the first repository to the second one so that only the public branch without privat is copied , saving all commits and changing the authorship to something like autobot?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vyachin, 2015-06-24
@vyachin

1. Clone repo1 to which thread folder git clone repo1-url
2. Change the author's commits here as https://help.github.com/articles/changing-author-info/
3. Connect 2 repository git remote add repo2 repo2-url
4. Push the public branch to the second repository git push -u repo2 public

I
Igor Deyashkin, 2015-09-07
@Lobotomist

I understand that this operation is not a one-time operation, but should occur regularly. That is, your repo2 should always contain the current version of the public branch with commits without attribution.
When pushing to repo1/public, you can execute code that will transfer all new commits to the repo1/public-bot branch, changing their author and pushing to repo2/public.
Before that, you need to manually prepare this branch (repo1 / public-bot), that is, so that the code in it matches the current repo1 / public and there are no conflicts when transferring commits. Here you can use the link given by vyachin .
Below I will write an implementation example, but it will most likely not work correctly if this branch has merge commits or if it is rebased. So, it still needs to be added.
In the repo1 repository, hang the post-receive hook. Hook Documentation

#!bash
 
while read oldrev newrev refname
do
  # выполняем код только при обновлении ветки public
  if [ $refname = refs/heads/public]
  then
    git checkout public-bot
    # получаем список новых коммитов
    commits=$(git log --oneline --reverse test..master --format="%h")
    for rev in $commits
    do
      git cherry-pick $rev
      git commit --amend --no-edit --author="bot <[email protected]>"
    done
  fi
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question