Search code examples
gitmergerebase

Correct procedure to merge two repositories Git


I'm trying to understand what's the correct and best way to merge differente istances of the same code into a git repostory.

I have the same code opened on two PCs with different local repositaries. I have modified this code in different aspects (teoretically they won't overlap/create issues with each other) and I want to merge the two codes in one which will contain both edits.

Now, I'm reading about the rebase command as well as the merge command. It seems that rebase could better fit my use case as the merge is used once more branch on the same repositary need to be merged.

How would I proceed having two different repositary that need to be merged?

The repositaries are both locals


Solution

  • You need to know what you're doing. Following is a bit more advanced way of how I would do this. Pro's, please don't judge hard - there might be simpler ways, of course.

    1. Setup git repository on some server. GitHub, for example.

    2. Add remote to both local repositories:

      git remote add github git@github.com:user/repo.git

    3. Push changes from one of your local repositories to remote:

      git add *
      git commit -m "first commit"
      git --set-upstream-to=github/master
      git push
      
    4. Commit changes in your other local repository and merge latest code from remote:

      git add *
      git commit -m "second commit"
      git fetch github
      git merge github/master
      # resolve conflicts
      
    5. Push changes from other local repository to remote

      git --set-upstream-to=github/master
      git push
      
    6. If your PC#1 needs to stay in sync and no changes yet done after PC#2 pushed something, just pull the changes:

      git pull
      

    Hope this helps. In general, I would recommend to think about these situations in advance and setup a repository from the very beginning.