I have a branch called demo
which I need to merge with master
branch. I can get the desired result with following commands:
git pull origin demo
git checkout master
git pull origin master
git merge demo
git push origin master
My only concern is, if there are any merge issues, I want to tell git
to overwrite changes in master
branch without giving me merge prompt. So basically changes in demo
branch should automatically overwrite changes in master
branch.
I looked around there are multiple options but I don't want to take chances with merging.
demo
branch into master
git fetch --all
demo
branch on localgit pull origin demo
master
branch. This branch will be completely changed with the code on demo
branchgit checkout master
master
branch and run this command.git reset --hard origin/demo
reset
means you will be resetting current branch
--hard
is a flag that means it will be reset without raising any merge conflict
origin/demo
will be the branch that will be considered to be the code that will forcefully overwrite current master
branch
The output of the above command will show you your last commit message on origin/demo
or demo
branch
Then, in the end, force push the code on the master
branch to your remote repo.
git push --force
When I tried using -X theirs
and other related command switches I kept getting a merge commit. I probably wasn't understanding it correctly. One easy to understand alternative is just to delete the branch then track it again.
git branch -D <branch-name>
git branch --track <branch-name> origin/<branch-name>
This isn't exactly a "merge", but this is what I was looking for when I came across this question. In my case I wanted to pull changes from a remote branch that were force pushed.
This merge approach will add one commit on top of master
which pastes in whatever is in feature
, without complaining about conflicts or other crap.
git stash
git status # if anything shows up here, move it to your desktop
git checkout master
git pull # if there is a problem in this step, it is outside the scope of this answer
feature
all dressed upgit checkout feature
git merge --strategy=ours master
git checkout master
git merge --no-ff feature
I had a similar issue, where I needed to effectively replace any file that had changes / conflicts with a different branch.
The solution I found was to use git merge -s ours branch
.
Note that the option is -s
and not -X
. -s
denotes the use of ours
as a top level merge strategy, -X
would be applying the ours
option to the recursive
merge strategy, which is not what I (or we) want in this case.
Steps, where oldbranch
is the branch you want to overwrite with newbranch
.
git checkout newbranch
checks out the branch you want to keepgit merge -s ours oldbranch
merges in the old branch, but keeps all of our files.git checkout oldbranch
checks out the branch that you want to overwriteget merge newbranch
merges in the new branch, overwriting the old branchYou can try "ours" option in git merge,
git merge branch -X ours
This option forces conflicting hunks to be auto-resolved cleanly by favoring our version. Changes from the other tree that do not conflict with our side are reflected to the merge result. For a binary file, the entire contents are taken from our side.
Source: Stackoverflow.com