How do I move the git repository from one remote to another without the history?

206 views Asked by At

My source code is currently hosted on VisualStudio.com (VSTS) in a Git repository. The client has asked that after every sprint the code should be updated in his owned repository in bitbucket without the long history that I may have on VSTS.

The firs time I did this in a very crude way. I retrieved all code from the dev branch to a temp folder, deleted Git folder and then performed git init and added all the files to client's bit bucket repository.

Now I already have source in both locations. Is there an easier way to achieve this? Overwriting the files is not making them marked as changed but as deleted and added in bitbucket.

so I can think of these steps. - Get the latest version from the branch - Squash the history locally ??? - Set the remote to client's repo??? - perform a commit ???

I had to write these steps because I think people are thinking it's the first commit I'm making in his repo. No, I already had the first commit. this question is about subsequent commits.

2

There are 2 answers

2
Rudi On

You can git merge --squash, which fold all your changes into a single commit. With this method git does a regular merge, but does not take your branch as a parent for the new commit. The squashed commits will be placed into the commit message, so you might want to edit this too. With this approach you don't loose your history, while you can fulfill your customers requirements.

As a side note, I never worked with squash commits, so I can't tell if there are side effects like recurring merge conflicts since git does not know about the same ancestors when you do changes on an already merged branch.

This is an example how the history would look like:

$ git log --graph --all
* commit e8f2e951e35d27a3f2337dc8f9f4f45bfce40620 (HEAD -> master)
| Author: Rudi <[email protected]>
| Date:   Mon Jan 6 08:44:43 2020 +0100
| 
|     Squashed commit of the following:
|     
|     commit b7128a9e393e1dde70ca1191068fe6cce9e6935b
|     Author: Rudi <[email protected]>
|     Date:   Mon Jan 6 08:44:28 2020 +0100
|     
|         sfd
|     
|     commit d7536eab83d540275cbe8355ecb6cc3f31bbd270
|     Author: Rudi <[email protected]>
|     Date:   Mon Jan 6 08:44:27 2020 +0100
|     
|         sfd
|     
|     commit de9880d2b7304d65d6f43d659c5494a09e97fcf4
|     Author: Rudi <[email protected]>
|     Date:   Mon Jan 6 08:44:24 2020 +0100
|     
|         sfd
|   
| * commit b7128a9e393e1dde70ca1191068fe6cce9e6935b (work)
| | Author: Rudi <[email protected]>
| | Date:   Mon Jan 6 08:44:28 2020 +0100
| | 
| |     sfd
| | 
| * commit d7536eab83d540275cbe8355ecb6cc3f31bbd270
| | Author: Rudi <[email protected]>
| | Date:   Mon Jan 6 08:44:27 2020 +0100
| | 
| |     sfd
| | 
| * commit de9880d2b7304d65d6f43d659c5494a09e97fcf4
|/  Author: Rudi <[email protected]>
|   Date:   Mon Jan 6 08:44:24 2020 +0100
|   
|       sfd
| 
* commit 6c44f29c30d57329af492e85e56f9c4672fcbf34
  Author: Rudi <[email protected]>
  Date:   Mon Jan 6 08:44:09 2020 +0100

      sfd
3
bahram On

following the below steps:

-- Remove the history from 
 rm -rf .git

 -- recreate the repos from the current content only
 git init
 git add .
 git commit -m "Initial commit"

 -- push to the github remote repos ensuring you overwrite history
 git remote add origin [email protected]:<New Repository>
 git push -u -f origin master

Note : rm -rf .git removing you .git folder and git push -u -f origin master replace any files in your new repository with current files