I had a git repository hosted on GitHub. I recently switched machines and copied the files manually to the new machine rather than doing git pull
, kind of forgot to do so. Now I made some changes and initiated a new git repository while adding the GitHub one as remote. I need to know how can I merge the two repositories i.e keep my commit history from the GitHub one and also keep my changes made to the new one? Thank you!

- 733
- 1
- 6
- 13
-
Push the changes from the copy back up to the remote, then pull down anywhere you want. You can also transpose any files you've changed from your copy to a checked out version, then commit those changes, but be sure to do this carefully as you'll stomp any edits that happened after you made a copy. – tadman Jun 28 '18 at 22:40
-
1Possible duplicate of [How do I clone into a non-empty directory?](https://stackoverflow.com/questions/2411031/how-do-i-clone-into-a-non-empty-directory) – phd Jun 29 '18 at 01:16
3 Answers
You just need to rebase your new branch with upstream branch. You can do this by setting an upstream branch for your new branch with git branch -u origin/my_branch
(assuming the remote you added is called origin
and you are currently on your new branch) then execute git pull --rebase
to rebase your branch. After that you can git push
your new commits on top of your existing commits on GitHub.

- 5,954
- 1
- 13
- 34
-
Thank you, this is the correct solution if you have a made quite a lot of changes in the new repository, this works perfectly fine then. I had only modified a handful of files, that's why I went with mkasberg's solution. Thanks again! – Humza Khan Jun 29 '18 at 20:33
If there are not too many changes (just a single commit's worth), this is really easy:
- Clone the (original) repo from Github into a new folder.
git clone ssh://... newfolder
- Copy and paste all your files from the folder where you made your changes, overwriting files in
newfolder
- Make a commit in
newfolder
and push. At this point, your Github history is correct. Delete the old folder, name the new one whatever you'd like.

- 16,022
- 3
- 42
- 46
-
Thank you, I modified 4-5 files and can't believe I am this dumb, this didn't cross my mind! – Humza Khan Jun 29 '18 at 20:33
Since you have already set remote url you can either create a new branch to store the changes and push it.Later you can merge the branches when necessary. Or Since you have created local repository and set remot-url you can directly push to your repository after commiting. Hope this helps.

- 11
- 4