Skip to content

Instantly share code, notes, and snippets.

@dhruvilp
Last active April 18, 2024 07:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dhruvilp/94aa3f505b5c5fd15fc3dab91fc46eb7 to your computer and use it in GitHub Desktop.
Save dhruvilp/94aa3f505b5c5fd15fc3dab91fc46eb7 to your computer and use it in GitHub Desktop.
Copy commits from one git repo to another

Recommended Approach for BitBucket Repo

Short Approach

# remove the remote to the original repo
git remote remove origin

# add a remote to the new repo
git remote add origin <my_repo_url>

# push to master
git push origin master

Long Approach

# add the old repo as a remote repository 
git remote add oldrepo https://github.com/path/to/oldrepo

# get the old repo commits
git remote update

# examine the whole tree
git log --all --oneline --graph --decorate

# copy (cherry-pick) the commits from the old repo into your new local one
git cherry-pick sha-of-commit-one
git cherry-pick sha-of-commit-two
git cherry-pick sha-of-commit-three

# check your local repo is correct
git log

# send your new tree (repo state) to github
git push origin master

# remove the now-unneeded reference to oldrepo
git remote remove oldrepo

Update and rebase your local copy

# Rearrange commnits
git rebase -i --root

# Verify updates
git log

Force push your new repo state & synchronize collaborators

# Force push to the repo
git push -f origin master

# make sure there are no unsaved changes
git status 

# pull the latest version from github
git fetch  

# move their master branch pointer to the one you published to github.
git reset --hard origin/master
@biswajit-k
Copy link

#push to master
git origin push master

I guess this line should be git push origin master

@dhruvilp
Copy link
Author

dhruvilp commented Jun 5, 2023

#push to master
git origin push master

I guess this line should be git push origin master

Yes, you're right. Thanks 👍

@jonesy-b-dev
Copy link

I have a repo from a github classroom that i want to move to my personal account to continue working on, does the short approach just work? or do i also need to do the Update and rebase your local copy and Force push your new repo state & synchronize collaborators?

The repo is only acessable to my school account now, i did commit with my personal account to it. so i dont know if all the commits will be correctly linked to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment