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
🌐
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. index.html | 1 + 1 file changed, 1 insertion(+) This looks a bit different than the ...
Discussions

Merge Branch to Master
Git will not overwrite anything unless you explicitly tell it to. More on reddit.com
🌐 r/git
6
0
July 11, 2025
Other - git: correct way to merge/pull commits/branches into master retaining history? | The FreeBSD Forums
The master branch should continue ... of 'merged branch XY'. Given that the 'git log' of the master branch already looks like that (all commits with their original author, date, description), I suppose this is the 'standard way' of doing this - but I couldn't find out how. As a bonus, I have at least one branch/commit from another remote repository ... More on forums.freebsd.org
🌐 forums.freebsd.org
September 4, 2024
Super dumb question, How to merge origin/master into my branch in one commit?
There isn't a way to do exactly what you want. If the merge was the other way you would squish the commits before merging, but in this case it would be a bad idea. If you squish the new commits on master and merge the squished commit into your branch, it will be fine until you try to merge your branch back into master. At that point the squished commit will count as a new commit on your branch as it doesn't exist on master which will of course cause conflicts. If you want to keep your branch up to date with master but not have a messy history then I recommend rebasing your branch on top of master. This achieves a similar effect to the merge except instead of creating a merge commit, it basically moves the commits on your branch and places them as if you had originally started the branch from the latest commits to main. If you don't want to rebase (re-writing history can be problematic), then I'm not aware of any other way except for putting up with the messy history. More on reddit.com
🌐 r/git
19
10
May 20, 2022
Is it wrong to merge master into a branch i.e. ideally should you use rebase instead?
Nothing wrong at all with merging master into another branch so long as you know what you are doing and this is what you want. More on reddit.com
🌐 r/git
36
6
February 23, 2021
🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-merge-a-git-branch-into-master
How to Merge a Git Branch into Master? - GeeksforGeeks
May 22, 2024 - Merge the changes from your feature branch into master. ... If Git reports conflicts, you'll need to resolve them manually.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › git merge master into branch
Git Merge Master into Branch {Two Methods Explained}
October 2, 2023 - Although both commands work, git rebase should be used when you are working alone on the project, while git merge is more appropriate for teams. The sections below show different methods for merging the master branch into another branch.
Find elsewhere
🌐
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 - In Git, a repository’s default and principal branch is called the master (or main) branch. It often refers to the most recent stable version of the project’s source code. Several developers can work on various features or fixes simultaneously and then combine those changes into a single branch by merging another branch into a master. Apart from this if you don’t want to affect the main branch and to work on different parts of the project one needs to create a new branch.
🌐
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.
🌐
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 - You then branch off into a feature branch and make additional changes. To merge the feature branch, you will want to first checkout your main branch with ... GitTip: Need help? See the step-by-step process of how to checkout a Git branch locally and how to checkout a remote Git branch. Then use the command ... You will then merge changes from the feature branch over to main, adding all changes from the feature branch to main.
🌐
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.
🌐
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
4 days ago - Merge a branch into your current branch with git merge. Covers fast-forward merges, no-fast-forward merges, and resolving merge conflicts.
🌐
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...
🌐
Reddit
reddit.com › r/git › merge branch to master
r/git on Reddit: Merge Branch to Master
July 11, 2025 -

0

I have very basic question and am very novice to GIT. Hence, my question is below.

I have "Master" as base branch where my code files and folders are. Now, I have 5 teammates, and they have created 5 branches separately to work on different folders cloning the masters. Now, my question is - I created some changes in folder xyz in my branch abc123. Now, I need to push my changes from abc123 branch and merge to Master. If I merge, then master repo will be updated with new changes. Now, my concern is that what if other teammates push their changes in to master working different folder called abc but in their cloned one in their branch there would be previous content of xyz content. will that previous content from his branch replace my updated one in master?

Top answer
1 of 1
144

You can either git merge master or git rebase master.

If the branch has not been distributed to other people, in this case i would prefer git rebase.

Because git rebase makes it as if the changes on the feature branch were made on top of the changes on the master branch, which makes the version graph simpler.

Rebase

Taking the example from the git rebase manual, git rebase master in branch feature:

      A---B---C feature                             A'--B'--C' feature
     /                   --rebase-->               /
D---E---F---G master                  D---E---F---G master

However, git rebase is only suitable when nobody else is working on it, or there will be confusion and extra work for them, because the old commits A, B, C are now replaced by new commits A', B', C', plus F and G that were not there before.

The actual result after git rebase master in branch feature is this:

      ( A---B---C ) feature@{1}
       /
      /       A'--B'--C' feature
     /       /
D---E---F---G master

Commits A, B, C are dangling after the rebase, but are reachable through git reflog feature as feature@{1}.

Merge

If someone has pulled your branch, or you have pushed it somewhere, you should merge into it instead, to avoid confusion and extra work on the other end. See Recovering from upstream rebase.

This is the result of git merge master in branch feature:

      A---B---C feature                    A---B---C---M feature
     /                   --merge-->       /       ,---’
D---E---F---G master                 D---E---F---G master

Alternatively, if you git merge feature in branch master, it would look like this:

      A---B---C feature                    A---B---C feature
     /                   --merge-->       /         \
D---E---F---G master                 D---E---F---G---M master
🌐
JanBask Training
janbasktraining.com › community › devops › how-do-i-safely-merge-a-git-branch-into-master
How do I safely merge a Git branch into master? | JanBask Training Community
September 3, 2025 - Merging a Git branch into the master branch is a common step when you finish working on a feature or bug fix. However, doing it safely is important to avoid conflicts, broken code, or overwriting changes made by others. Here are some best practices to follow for a safe merge: ... Bring the latest changes from master into your branch before merging.
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
Other - git: correct way to merge/pull commits/branches into master retaining history? | The FreeBSD Forums
September 4, 2024 - Then a merge is straight-forward, all you have to do is "move where master points to", and you won't need any merge commit with more than one parent. As for the examples you show, this looks to me more like you pushed some branches to your remote ...
🌐
Barbagroup
barbagroup.github.io › essential_skills_RRC › git › branching
Branch and Merge - Essential skills for reproducible research computing
To do a merge (locally), git checkout the branch you want to merge INTO. Then type git merge <branch> where <branch> is the branch you want to merge FROM. We are on the master branch and want to merge in make_function so we do:
🌐
Graphite
graphite.com › guides › git-merge-main-into-another-branch
Git merge main branch into another branch - Graphite
This command brings in the changes from origin/main to your current branch (feature-branch). Conflicts may arise during the merge if the changes in the main branch are in conflict with the changes in your feature branch. Here’s how to handle them: Identify the files with conflicts: Git will list conflicts during the merge.
🌐
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