merge - How can I replace the 'master' branch in Git, entirely, from another branch? - Stack Overflow
git - How I can overwrite a branch with another one? - Stack Overflow
merge - How to replace master branch in Git, entirely, from another branch? - Stack Overflow
How to overwrite remote with another remote branch?
How do I overwrite a single directory from another branch without merging the whole branch?
Use git checkout -- . For example, git checkout dev -- scripts/ copies only the scripts/ directory from dev into your current branch, without merging any other files. Commit the result to record the change.
What is the difference between git checkout and git restore for restoring files?
git checkout moves HEAD, switches branches, and can overwrite paths from another branch. git restore is the newer, narrower command dedicated to restoring file contents from the index or a commit, giving more granular control without touching HEAD.
Does git checkout overwrite uncommitted changes in the working directory?
Yes. git checkout -- replaces the files at that path with the source-branch versions, discarding any uncommitted local edits there. Commit or stash your work first if you need to keep it.
You can use the 'ours' merge strategy:
$ git checkout staging
$ git merge -s ours email # Merge branches, but use our (=staging) branch head
$ git checkout email
$ git merge staging
The question requires the commits of the email branch to be kept. They must not be made unreachable, which rules out any reset-based approaches or deleting and recreating the branch.
I no longer need the old changes in email branch, yet I don't want to delete them.
To keep commits of both branches reachable, one or more references must exist that point to those commits. You can always keep two references (staging and email), but the question asks to get rid of one of the refs. Therefore, the branches need to be merged somehow.
EDIT 2020-07-30:
I thought a bit more about this question and possible solutions. If you absolutely require the merge parents in the correct order, need to perform this action with a single command line invocation, and don't mind running plumbing commands, you can do the following:
$ git checkout A
$ git merge --ff-only $(git commit-tree -m "Throw away branch 'A'" -p A -p B B^{tree})
This basically acts like the (non-existent) merge -s theirs strategy.
You can find the resulting history in the plumbing branch of the demo repository
Not very readable and not as easy to remember compared to the -s ours switch, but it does the job. The resulting tree is again the same as branch B:
$ git rev-parse A^{tree} B^{tree} HEAD^{tree}
3859ea064e85b2291d189e798bfa1bff87f51f3e
0389f8f2a3e560b639d82597a7bc5489a4c96d44
0389f8f2a3e560b639d82597a7bc5489a4c96d44
EDIT 2020-07-29:
There seems to be a lot of confusion as to what the difference between -s ours and -X ours (the latter being equivalent to -s recursive --strategy-option ours) is. Here's a small example to show the two results from using these two methods. I also recommend reading the question and answers of (Git Merging) When to use 'ours' strategy, 'ours' option and 'theirs' option?
First, setup a repository with 2 branches and 3 commits (1 base commit, and 1 commit per branch). You can find the sample repository on GitHub
$ git init
$ echo 'original' | tee file1 file2 file3
$ git commit -m 'initial commit'
$ git branch A
$ git branch B
$ git checkout A
$ echo 'A' > file1
$ git commit -m 'change on branch A' file1
$ git checkout B
$ echo 'B' > file2
$ git commit -m 'change on branch B' file2
Now, let's try the strategy option (doesn't really matter if we use theirs or ours for this explanation):
$ git merge -X ours A
$ cat file*
A
B
original
We end up with a merge of both branches' contents (branch "strategy-option" in the sample repo). Compare that to using the merge strategy (re-init your repository or reset branch, before executing the next steps):
$ git merge -s ours A
$ cat file*
original
B
original
The result is quite different (branch "merge-strategy" in the sample repo). With the strategy option, we get a merge result of both branches, with the strategy we throw away any changes which happened in the other branch.
You will also notice that the commit created by the merge-strategy in fact points to the exact same tree as the latest commit of "our" branch, while the strategy-option created a new, previously-unseen tree:
$ git rev-parse A^{tree} B^{tree} merge-strategy^{tree} strategy-option^{tree}
3859ea064e85b2291d189e798bfa1bff87f51f3e
0389f8f2a3e560b639d82597a7bc5489a4c96d44
0389f8f2a3e560b639d82597a7bc5489a4c96d44
5b09d34a37a183723b409d25268c8cb4d073206e
OP indeed asked for "I no longer need the old changes in […] branch" and "So I just want to dump all the contents of [A] into [B]", which is not possible to do with a strategy option. Using the 'ours' merge strategy is one possibility of many, but likely the easiest (other possibilities include using low level commands of Git such as write-tree and commit-tree).
I've seen several answers and that's the only procedure that let me fix that without any conflicts.
If you want all changes from branch_new in branch_old, then:
git checkout branch_new
git merge -s ours branch_old
git checkout branch_old
git merge branch_new
once applied those four commands you can push the branch_old without any problem
You should be able to use the “ours” merge strategy to overwrite master with seotweaks like this:
git checkout master
git pull
git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks
The first two steps are a useful precaution to ensure your local copy of master is up-to-date. The result should be that your master is now essentially seotweaks.
(-s ours is short for --strategy=ours)
From the docs about the 'ours' strategy:
This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.
Update from comments: If you get fatal: refusing to merge unrelated histories, then change the fourth line to this: git merge --allow-unrelated-histories -s ours master
Use git branch -m to rename the master branch to another one, and then rename seotweaks branch to master. Something like this:
git branch -m master old-master
git branch -m seotweaks master
git push -f origin master
This might remove commits in origin master. Please check your origin master before running git push -f origin master.
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
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.
You should be able to use the “ours” merge strategy to overwrite master with seotweaks like this:
git checkout master
git pull
git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks
The first two steps are a useful precaution to ensure your local copy of master is up-to-date. The result should be that your master is now essentially seotweaks.
(-s ours is short for --strategy=ours)
From the docs about the 'ours' strategy:
This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.
Update from comments: If you get fatal: refusing to merge unrelated histories, then change the fourth line to this: git merge --allow-unrelated-histories -s ours master
What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master? Something like this:
git branch -m master old-master
git branch -m seotweaks master
git push -f origin master
This might remove commits in origin master, please check your origin master before running git push -f origin master.
I've pushed committed changes onto my remote branch but realized the stuff I'm pushing isn't the same as my co-worker's remote branch. So I decided I just want to overwrite my branch to be the same as my co-worker's branch without deleting my branch... Is that possible?