i had to delete the tracking branch as well

git branch -d -r origin/develop
Answer from webstrap on Stack Overflow
🌐
TecHighness
techighness.com › home › shell script to delete and recreate a branch from master
Shell Script to Delete and Recreate a Branch From Master - TecHighness
July 9, 2021 - read -p $'\e[31mEnter "yes" to delete development branch & recreate it from master:\e[0m ' CONT if [ "$CONT" = "yes" ]; then git checkout master git branch -d development #delete locally git push origin --delete development #delete remotely ...
Discussions

git - how to reset develop branch to master - Stack Overflow
You should be doing what's in the ... do: git reset --hard master Otherwise you get an error message 2021-04-04T17:35:10.903Z+00:00 ... Save this answer. Show activity on this post. If all else fails, you can delete your current branch and just directly recreate it from ... More on stackoverflow.com
🌐 stackoverflow.com
recreate my master branch in git - Stack Overflow
My master branch is now not up to date and when I pull from the release branch I get too many conflicts how can I track the release branch and replacing the old code with the new one, I've tried the More on stackoverflow.com
🌐 stackoverflow.com
merge - How to replace master branch in Git, entirely, from another branch? - Stack Overflow
I have two branches in my Git repository: master seotweaks (created originally from master) I created seotweaks with the intention of quickly merging it back into master. However, that was three mo... More on stackoverflow.com
🌐 stackoverflow.com
Can I destroy and recreate a Git remote branch in one command? - Stack Overflow
In Git, I sometimes work on long-running branches. I like to rebase on master from time to time to make merging easier when I'm ready. After rebasing, I can't push a previously-pushed branch to a ... More on stackoverflow.com
🌐 stackoverflow.com
April 26, 2020
🌐
GitHub
github.com › affinitybridge › git-bpf › blob › master › lib › git_bpf › commands › recreate-branch.rb
git-bpf/lib/git_bpf/commands/recreate-branch.rb at master · affinitybridge/git-bpf
include GitHelpersMixin · · @@prefix = "BRANCH-PER-FEATURE-PREFIX" · @documentation = "Recreates the source branch in place or as a new branch by re-merging all of the merge commits." · · def options(opts) opts.base = 'master' opts.exclude = [] · [ ['-a', '--base NAME', "A reference to the commit from which the source branch is based, defaults to #{opts.base}.", lambda { |n| opts.base = n }], ['-b', '--branch NAME', "Instead of deleting the source branch and replacng it with a new branch of the same name, leave the source branch and create a new branch called NAME.", lambda { |n| opts.branch = n }], ['-x', '--exclude NAME', "Specify a list of branches to be excluded.", lambda { |n| opts.exclude.push(n) }], ['-l', '--list', "Process source branch for merge commits and list them.
Author   affinitybridge
🌐
MEDIAPORTAL
team-mediaportal.com › wiki › display › MediaPortal1 › Recreate+a+Branch
Recreate a Branch - MediaPortal
March 12, 2016 - ... git log master --grep=log4net it shows up with this merge commit: *commit 7706a04def9fb08ca4b88b103a13534c20026f74 *Merge: 2b89ef5 7a10a1c *Author: sebastiii <sebastiii@team-mediaportal.com> Date: Tue Jan 1 12:43:38 2013** Merge remote-tracking branch 'remotes/origin/FEAT-3273-Switch_t...
🌐
GitHub
gist.github.com › fetis › f0fa9ef3ebc972c3809f21ab9a4fda36
How to restore `master` branch · GitHub
Save fetis/f0fa9ef3ebc972c3809f21ab9a4fda36 to your computer and use it in GitHub Desktop. ... Once you've created your new repo you might be interested to bring master branch back to be aligned with your other projects
Top answer
1 of 2
99

If you want all changes from master in dev_branch, then:

git switch dev_branch
git reset --hard master

If you have dev_branch pushed to a remote already, you have to do:

git push --force

To force-push the branch to the remote. Warning: This will break the history of the branch for people who cloned it before! Then, other people will have to do a git pull --rebase on the dev_branch to get the changes.


You can also rename the dev branch to something old and then make a new branch from master with the same name:

git branch -m dev_branch old_dev_branch
git branch -m master dev_branch

Or, use the ours strategy — not sure why it wouldn't work for you:

git checkout master
git merge -s ours dev_branch
git checkout dev_branch
git merge master
2 of 2
1

Why do that? You can delete the branch if it isn't needed anymore (But why? Branches cost next to nothing.). Or you can rename it:

git branch -m dev_branch obsolete_dev

Or you could do this to delete it:

git branch -D dev_branch

Now create a new branch off master (assuming you are on it):

git branch dev_branch

See git branch --help for further options (setting up remotes and all that jazz).

If you now have new branches, you'll have to synchronize with any peer repositories.

Best way to avoid hassle: Have an "active" development branch, if it goes stale, abandon it and create a new one. No history lost that way (could prove crucial sometime).

Have e.g. a branch for each major version, develop on branches off those to fix version bugs, master forges ahead. Use cherry-pick and perhaps merges to port fixes to older versions.

🌐
Stack Overflow
stackoverflow.com › questions › 12199213 › recreate-my-master-branch-in-git
recreate my master branch in git - Stack Overflow
My master branch is now not up to date and when I pull from the release branch I get too many conflicts how can I track the release branch and replacing the old code with the new one, I've tried the following : git branch --track master ... this doesn't work because the master branch is already created.
Find elsewhere
🌐
Skofgar
skofgar.ch › dev › 2020 › 08 › git-how-to-replace-the-master-branch
Git: How to replace the master branch – Skofgar's Blog
August 31, 2020 - Replace the main “master” branch with the “fixed “branch using: git push origin +fixed:master Depending on what your remote repository is named you might need to replace origin.
🌐
GitHub
gist.github.com › jtpaasch › 9592f201366a32d3e732
Recreate a git branch clean · GitHub
May 30, 2014 - Recreate a git branch clean · Raw · clean_branch.sh · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.
🌐
Nick Ang
nickang.com › 2017-09-30-replace-git-branch-code
How to completely replace git branch code with another branch's code | Nick Ang
September 30, 2017 - The post explains how a team discovered that their staging and master branches had different commit counts because GitHub merge commits added extra history to master, even though the code was effectively in sync. It then describes replacing one branch's code and history with another by force-pushing the source branch into the target branch, using commands like fetch, reset, renaming branches, and pushing to update remote history.
Top answer
1 of 3
8

Well, first, branches don't really get deleted. Only branch names get deleted. But this gets us into a thorny question: What exactly do we mean by "branch"?

Let's take a fast look at the process of merging. We start with a series of commits in the commit graph (or "DAG"; see the linked question) that look like this:

...--o--*-----o   <-- master
         \
          o--o--o--o--o   <-- feature

We then run:

git checkout master
git merge feature

which somehow figures out what we've changed in both master and feature since the last time we merged them (which was actually never, but they were together at one time, at the point marked * here). Git then makes a new merge commit that points back to both of these branch tip commits:

...--o--*-----o---------o   <-- master
         \             /
          o--o--o--o--o   <-- feature

and we have "a merge": a commit, of type merge commit.

We can now erase the word feature and the arrow, i.e., remove the name. The graph remains intact, retained by the name master:

...--o--*-----o---------M   <-- master
         \             /
          o--o--o--o--F

Should we wish to see what went into master via feature, all we have to do is find the commit I have labeled F (for Feature) here. Note that I have also labeled the merge commit M (for merge).

The way to find it is to start from master and work backwards until we find M. (It's right there at the tip of master right now, although later, it will be some number of steps back from the tip.) Then, we simply look at the second parent commit of M.

To find the second parent of a commit whose hash ID we know, we just tell Git: tell me the hash ID of the second parent of this other hash ID. The easy way to do this is with git rev-parse. Let's say the hash ID of M is badf00d:

git rev-parse badf00d^2

Git spits out the full hash ID of F. The hat-two suffix means "second parent" (hat-one, or just hat by itself, means "first parent").

Now we may also want to find commit *. That's the merge base of the commit that is the first parent of M, and this particular commit F that we just found. To find the merge base of two commits, we ask Git:

git merge-base badf00d^1 badf00d^2

We can then look at every commit in the range starting just after the merge base * and going up through and including commit F, using git log or git format-patch or whatever.

We can do this with the raw hashes, or we can point names (temporary or permanent, they will live exactly as long as you like) to commits M, F, and/or *, using git branch or git tag. Each name remembers the hash ID for you. The chief difference between a tag name and a branch name is that if you git checkout a tag name, you get a "detached HEAD" and are not on a branch, but if you git checkout a branch name, you get on that branch, and if you make new commits, they will cause that branch to advance:

$ git branch newname <hash-ID-of-commit-F>

...--o--*-----o---------M--o--o--o   <-- master
         \             /
          o--o--o--o--F   <-- newname

$ git checkout newname
... hack away ...
$ git commit ...

...--o--*-----o---------M--o--o--o   <-- master
         \             /
          o--o--o--o--F--o   <-- newname (HEAD)

This is all that branches are, in Git: the names just point to commits, while the branch structure, the history or DAGlet or whatever name you want, is formed by the permanent parts of the commit DAG. Branch names have the special feature that you can git checkout them and make them advance by running git commit.

2 of 3
2

Let's assume the following simplified history: The feature branch was based on commit N and contained the commits P and Q. In the meantime, commit O was added to the master branch. The feature branch was merged in commit R. After that, commits S and T were created, which is where master is now.

M -- N -- O -- R -- S -- T [master]
      \       /
       P --- Q

You want to find commit Q. I don't think there is a programmatic way to do this, so you need to use any repository browser, for example gitk.

Once you have found Q, you can easily recreate the feature branch:

$ git branch my_feature_branch <hash of commit Q>

I'm not sure if you are aware of this, but a key to understanding this is knowing that a branch is not much more than a "pointer" or "bookmark" to a commit.

🌐
Reddit
reddit.com › r/git › how do i overwrite one branch with another branch?
r/git on Reddit: How do I overwrite one branch with another branch?
May 20, 2019 - You could try to use git pull --rebase to apply your changes on top of the new master. ... Why not just do a fresh clone if you're going to make the changes manually anyway? Or are you going to cherry pick them?
🌐
Atlassian
confluence.atlassian.com › bbkb › how-to-restore-a-deleted-branch-765757540.html
How to restore a deleted branch | Bitbucket Cloud | Atlassian Support
April 8, 2025 - kb-example-repo$ git branch -r origin/HEAD -> origin/master origin/bugfix/CS-1000 origin/feature/one origin/master kb-example-repo$ git rev-parse origin/bugfix/CS-1000 773677e7173488c64410af59e0c3287a24c54326 kb-example-repo$ ... The git reflog may have the branch and the last reference.
🌐
GitHub
gist.github.com › bultas › 3fcd11d8e5e31e93562d
GIT - how to replace master branch with another branch · GitHub
GIT - how to replace master branch with another branch - gist:3fcd11d8e5e31e93562d