1

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.

Simple Fellow
  • 4,315
  • 2
  • 31
  • 44
  • Take a look at https://stackoverflow.com/questions/29368837/copy-a-git-repo-without-history – funnydman Jan 06 '20 at 06:47
  • Just synchronize the current state of your working folder across the two repositories and make a commit in the client repository. By synchronize I mean both copy over added and modified files, but also making sure files that were deleted in your real repository are deleted in the client repository. You can use `robocopy` for this kind of thing, just ask it to leave the .git folder alone with `/XD .git`, something like `robocopy /E /MIR c:\source c:\client *.* /XD .git` – Lasse V. Karlsen Jan 06 '20 at 07:25
  • But you should clarify one thing. I *assume* you mean that the client repository should in fact maintain a history, just not the *full* history, instead it should contain an abbreviated history containing only the major release points. In that respect you want to do what I suggested above in some variation. If you want a clean repository with only the latest release, do what bahram answered below. – Lasse V. Karlsen Jan 06 '20 at 07:27
  • @LasseV.Karlsen Client wants to have the latest source code at the end of each sprint in his repo just with the clean history. You know we devs make quite many commits. He is not interested in all that. only the final tested code and approved code only without the individual item history. so logically I can think of it like performing a commit on his repo at end of each sprint so his repo will only have history like Sprint1, sprint2, sprint 3 and so on. – Simple Fellow Jan 06 '20 at 07:57
  • @funnydman I read that question before posting mine. My first commit is already there. So no I cannot use the methods mentioned in that answer. My question is related subsequent commits and not the first one. – Simple Fellow Jan 06 '20 at 08:02
  • What I mean is, once the *next* sprint completes, does he want the previous code wiped from his repository, or does he want a "sprint history" so that he can see what each sprint accomplished? – Lasse V. Karlsen Jan 06 '20 at 08:13
  • yes sprint history you can say, but in his repository (bitbucket) not in ours (VSTS). But robocopy isn't related to Git. I couldn't find anything in Git called robocopy – Simple Fellow Jan 06 '20 at 08:19

2 Answers2

0

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 git@github.com:<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

bahram
  • 92
  • 8
  • I wonder can you rename `.git` to `.git.before` so you keep all git history just in case – nonopolarity Jan 06 '20 at 07:03
  • yes it is a safe way but most of the time that client request to clear history is that .git folder became a little heavy and this suggestion not solving this problem. and it this case he has the his repository and not worry about deleted the history, he just want to has clean repository for his client – bahram Jan 06 '20 at 07:14
  • @bahram I already have the first commit in client's repo. I need to do subsequuent commits which is what question is about. – Simple Fellow Jan 06 '20 at 08:03
0

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 <rudi@example.com>
| Date:   Mon Jan 6 08:44:43 2020 +0100
| 
|     Squashed commit of the following:
|     
|     commit b7128a9e393e1dde70ca1191068fe6cce9e6935b
|     Author: Rudi <rudi@example.com>
|     Date:   Mon Jan 6 08:44:28 2020 +0100
|     
|         sfd
|     
|     commit d7536eab83d540275cbe8355ecb6cc3f31bbd270
|     Author: Rudi <rudi@example.com>
|     Date:   Mon Jan 6 08:44:27 2020 +0100
|     
|         sfd
|     
|     commit de9880d2b7304d65d6f43d659c5494a09e97fcf4
|     Author: Rudi <rudi@example.com>
|     Date:   Mon Jan 6 08:44:24 2020 +0100
|     
|         sfd
|   
| * commit b7128a9e393e1dde70ca1191068fe6cce9e6935b (work)
| | Author: Rudi <rudi@example.com>
| | Date:   Mon Jan 6 08:44:28 2020 +0100
| | 
| |     sfd
| | 
| * commit d7536eab83d540275cbe8355ecb6cc3f31bbd270
| | Author: Rudi <rudi@example.com>
| | Date:   Mon Jan 6 08:44:27 2020 +0100
| | 
| |     sfd
| | 
| * commit de9880d2b7304d65d6f43d659c5494a09e97fcf4
|/  Author: Rudi <rudi@example.com>
|   Date:   Mon Jan 6 08:44:24 2020 +0100
|   
|       sfd
| 
* commit 6c44f29c30d57329af492e85e56f9c4672fcbf34
  Author: Rudi <rudi@example.com>
  Date:   Mon Jan 6 08:44:09 2020 +0100

      sfd
Rudi
  • 19,366
  • 3
  • 55
  • 77
  • so i also need to reconfigure the remote origin? – Simple Fellow Jan 06 '20 at 08:07
  • It depends on what you mean with reconfigure and your workflow. The command itself and the commits it creates are standard git objects, so for the usage itself there is no special configuration needed. If you want to push your work branches before (like for backup purposes), and your client does not want to see those working branches, you can add a non-client remote where those working branches can reside. You then develop on those working branches, and merge these changes back onto your client branch. Then you only push the client branches to the client, but keep your work branches private. – Rudi Jan 06 '20 at 11:33