Skip to content

Instantly share code, notes, and snippets.

@ummahusla
Forked from brev/git-overwrite-branch.sh
Last active September 5, 2024 13:48
Show Gist options
  • Save ummahusla/8ccfdae6fbbe50171d77 to your computer and use it in GitHub Desktop.
Save ummahusla/8ccfdae6fbbe50171d77 to your computer and use it in GitHub Desktop.
Git overwrite branch with another branch
# overwrite master with contents of feature branch (feature > master)
git checkout feature # source name
git merge -s ours master # target name
git checkout master # target name
git merge feature # source name
@tatimblin
Copy link

If refusing to merge unrelated histories
You may need to use --allow-unrelated-histories if the histories don't line up

@kisplab
Copy link

kisplab commented Dec 31, 2019

Would anyone care to explain what the 3rd line does? Please.

@zheyuanWang
Copy link

zheyuanWang commented Jan 13, 2020

Would anyone care to explain what the 3rd line does? Please.

see this for "ours" merge strategy

@tokyopdm
Copy link

tokyopdm commented Sep 6, 2020

Thanks, I'm a beginner and I was having trouble with this!

@bogdoslavik
Copy link

Thank you!

@chrisjarvis1
Copy link

This is amazing!
Thank you.

@msakar
Copy link

msakar commented Mar 4, 2021

Is it required to do git push after 5th step “git merge feature” ? Also should we expect git status to show any files which are different and which will be overwritten ?

@gate3
Copy link

gate3 commented Jan 21, 2022

Awesome, worked like a charm. One thing i will add though is to use git push -f in case you can't push

@poojaleo
Copy link

This works. Thank you!

@sanieakhtar
Copy link

Awesome, worked like a charm. One thing i will add though is to use git push -f in case you can't push

Thank you!! I needed this one. Would've had a breakdown otherwise.

@mikkebjo
Copy link

mikkebjo commented Feb 3, 2023

Thank you, this was perfect :)

@nicolaswendling
Copy link

Approved, that works! Thanks

@socketbox
Copy link

That still leaves non-conflicting stuff in master. Say i deleted shit in seotweaks but master still has them, the merge will let master keep it because it doesn't conflict, it's just master "adding" things.

git checkout -B master anotherbranch
See https://stackoverflow.com/a/29871400/148680

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment