Skip to content
Fix Code Error

Make the current Git branch a master branch

March 13, 2021 by Code Error
Posted By: Anonymous

I have a repository in Git. I made a branch, then did some changes both to the master and to the branch.

Then, tens of commits later, I realized the branch is in much better state than the master, so I want the branch to “become” the master and disregard the changes on master.

I cannot merge it, because I don’t want to keep the changes on master. What should I do?

Extra: In this case, the ‘old’ master has already been push-ed to another repository such as GitHub. How does this change things?

Solution

The problem with the other two answers is that the new master doesn’t have the old master as an ancestor, so when you push it, everyone else will get messed up. This is what you want to do:

git checkout better_branch
git merge --strategy=ours master    # keep the content of this branch, but record a merge
git checkout master
git merge better_branch             # fast-forward master up to the merge

If you want your history to be a little clearer, I’d recommend adding some information to the merge commit message to make it clear what you’ve done. Change the second line to:

git merge --strategy=ours --no-commit master
git commit          # add information to the template merge message
Answered By: Anonymous

Related Articles

  • Why do I have to "git push --set-upstream origin "?
  • Git Using Remote Branch
  • Checkout another branch when there are uncommitted…
  • Git merge with force overwrite
  • Git workflow and rebase vs merge questions
  • Why do git fetch origin and git fetch : behave differently?
  • Why does git perform fast-forward merges by default?
  • Why call git branch --unset-upstream to fixup?
  • In plain English, what does "git reset" do?
  • Git - Pushing code to two remotes
  • git pull while not in a git directory
  • What exactly does the "u" do? "git push -u origin…
  • How to recover stashed uncommitted changes
  • Various ways to remove local Git changes
  • What's the difference between HEAD^ and HEAD~ in Git?
  • git lfs push to github failure on Ubuntu 18.04
  • Setting up and using Meld as your git difftool and mergetool
  • How to pull in changes from skeleton sub-repository…
  • Recalculate merge conflicts (ie. how to generate…
  • error LNK2005: ✘✘✘ already defined in…
  • How to track untracked content?
  • How do I make a Git commit in the past?
  • Difference between git checkout --track…
  • Retrieve specific commit from a remote Git repository
  • Why does Git say my master branch is "already up to…
  • Push git commits & tags simultaneously
  • How do I 'overwrite', rather than 'merge', a branch…
  • How to cherry pick a range of commits and merge into…
  • “tag already exists in the remote" error after…
  • How can I find the location of origin/master in git,…
  • Is it safe to shallow clone with --depth 1, create…
  • Why does my operation of mod (%) on a 2 digit number…
  • What is "git remote add ..." and "git push origin master"?
  • Git push won't do anything (everything up-to-date)
  • Your configuration specifies to merge with the from…
  • Git merge master into feature branch
  • How to clone ('fork') your own personal GitHub repo…
  • Git submodule update
  • Move the most recent commit(s) to a new branch with Git
  • Practical uses of git reset --soft?
  • What is a tracking branch?
  • Does "git fetch --tags" include "git fetch"?
  • Why did my Git repo enter a detached HEAD state?
  • How do I delete a Git branch locally and remotely?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How to modify existing, unpushed commit messages?
  • How can I reconcile detached HEAD with master/origin?
  • How to create the branch from specific commit in…
  • Pandas Merging 101
  • How do I revert a Git repository to a previous commit?
  • Git Cherry-pick vs Merge Workflow
  • In git, what is the difference between merge…
  • Git pull a certain branch from GitHub
  • How do I clone a single branch in Git?
  • Git Pull Request no changes but git diff show changes
  • Merge, update, and pull Git branches without using checkouts
  • Why does git say "Pull is not possible because you…
  • Why is Git better than Subversion?
  • What is git tag, How to create tags & How to…
  • git switch branch without discarding local changes
  • Homebrew install specific version of formula?
  • Git: Create a branch from unstaged/uncommitted…
  • How do you get git to always pull from a specific branch?
  • Git "error: The branch 'x' is not fully merged"
  • moving committed (but not pushed) changes to a new…
  • Retrieve the commit log for a specific line in a file?
  • When would you use the different git merge strategies?
  • Why do I need to do `--set-upstream` all the time?
  • How to fix Git error: object file is empty?
  • How to retrieve a single file from a specific…
  • How to keep a git branch in sync with master
  • What's the difference between "git reset" and "git…
  • Ukkonen's suffix tree algorithm in plain English
  • git pull from master into the development branch
  • Push on GIT and Bitbucket
  • Why does C++ code for testing the Collatz conjecture…
  • Git merge errors
  • How can I switch to another branch in git?
  • What are the differences between git branch, fork,…
  • Force "git push" to overwrite remote files
  • Git push: "fatal 'origin' does not appear to be a…
  • Squash the first two commits in Git?
  • Check if pull needed in Git
  • Windows git "warning: LF will be replaced by CRLF",…
  • How do I add files and folders into GitHub repos?
  • Change a Git remote HEAD to point to something…
  • Detach (move) subdirectory into separate Git repository
  • Git's famous "ERROR: Permission to .git denied to user"
  • How to revert multiple git commits?
  • How does PHP 'foreach' actually work?
  • How to merge remote master to local branch
  • How can I merge two commits into one if I already…
  • failed to push some refs to [email protected]
  • Git push error: "origin does not appear to be a git…
  • master branch and 'origin/master' have diverged, how…
  • Git merge branch differences
  • data.table vs dplyr: can one do something well the…
  • Git push rejected after feature branch rebase
  • How to backup a local Git repository?
  • Remove sensitive files and their commits from Git history

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Ubuntu says “bash: ./program Permission denied”

Next Post:

How do I move to end of line in Vim?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error