How I would do this

git checkout master
git pull origin master
git merge test
git push origin master

If I have a local branch from a remote one, I don't feel comfortable with merging other branches than this one with the remote. Also I would not push my changes, until I'm happy with what I want to push and also I wouldn't push things at all, that are only for me and my local repository. In your description it seems, that test is only for you? So no reason to publish it.

git always tries to respect yours and others changes, and so will --rebase. I don't think I can explain it appropriately, so have a look at the Git book - Rebasing or git-ready: Intro into rebasing for a little description. It's a quite cool feature

Answer from KingCrunch on Stack Overflow
Top answer
1 of 16
3750

How I would do this

git checkout master
git pull origin master
git merge test
git push origin master

If I have a local branch from a remote one, I don't feel comfortable with merging other branches than this one with the remote. Also I would not push my changes, until I'm happy with what I want to push and also I wouldn't push things at all, that are only for me and my local repository. In your description it seems, that test is only for you? So no reason to publish it.

git always tries to respect yours and others changes, and so will --rebase. I don't think I can explain it appropriately, so have a look at the Git book - Rebasing or git-ready: Intro into rebasing for a little description. It's a quite cool feature

2 of 16
558

This is a very practical question, but all the answers above are not practical.

Like

git checkout master
git pull origin master
git merge test
git push origin master

This approach has two issues:

  1. It's unsafe, because we don't know if there are any conflicts between test branch and master branch.

  2. It would "squeeze" all test commits into one merge commit on master; that is to say on master branch, we can't see the all change logs of test branch.

So, when we suspect there would some conflicts, we can have following git operations:

git checkout test
git pull 
git checkout master
git pull
git merge --no-ff --no-commit test

Test merge before commit, avoid a fast-forward commit by --no-ff,

If conflict is encountered, we can run git status to check details about the conflicts and try to solve

git status

Once we solve the conflicts, or if there is no conflict, we commit and push them

git commit -m 'merge test branch'
git push

But this way will lose the changes history logged in test branch, and it would make master branch to be hard for other developers to understand the history of the project.

So the best method is we have to use rebase instead of merge (suppose, when in this time, we have solved the branch conflicts).

Following is one simple sample, for advanced operations, please refer to http://git-scm.com/book/en/v2/Git-Branching-Rebasing

git checkout master
git pull
git checkout test
git pull
git rebase -i master
git checkout master
git merge test

Yep, when you have uppers done, all the Test branch's commits will be moved onto the head of Master branch. The major benefit of rebasing is that you get a linear and much cleaner project history.

The only thing you need to avoid is: never use rebase on public branch, like master branch.

Never do operations like the following:

git checkout master
git rebase -i test

Details for https://www.atlassian.com/git/tutorials/merging-vs-rebasing/the-golden-rule-of-rebasing

appendix:

  • if you are not sure about rebasing operations, please refer to: https://git-scm.com/book/en/v2/Git-Branching-Rebasing
Discussions

github - Git - How to merge a remote branch into remote master - Stack Overflow
So here's the situation. I created a local branch. Then I pushed that local branch to github. So github now has two branches. Now I want to merge the two branches together both locally and on g... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
git - How to merge branch to master? - Stack Overflow
I do have a local branch with some changes, and I want to merge it to remote master. When I run: git merge master I get: Already up-to-date but I still can see that the master doesn't contain th... More on stackoverflow.com
🌐 stackoverflow.com
github - Git: Merge Remote Branch into Remote Master? - Stack Overflow
I'm trying to figure out the standard way of using Git to merge a remote branch (origin/develop) into my remote master branch (origin/master). I've taken a look at this: Git - How to merge a remote More on stackoverflow.com
🌐 stackoverflow.com
June 25, 2018
Merge local master branch with remote master branch in Git - Stack Overflow
Is the repo still set up to point at the same remote master? Is there anything that prevents you from simply pulling & merging? ... Save this answer. ... Show activity on this post. If remote/master contains all of the commits that the local master contains, simply do a git pull: More on stackoverflow.com
🌐 stackoverflow.com
🌐
Git
git-scm.com › book › en › v2 › Git-Branching-Basic-Branching-and-Merging
Git - Basic Branching and Merging
All you have to do is check out the branch you wish to merge into and then run the git merge command: $ git checkout master Switched to branch 'master' $ git merge iss53 Merge made by the 'recursive' strategy.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to merge a git branch into master
How to Merge a Git Branch into Master | phoenixNAP KB
December 17, 2025 - The command switches to the master branch. After switching, use the git merge command to merge another branch into master.
🌐
W3docs
w3docs.com › git
Best and Safe Way to Merge a Git Branch into Master
Once you make changes in the local repository and ready to share it with your team members, then execute git push. The git push command uploads the local changes to the remote repository.
🌐
Git Tower
git-tower.com › learn › git faq › how to merge branches in git with git merge
How to Merge Branches in Git with git merge | Learn Version Control with Git
1 week ago - The [behind 1] remark tells us that "master" has received new changes on the remote. We must update "master" before we can integrate our own changes. If properly configured, a plain "git pull" should suffice (after making "master" our active branch): ... The last thing to check before actually starting the merge process is our current HEAD branch: we need to make sure that we've checked out the branch that should receive the changes.
Find elsewhere
🌐
Namehero
namehero.com › blog › how-to-merge-a-git-branch-to-master
How to Merge a Git Branch to Master
October 6, 2025 - It’s always a good idea to first update the branch into which you’re merging because you don’t know what changes have been made since the last time you pulled from the remote repository. You don’t want to accidentally merge the new branch into an outdated version of the main branch. So always ensure that you have the most recent copy by using the following command: ... This will get the most recent copy of the main branch. We’re ready to merge! Type the following command: ... As long as there are no conflicts, you can see that git merges the changes you made in the feature branch into main.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-merge-a-git-branch-into-master
How to Merge a Git Branch into Master? - GeeksforGeeks
May 22, 2024 - Fetch and integrate the latest changes from the remote repository. ... Switch to the branch you want to merge into master. ... Update your feature branch with the latest changes from master to minimize conflicts.
🌐
DedicatedCore
dedicatedcore.com › home › how to safely merge a git branch into master
How to Safely Merge a Git Branch into Master - DedicatedCore Blog
October 31, 2024 - The master branch is selected by the command. Utilize the git merge command to combine a different branch into a master after switching.
🌐
Delft Stack
delftstack.com › home › howto › git › best way to merge a git branch into master
How to Merge a Git Branch Into Master in Git | Delft Stack
February 2, 2024 - In the above diagram, G is a newly created commit and created entirely by git. This commit has two parents! and they have an order: The first parent is D, which was master previously. The second parent is F, which was feature-1 previously. This type of commit is called a merge commit. Now switch back to our repository example and merge the newly created feature-1 branch to master · First, check out the master branch. ... Now, pull the remote master changes to the local master.
🌐
Stack Abuse
stackabuse.com › git-merge-branch-into-master
Git: Merge Branch into Master
April 20, 2023 - If you're looking for a quick answer, to merge a branch into the master branch - you checkout master and merge some_branch: $ git checkout new-branch # ...dev...
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how do you merge a git branch?
How do you merge a Git branch? | Solutions to Git Problems
February 7, 2025 - Merging branches in Git helps combine changes from one branch to another. To merge a Git branch into master, you will first need to checkout the Git branch...
Top answer
1 of 1
3

Do I never need to merge one remote branch into another?

In fact, you can't do that. Well, you can, sort of, but—well, it's confusing. Let's back up. :-)

Git has some rather poor terminology here. The word branch is ambiguous. Once you get used to all of its different meanings, it's not such a bad name: one says "work on branch X" or "checkout out branch Y" and we all know what we meant, but getting to that point is hard. For more about this, see What exactly do we mean by "branch"? But start with this: Git is mostly about commits. Branch names come in later.

Commits and branch names

The most basic way that Git (and you) can find a commit is by its hash ID, which is guaranteed to be unique to that particular commit. But these hash IDs appear to be random: 26e47e261 comes before 8b026edac but 1f1cddd55 comes after either of those, for instance (these are actual commits in the Git repository for Git). Names, like branch names and remote-tracking names, are there to let Git—and you—find the commits. Each such name remembers exactly one hash ID.

Almost every commit also remembers one or two hash IDs. These are the parent commits of that commit. Git can string these various hash IDs, starting with the one stored in a name and working backwards, into chains of commits:

A  <-B  <-C   <--master

The name master remembers the hash ID of commit C, which remembers the hash ID of commit B, which remembers the hash ID of commit A. A is the very first commit (in this tiny repository with only three commits) so it has no parent and thus ends the chain.

The process of adding a new commit consists of writing out the new commit, using the current end-of-branch-chain hash ID as its parent, and then making the name point to the new commit:

A--B--C--D   <-- master

(It's easier not to draw the internal arrows, which always point backwards by definition and—unlike branch names—can never be changed once made, so I generally don't draw them.)

Remote-tracking names

When you work with a Git repository, you generally start by getting all the commits from some other, existing, Git repository. The other Git is using its branch names to find these commits. Your Git collects those commits, but your Git is going to give you your branch names, independent of theirs. So your Git needs some other names—names that are not branch names—in order to find their commits. You clone their repository, and your Git renames their master to your origin/master—a remote-tracking name or remote-tracking branch name, which some people call a remote branch name.1

After the initial clone, which creates origin/*, a git fetch origin will call up the same URL—saved from when you did the clone—to talk to the same Git, collect from it any new commits it may have, and update all your remote-tracking names to match their branch names. So your remote-tracking names automatically follow their branch names. That's their main purpose: to remember which commit their branch names are remembering.


1"Remote branch name" is probably the worst of all these phrases, not that any of them are great, because Git uses the word remote to refer to a name like origin that you can use to identify the other Git repository. So if you were to go to the Git at the URL listed under your remote.origin.url, and ask it about its branches, you'd get their master, their develop, and so on. These are the branch names on the remote: the remote's branch names. Each one must necessarily be a remote branch name. But they're not in your Git, which has instead names like origin/master and origin/develop. I think calling origin/master a remote-tracking name is the best of these ambiguous phrases: it's your name for their (origin's) master.


Creating a branch name from another name

The last step of git clone is generally git checkout master.2 Your Git doesn't have a master yet, so your Git prowls through all the remote-tracking names it has (it just made them)—origin/develop and origin/master and so on—and finds their origin/master. This points to some specific commit, so your Git now creates your master, pointing to the same commit:

A--B--C--D   <-- master (HEAD), origin/master
          \
           E   <-- origin/develop

If you now run git checkout develop, that creates your own develop, pointing to the same commit as origin/develop:

A--B--C--D   <-- master, origin/master
          \
           E   <-- develop (HEAD), origin/develop

Note that HEAD is attached to one of your branch names—that's how Git knows which branch you're on, when you make a new commit.

Let's say you add your own commit to your own master, by doing git checkout master and doing some work. This creates a new, unique-hash-ID commit, and makes your master point to this new commit:

           F   <-- master (HEAD)
          /
A--B--C--D   <-- origin/master
          \
           E   <-- develop, origin/develop

This particular example does not use git merge, which has its own set of complications (I don't want to get into the details here), but the point is simple enough: you always do all your work on your own branches. When you have finished this work, you can then run git push to:

  • send commit(s) to the other Git, as necessary; and then
  • ask their Git to set their branch name based on what you just sent.

Hence you can now git push origin master to send your new commit F to them, and ask them to make their master point to commit F. If they agree to this, your Git records this update so that you now have:

           F   <-- master (HEAD), origin/master
          /
A--B--C--D
          \
           E   <-- develop, origin/develop

2You can tell your Git to use another name, and if you don't, the other Git tells your Git which name to use, but usually that's master.

Top answer
1 of 1
54

Case 1: remote/master has everything that local master has

If remote/master contains all of the commits that the local master contains, simply do a git pull:

git checkout master
git pull remote master

You can check if the local master has commits that remote/master doesn't by using the following:

git fetch remote
git log --oneline --graph remote/master..master

That will show you all commits that are contained in master but not in remote/master. If you don't see any output, that means remote/master has everything that the local master has.

Case 2: local master has commits that remote/master doesn't have

If the local master contains commits that remote/master doesn't contain, you'll have to figure out how you want to handle that. Do you want to keep them and merge them with remote/master, or do you simply want to throw them away?

Case 2a: merge/rebase local master commits into remote/master

If you want to keep them, you can either merge or rebase the local master with remote/master:

git checkout master
git fetch <remote>

# Merge remote/master
git merge remote/master

# Or rebase local commits on top instead
git rebase remote/master

# Push the results
git push remote master

Case 2b: throw away local master commits

If you don't want to keep the local commits, then just do a hard reset of the local master to the same point as the remote/master:

git checkout master
git fetch remote
git reset --hard remote/master

Documentation

You can read more about all of these commands from the Git documentation. I also highly recommend the excellent free online Pro Git book, especially chapters 1-3 and 6-6.5.

🌐
Atlassian
atlassian.com › git › tutorials › using branches › git merge
Git Merge | Atlassian Git Tutorial
Once the previously discussed "preparing to merge" steps have been taken a merge can be initiated by executing git merge <branch name> where <branch name> is the name of the branch that will be merged into the receiving branch.
🌐
Reddit
reddit.com › r/git › super dumb question, how to merge origin/master into my branch in one commit?
r/git on Reddit: Super dumb question, How to merge origin/master into my branch in one commit?
May 20, 2022 -

Not sure if this is the right place to ask this. My branch is behind origin master by many commits, I tried to git merge master to my branch earlier but my git log history now shows a bunch of other people’s commits, but I actually just wanted to keep my branch up-to-date with master in one commit only, something like “Merge branch ‘master’ into ‘some-branch’” any help is appreciated. Thanks

🌐
Togaware
togaware.com › linux › survivor › Git_Merge_Master_into.html
Git Merge Master into Branch
The commits are those that were committed by others to the remote repository's main branch. Notice that we could skip the first two lines and change the merge to merge origin/main to also effect a merge from the remote main into the current branch.
🌐
W3Schools
w3schools.com › git › git_branch_merge.asp
Git Branch Merge
To combine the changes from one branch into another, use git merge. Usually, you first switch to the branch you want to merge into (often main or master), then run the merge command with the branch name you want to combine in.