Search code examples
gitgit-mergegit-rebasegit-pull

Git pull into an untracked copy of repo


So in an ideal world, when I started this project, I would have cloned from a source, and then committed all my changes to this repo. When the main project was updated, I could pull those changes into my repo. (This is one way traffic, no pushing going on.)

However, when I started, I did not clone, but simply downloaded a released ZIP of the code, unzipped it, and then did a git init. I have since committed all my changes to this copy of the code. However, the main codebase has now been updated, but as my copy is not a merge, but my own repo of the same base code, I cannot merge the master's changes into mine.

Is there any way of getting these changes from the main code into my repo without having to start again?


Solution

  • You can fix your repository by rebasing the changes of your private repository on top of the remote changes.

    • add a new remote to your repository containing the correct remote git URL.
    • fetch from that remote to fetch all the correct remote commits to your own repository
    • use git rebase to transplant you commits on top of the remote commits.
    • merge your branch into the remote one.

    This would be something like:

    cd $your_git_repository
    git remote add origin $URL
    git fetch origin
    git branch -m my_branch
    git rebase --onto $base_commit $your_first_commit my_branch
    git merge $remote_branch
    git log --graph --decorate --oneline
    

    where $base_commit is the commit corresponding to the zip you downloaded in the first place, and $your_first_commit is the commit you commited the extracted zip file as.