简体   繁体   中英

How to move new commits from old repository to new one

So I did a stupid mistake. I migrated my private repos from BitBucket to GitHub recently to bring all my data under one account (simplicity). Deleted the local BitBucket repos and cloned the new GitHub repos on all of my computers. Unfortunately, I forgot to do the same to server that I work in.

My current situation is such. After the migration, I have new commits on both the GitHub repo and the BitBucket repo. I want to move those BitBucket commits onto the GitHub repo, while still retaining the new GitHub commits. It'd be nice if the timing of the BitBucket commits was also retained, but I could be ok without that.

How should I go about doing that? I've seen answers about moving commits from one repo to another, but nothing about attempting to retain the new commits on the new repo. Feel free to direct me to a previously answered question if it's the correct way to do it.

To maintain the old commits (author, date, time etc), you wold have to merge the BB commit into GitHUB. You would do this using a local repo cloned from GitHub. Next, add a remote to point to your BB repo and do a 'git fetch --all'. Now you have all the commits from BB in the same (local) repo as the GitHub commits. Next, create (and checkout) a branch where you would like the BB commits to be merge into. This branch would branch from a GitHUB commit. Then, you can do 'git merge' to merge in the new BB commits into the new GitHub branch. Next remove the BB remote and then push the new branch to the remote GitHUB repo.

If merging won't work, you could use cherry-pick but that does not preserve the BB commits (author, date/time)

The way I went about it is more similar to chepner than Tim, but I'll go over what I actually ended up doing. Note, just as chepner said, this will probably work with any combination of git remote services, so if you're not using BitBucket or GitHub, just do the same things with the other git remote services urls.

Assumptions for these steps (which were true in my situation):

  • the local BitBucket repository is up to date with all the it's latest commits

  • the repository only had a master branch (no sub branches at all)

  • the new GitHub commits edited different files than the new BitBucket commits (so no chance of merge conflicts whatsoever).

See "Things to do Differently" for suggestions on how to tackle this if those assumptions aren't true.

tl;dr Added GitHub remote to the BitBucket local repository, fetched the GitHub commits, and pushed the local repo to GitHub.

Exact Step by Step of What I Did:

  1. Go to BitBucket local repository.

  2. Add a remote to point to your GitHub repo.

    git remote add github <url GitHub repo>

    This can be verified via git remote -v . This will output all the remote urls that are used in that local repository. There should be four entries, two fetch/push for BitBucket, two fetch/push for GitHub:

    $ git remote -v origin <url BitBucket> (fetch) origin <url BitBucket> (push) github <url GitHub> (fetch) github <url GitHub> (push)

  3. Fetch the GitHub commits. I did this by:

    git fetch github

    Now your local repository has all the commits from the GitHub repository.

  4. Push the commits in the local repository to the GitHub remote repository

    git push github master

    Now the GitHub remote has it's original commits (from when you fetched them in step 3) and the BitBucket commits (which were present in local repository to begin with).

  5. Remove the BitBucket remote url from the repository...

    git remote remove origin

    ...and rename the GitHub remote url to origin :

    git remote rename github origin

Now, all is well with the world.

Things to do Differently:

  1. Use git fetch --all

    This will fetch new commits from every remote url that you have saved. This would replace the first assumption I used from above (that the BitBucket repo had all it's latest commits).

  2. You have multiple branches to transfer.

    From my understanding, the git fetch command will pull down commits from all branches in step 3.

    To push the results back up, from the documentation, you can do git push github --all to push changes made to every branch.

  3. You have conflicting commits

    You'll probably have to do a git merge before pushing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM