4

I have a Java Project on Github, now I changed it to use Gradle and had to change the whole project structure. For this I created a whole new Java Project and copy pasted the code files. As I was expecting I can't just merge the project to the master branch on Github. Is there a way on how to overwrite the old master branch? I dont need the old structure, the code is the same. I also dont want code duplicates on different folders. How can I solve this?

Summary

I want to overwrite the old code with the new code because this is a complex multi-project solution with shared code and multi-platform and there are a lot of conflicts that would be so much more easy to solve if I just keep the new code and throw the old code away.

El Mac
  • 3,256
  • 7
  • 38
  • 58

2 Answers2

3

So, lets say you have the old master as the master branch in your local repository and the new master branch as new-master. You want to merge the changes from new-master into master and always take the changes introduced by new-master when there is a conflict. To do this, execute the following:

  • git checkout master
  • git merge -X theirs new-master

Afterwards, master will contain all changes made in new-master. Now, you can simply push the master branch to github. You will not have lost any version history.

Description of -X theirs from the git-merge man page.

ours
    This option forces conflicting hunks to be auto-resolved cleanly by
    favoring our version. Changes from the other tree that do not conflict
    with our side are reflected to the merge result. For a binary file, 
    the entire contents are taken from our side.

    This should not be confused with the ours merge strategy, which does
    not even look at what the other tree contains at all. It discards
    everything the other tree did, declaring our history contains all
    that happened in it.

theirs
    This is the opposite of ours.

If you want to delete the history and start over with the new repository, you need to force push the new local repository to the github repository.

git push --force origin master

See also:

Please be sure to understand what you are doing before executing the command. It is generally a bad idea to force push to a public repository. Especially, if you are not the only developer.

Community
  • 1
  • 1
Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
  • I have the new project structure in a new branch (already on GitHub). I guess I have to check it out first? Thanks. PS: It's only me working on a private branch. – El Mac Feb 10 '16 at 16:43
  • If you already have the changes in the same repository, you should just merge the branch into master. This will change the project structure there. There is no need to delete the project history. The reason to use git is to keep the history. – Stefan Dollase Feb 10 '16 at 16:46
  • I dont want to delete the history, but I just got so many conflicts I tought it would be easier to just overwrite the old stuff... and keep the new one – El Mac Feb 10 '16 at 16:46
  • 1
    Okay, in this case my answer will not help you. I will edit it. – Stefan Dollase Feb 10 '16 at 16:49
2

Here is my suggestion -

  1. Import your old master branch from origin
  2. Switch the branch and name the branch "new master" or something
  3. Make your changes in "new master"
  4. Now merge new master and master on your local
  5. Resolve the conflicts
  6. Build it, if successful push it.

Or -

1) Just make the changes in new master 
2) Push it as a new branch 
3) Use it as the origin for your future forks.

you can deprecate old master, in this way you'll have history of the changes as well, the only thing that would change is your URI of origin

to rename you branch -

  git branch -m old_branch new_branch
  git push origin new_branch
Shek
  • 1,543
  • 6
  • 16
  • 34
  • Thanks, I already did this. I am stuck at step 5: Resolve the conflicts. This is why I tought it might be easier to just overwrite the old branch. – El Mac Feb 10 '16 at 16:45
  • overwriting is sometimes frowned upon, but if conflicts are impossible to resolved, you might as well nuke the old code and force push it to origin. – Shek Feb 10 '16 at 16:48
  • Make sure your team members are aware of this, otherwise this will be a disaster and you can get fired. – Shek Feb 10 '16 at 16:49
  • They are aware. It's just me doing my own project in my free time, so I hope I dont get fired. If I force push it, will I have for example two files (from the new and old project) in different folders (old and new folder structure)? – El Mac Feb 10 '16 at 16:52
  • 1
    Or just make the changes in new master and push it as a new branch and use it as the origin for your future forks, you can deprecate old master, in this way you'll have history of the changes as well, the only thing that would change is your URI of origin. – Shek Feb 10 '16 at 16:56
  • thank you, this is indeed the solution I will use. I just made a new branch "master-gradle" and am using it as default now. Works perfectly. If you update your answer I'll mark it. One more thing: Do you know if I can mark the old branch as deprecated? Or at least rename it "master-deprecated"? I don't want to loose the history, and maybe renaming it is against the principles of a git repo tho. – El Mac Feb 14 '16 at 16:34