How to change a fresh git repository into a branch from another repo?

I downloaded a 3rd party project as non-tracked files (without .git folder) and started a new git repository with $git init in it, to track my changes.

I later found out that this project already has a public git repository, which has the same state as the bare files I downloaded (is identical to my initial commit).

  • git: avoiding merge conflicts in git-notes
  • Change old commit message on Git
  • git undo only changes to some files in a commit
  • Git remote for testing
  • How does git handle commits in a merge?
  • Why is my key w/ fingerprint not authorized when I try to push latest changes to Heroku?
  • What I should have done:

    Cloned the remote repo and made a new local branch and then pushed it to remote.

    What I actually did:

    Just took the files and started a clean local git repo (master) in it and made several commits.

    What I want to do:

    Turn my local initial commit into a branch of the remote repo (change its parent) and keep the changes I’ve already done.

    I guess this should be possible, but I don’t know enough Git.

    Thanks!

  • Git log graph showing commits per branch in one place
  • Creating branch inside a branch in git
  • Is it possible to easily change the default conflict markers in Git?
  • Merge or Rebase or Branch Default In Android Studio? What are the differences?
  • Best way to share Github pull request templates between projects?
  • git: 'log master..origin/master' not behaving as expected
  • 2 Solutions collect form web for “How to change a fresh git repository into a branch from another repo?”

    Well, you could try the following (you may want to use a different name than origin, up to you):

    1. git remote add origin url_of_project_repository
    2. git fetch origin
    3. git rebase FETCH_HEAD

    You have significant chances git will actually figure ot your base and the new remote’s are actually the same set of files, and will resolve the fake merge conflict by skipping anything but your changes.

    If that doesn’t work peroperly, you could also juts clone the “real” repository somewhere else, then re-apply your changes on top of it (and there would be countless ways of doing this, from a very manual DIY way to some more automated means).

    You can add your repo as a remote to the original one, fetch the commits, and try to rebase.

    Git Baby is a git and github fan, let's start git clone.