0

I have a code in a GIT repository repA and I need to move it to another repository repB that is currently empty but having now the commit and tag history of repA in repB.

Any advice on the workflow/commands for this process?
Thanks in advance!

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Leo
  • 3
  • 1
  • The commits *are* the history, so anything that copies the commits, copies the history. The remaining problem is to copy any *names* (branch and/or tag names). Since Git uses the names to *find* the commits, you actually have to solve the second problem first, which then immediately solves the first problem for you. CodeWizard's script will suffice for most users with most repositories. – torek Feb 25 '17 at 18:55

1 Answers1

4

You simply need to add new remote and than push your code to the new repo

git remote add origin2 <url>
git push origin2 <branch name>

Here is a scrip which im using to checkout all branches and than push them to the new remote

# first add the new rmeote
git remote add origin2 <new-url> 

#!/bin/bash

# loop over all the original branches
# 1. check them out as local branches 
# 2. set the origin as the track branch
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
    git branch --track ${branch#remotes/origin2/} $branch
done

# now push all branches and tags
git push origin2 --all    
git push origin2 --tags

What does the script do?

git branch -a
get a list of all the local branches

| grep remotes The branch names are : 'remotes/origin/' so this will remove the remotes from the branch names

| grep -v HEAD | grep -v master
remove the master (current branch) and HEAD which is an alias to the latest commit

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167