DEV Community

B. Burt
B. Burt

Posted on

Merge yer Own!

When Git and GitHub don't wanna play nice.


Recently, I struggled trying to merge my remote repo on GitHub with a pre-existing, local repo. I'm gonna try and recreate the scenario here.

Hopefully this article will save someone the grief that I went through.

And also, I'm hoping for people with more Git experience than I, to comment down below with some solid advice for me and others. The beauty of learning in public!

So the other day I made this silly click-counter/color-changer site (link below) and I'm going to merge it with my GitHub repo that I just created. Here's a screenshot of it. Nothing fancy, just for fun and practice.

my color-changer/button-click-counter

I know this is beginner-level but hey, what can I say, I'm a beginner :)

So on my local branch, I don't have a README.md yet. But on GitHub, I checked the "Add a README file" checkbox when I made the repository.

Creating a repository on GitHub and checking the "Add a README file" checkbox

After creating this repository on GitHub, I made a change to README.md and then commited that change there on GitHub. So now I have divergent branches or unrelated histories or something like that.

Now, some of you are probably thinking that GitHub gives you instructions on connecting your local repo with GitHub immediately after you create your GitHub repo. That page looks like this:

Some GitHub command line instructions

Correct me if I'm wrong, but it seems that that page gets skipped if you check the "Add a README file" checkbox. Try it for yourself.

But remember, this page, the picture up there, you see this when you DON'T add a README. I added a README and so this page got skipped for me. The photo is from a repo I deleted and then I made a new one so I could try a few different things.

But so, back to my terminal I did:

git init

and all that, did my commit, I add my remote repository that I just created with:

git remote add origin git@github.com:beeburrt/color-changer.git

To check it, run:

git remote -v

I think this is the part that tripped me up last time, when things didn't go so smoothly for me and I spent way too long trying to figure it out. I'm going through my bash history now, trying to piece it together haha.

Well, so anyways, I'm just gonna show a few random things first. If I do like it says in that picture above, at the bottom of it, it says to do:

git branch -M main

if you run

git branch --help

and scroll down a bit, you'll see that the -M flag is a shortcut for --move --force.

Above that it says this:

 With a -m or -M option, <oldbranch> will be renamed to <newbranch>. If
   <oldbranch> had a corresponding reflog, it is renamed to match
   <newbranch>, and a reflog entry is created to remember the branch
   renaming. If <newbranch> exists, -M must be used to force the rename to
   happen.

I'm not sure what reflog is, but it sounds kinda gross. Like it automatically comes with dog-slobber on it. "Would you like some reflog on your turkey young man?" I'll have to pass on the reflog, thanks though. Yummy reflog!

After that at the bottom of that picture it says to do:

git push -u origin main

if I do

git push --help

it says the -u flag is for --set-upstream let's run it!

Here's a shot of the output of that last command:

git push -u origin main from my command-line

The message mentions git pull and fast-forward. Let's run it!

git pull --ff-only origin main

Here's the output:

command-line output after git pull --ff-only origin main

This output says fatal, uh oh, are you ok?

It says refusing to merge unrelated histories. So let's tell git that we don't care about unrelated histories.

`git pull origin main --allow-unrelated-histories

Success! Here's the output:

command-line output after git pull origin main --allow-unrelated-histories

And for the win, we do:


git push -u origin main

Enter fullscreen mode Exit fullscreen mode

remember the -u is for --set-upstream.

You can see the silly site here

This post helped.

Here's a couple good Git and GitHub free books.
This one from Launch School, thanks Launch School!

And this one from the Learn Enough series Learn Enough Git to Be Dangerous

And of course, the ultimate, granddaddy of all free git books is Pro Git by Scott Chacon and Ben Straub. I need to spend more time with my nose in this book!

That's all for now! I hope you liked it. Leave a comment and tell me how I could've done this easier.

Buy me a coffee so I can spend more time learning how to make better stuff than a color-changer/click-counter site haha just kidding, not kidding :)

Top comments (0)