Gauthier Buttez
Gauthier Buttez

Reputation: 1193

How to merge a branch to main branch in Github?

I have my code on my local computer. I created a github repository. I wanted to upload my local code to this remote github repository. This is what I did on terminal:

  1. I went to the directory of my code from terminal

Then I type:

>git init
>git add .
>git commit -m "First commit"
>git remote add origin https://github.com/usergithub/repository_name.git

Then I went to my github repository and I saw a new branch:

enter image description here

So I click on the green button to make a pull request:

enter image description here

But there is nothing there I can do to merge these 2 branches?

How can I merge the branch to my main project?

I tried different stuff from the terminal:

git push --force origin main

But I get this error message:

error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/usergithub/repository_name.git'

Upvotes: 17

Views: 83251

Answers (2)

Mike Lin
Mike Lin

Reputation: 44

In this order using Terminal or Command Prompt,

git checkout main switch to main branch

git pull origin main grab latest updates from main branch

git merge master merge master branch to your main branch

git push origin main push your changes to main

For easier route, you can download Github Desktop app, add your repo into the app and do all the fetch, commit, push and even PULL REQUEST to merge the branch to the main brach.

FYI, naming your other branch master is not recommended and it will making it difficult to distinguish your main (your main branch) and master (other branch). Github has changed its naming from master to main instead to avoid unpleasant term.

Upvotes: 1

Monish Khatri
Monish Khatri

Reputation: 980

I hope the master branch contains all of your pushed changes Steps to merge your branch

  1. git checkout main
  2. git merge master
  3. git push origin main

Now master is merged in main branch and main branch contains all the changes of master branch.

Or you can go to Pull Request tab and click on the Pull request you have created then scroll down you'll able to see the Merge Pull Request Button. On clicking that button it will merge your branch into main branch.

Upvotes: 20

Related Questions