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
🌐
Git
git-scm.com › book › en › v2 › Git-Branching-Basic-Branching-and-Merging
Git - Basic Branching and Merging
After it’s tested, merge the hotfix branch, and push to production. Switch back to your original user story and continue working. First, let’s say you’re working on your project and have a couple of commits already on the master branch. Figure 18. A simple commit history · You’ve decided that you’re going to work on issue #53 in whatever issue-tracking system your company uses. To create a new branch and switch to it at the same time, you can run the git checkout command with the -b switch:
🌐
W3Schools
w3schools.com › git › git_branch_merge.asp
Git Branch 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. ... git merge emergency-fix Updating 09f4acd..dfa79db Fast-forward index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Discussions

How do I safely merge a Git branch into master? - Stack Overflow
A new branch from master is created, we call it test. There are several developers who either commit to master or create other branches and later merge into master. Let's say work on test is taking More on stackoverflow.com
🌐 stackoverflow.com
Git merge branch into master - Stack Overflow
I have a master branch and a working branch_1. I want to 'move' branch_1 exactly as it is to master. So I want something like this: git checkout master git merge branch_1 # I don't know what is co... More on stackoverflow.com
🌐 stackoverflow.com
what is the Best (and safest) way to merge a git branch into master ?
I am not using --rebase because from my understanding, rebase will get the changes from master and stack mine on top of that hence it could overwrite changes other people made. That is not true. Rebase will re-apply the changes you made on top of master. If it touched anything someone else had modified, it would produce a conflict - the same way a merge would produce a conflict in this case. More on reddit.com
🌐 r/git
8
3
July 26, 2018
Is it possible to merge UP into a branch? Say, from 'main'? into the current branch I am working on?
Yes this is possible. I prefer rebasing rather than merging in this instance. More on reddit.com
🌐 r/git
10
4
October 10, 2021
People also ask

What is creating a branch in Git?
Creating a branch takes a snapshot of the existing code so you can work on it independently of the main branch.
🌐
varonis.com
varonis.com › blog › git-branching-and-merging
Git Branching and Merging: A Step-By-Step Guide
How do I create a new branch in Git?
Use the git branch command and specify the branch name, e.g., git branch feature1.
🌐
varonis.com
varonis.com › blog › git-branching-and-merging
Git Branching and Merging: A Step-By-Step Guide
My branch only exists locally. How can I add my branch to my remote Git repository?
Yes, use the git push command to set the upstream branch, e.g., git push –set-upstream origin .
🌐
varonis.com
varonis.com › blog › git-branching-and-merging
Git Branching and Merging: A Step-By-Step Guide
🌐
Atlassian
atlassian.com › git › tutorials › using branches › git merge
Git Merge | Atlassian Git Tutorial
In the event that you require a merge commit during a fast forward merge for record keeping purposes you can execute git merge with the --no-ff option. ... This command merges the specified branch into the current branch, but always generates a merge commit (even if it was a fast-forward merge). This is useful for documenting all merges that occur in your repository. The next example is very similar, but requires a 3-way merge because main progresses while the feature is in-progress.
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
🌐
DataCamp
datacamp.com › tutorial › git-merge
Git Merge Tutorial: A Comprehensive Guide with Examples | DataCamp
March 12, 2025 - If both the base and feature branches ... history of the feature branch and the main branch. In the below example, “E” and “G” are new commits to the main branch after the feature branch was created....
Find elsewhere
🌐
Varonis
varonis.com › blog › git-branching-and-merging
Git Branching and Merging: A Step-By-Step Guide
September 12, 2025 - This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.
🌐
Medium
medium.com › david-fellows › git-branch-and-merge-made-easy-ec6918469837
Git branch and merge. Step by step instructions and reference… | by David Fellows | David Fellows | Medium
October 3, 2019 - Although git status shows there is nothing to commit, the master branch is not update to date with your examplebranch ... Merging your branch into the master will overwrite your master branch with the changes you made in your new branch.
🌐
Luke Merrett
lukemerrett.com › different-merge-types-in-git
Different Merge Types in Git
August 7, 2021 - Here's what we'll use as our sample Git repository. We have 2 branches, the base branch where the merge is going into (e.g: main, or release) and the branch being merged.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-merge-a-git-branch-into-master
How to Merge a Git Branch into Master? - GeeksforGeeks
May 22, 2024 - In this guide, we'll walk you through the process of merging a Git branch into the master, which helps your codebase remain up-to-date and conflict-free.
🌐
Graph AI
graphapp.ai › engineering-glossary › git › merge
Merge: Definition, Examples, and Applications | Graph AI
Let's consider a simple example where a developer has been working on a new feature in a separate branch and wants to merge their changes into the main branch. They would first check out the main branch with the command 'git checkout main'. Then, they would run the command 'git merge feature' ...
🌐
Vercel
vercel.com › vercel documentation › ship and scale › git
Deploying Git Repositories with Vercel
June 16, 2026 - Add a domain of your choice (like staging.example.com) on your Vercel project and assign it to the "staging" Git branch like this. Add Environment Variables that you'd like to use for your new Staging phase on your Vercel project like this. Push to the "staging" Git branch to update your Staging phase and automatically receive the domain and environment variables you've defined. Once you're happy with your changes, you would then merge the respective Preview Branch into your production branch.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-merge-two-branches-in-git
How to Merge Two Branches in Git? - GeeksforGeeks
July 23, 2025 - Recursive merge is performed when the commit histories of the two branches diverge, meaning there are unique commits on both branches. Git analyzes the commit history of both branches and combines the changes, creating a new merge commit that represents the merged state.
🌐
DEV Community
dev.to › mohsenkamrani › git-merge-learn-by-example-1lbf
Git merge - learn by example - DEV Community
December 19, 2022 - $ git checkout master $ git merge --squash new-feature $ git commit -m "Merge new-feature" The output of this command will look similar to the previous example, but if we run git log, we can see that there is only one commit for the merged changes:
🌐
X
x.com › arpit_bhayani › status › 2041507648265482455
Arpit Bhayani on X: "Distributed systems do not have a 'shared clock', so it becomes very difficult for two nodes to agree on what "now" means. To be honest, this is what makes distributed systems so interesting. Also, this is where the "happened-before" relationship comes in handy and brings order" / X
We’ve detected that JavaScript is disabled in this browser. Please enable JavaScript or switch to a supported browser to continue using x.com. You can see a list of supported browsers in our Help Center · By signing up, you agree to the Terms of Service and Privacy Policy, including Cookie Use
🌐
GeeksforGeeks
geeksforgeeks.org › git › merge-strategies-in-git
Merge Strategies in Git - GeeksforGeeks
April 23, 2026 - Use Case: Ideal for merging feature branches with numerous small commits, resulting in a cleaner main branch history. ... git checkout main git merge --squash feature-branch git commit -m "Merged feature-branch with squash"
🌐
CodeChef
codechef.com › learn › course › git-github › LOCALGIT › problems › GITPROB52
Getting Started with Git Merging in Git/Github
Test your Git/Github knowledge with our Getting Started with Git Merging practice problem. Dive into the world of git-github challenges at CodeChef.
🌐
Git
git.github.io › git-reference › branching
Git Reference
You can merge any branch into your current branch with the git merge command. Let's take as a simple example the 'removals' branch from above. If we create a branch and remove files in it and commit our removals to that branch, it is isolated from our main ('master', in this case) branch.
🌐
Reddit
reddit.com › r/git › what is the best (and safest) way to merge a git branch into master ?
r/git on Reddit: what is the Best (and safest) way to merge a git branch into master ?
July 26, 2018 -

A new branch from master
is created, we call it test
.

There are several developers who either commit to master
or create other branches and later merge into master
.

Let's say work on test
is taking several days and you want to continuously keep test
updated with commits inside master
.

I would do git pull origin master
from test
.

Question 1: Is this the right approach? Other developers could have easily worked on same files as I have worked btw.

My work on test
is done and I am ready to merge it back to master
. Here are the two ways I can think of:

A:

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

B:

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

I am not using --rebase
because from my understanding, rebase will get the changes from master
and stack mine on top of that hence it could overwrite changes other people made.

Question 2: Which one of these two methods is right? What is the difference there?

The goal in all of this is to keep my test
branch updated with the things happening in master
and later I could merge them back into master
hoping to keep the timeline as linear as possible.

🌐
Visual Studio Code
code.visualstudio.com › docs › sourcecontrol › overview
Source Control in VS Code
November 3, 2021 - Learn how to use VS Code's integrated Git source control features like staging, committing, branching, merge conflict resolution, and GitHub integration.