git checkout new-mastergit branch -m master old-mastergit branch -m new-master master# And don't do this part. Just don't. But if you want to...# git branch -d --force old-master
git checkout better_branchgit merge --strategy=ours master # keep the content of this branch, but record a mergegit checkout mastergit merge better_branch # fast-forward master up to the merge
# This will force push the current branch to the remote mastergit push -f origin HEAD:master
# Switch current branch to mastergit checkout master
# Reset the local master branch to what's on the remotegit reset --hard origin/master
git checkout master # Set local repository to mastergit reset --hard branch # Force working tree and index to branchgit push origin master # Update remote repository
git checkout <source>git checkout -b temp # temporary branch for mergegit merge -s ours <target> # create merge commit with contents of <source>git checkout <target> # fast forward <target> to merge commitgit merge temp # ...git branch -d temp # throw temporary branch away
这样,合并提交将只存在于target分支的历史记录中。
或者,如果您根本不想创建合并,您可以简单地获取source的内容并将它们用于target的新提交:
git checkout <source> # fill index with contents of <source>git symbolic-ref HEAD <target> # tell git we're committing on <target>git commit -m "Setting contents to <source>" # make an ordinary commit with the contents of <source>
git checkout feature_branchgit merge -s ours --no-commit mastergit commit # Add a message regarding the replacement that you just didgit checkout mastergit merge feature_branch