将当前 Git 分支设为 master 分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2763006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make the current Git branch a master branch
提问by Karel Bílek
I have a repository in Git. I made a branch, then did some changes both to the master and to the branch.
我在 Git 中有一个存储库。我创建了一个分支,然后对 master 和分支都做了一些更改。
Then, tens of commits later, I realized the branch is in much better state than the master, so I want the branch to "become" the master and disregard the changes on master.
然后,几十次提交后,我意识到分支的状态比 master 好得多,所以我希望分支“成为” master 并忽略 master 上的更改。
I cannot merge it, because I don't want to keep the changes on master. What should I do?
我无法合并它,因为我不想将更改保留在 master 上。我该怎么办?
Extra: In this case, the 'old' master has already been push
-ed to another repository such as GitHub. How does this change things?
额外:在这种情况下,'旧' 主已经被push
-ed 到另一个存储库,如 GitHub。这如何改变事情?
回答by Cascabel
The problem with the other two answers is that the new master doesn't have the old master as an ancestor, so when you push it, everyone else will get messed up. This is what you want to do:
其他两个答案的问题是,新主人没有老主人作为祖先,所以当你推它时,其他人都会被搞砸。这是你想要做的:
git checkout better_branch
git merge --strategy=ours master # keep the content of this branch, but record a merge
git checkout master
git merge better_branch # fast-forward master up to the merge
If you want your history to be a little clearer, I'd recommend adding some information to the merge commit message to make it clear what you've done. Change the second line to:
如果您希望您的历史记录更清晰一些,我建议您在合并提交消息中添加一些信息,以明确您所做的事情。将第二行更改为:
git merge --strategy=ours --no-commit master
git commit # add information to the template merge message
回答by Brandon Howard
Make sure everything is pushed up to your remote repository (GitHub):
确保所有内容都推送到您的远程存储库 (GitHub):
git checkout master
Overwrite "master" with "better_branch":
用“better_branch”覆盖“master”:
git reset --hard better_branch
Force the push to your remote repository:
强制推送到您的远程存储库:
git push -f origin master
回答by Dietrich Epp
Edit:You didn't say you had pushed to a public repo! That makes a world of difference.
编辑:您没有说您已推送到公共回购!这让世界大不相同。
There are two ways, the "dirty" way and the "clean" way. Suppose your branch is named new-master
. This is the clean way:
有两种方式,“脏”方式和“清洁”方式。假设您的分支名为new-master
。这是干净的方式:
git checkout new-master
git branch -m master old-master
git 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
This will make the config files change to match the renamed branches.
这将使配置文件更改以匹配重命名的分支。
You can also do it the dirty way, which won't update the config files. This is kind of what goes on under the hood of the above...
你也可以用肮脏的方式来做,这不会更新配置文件。这就是上述情况下发生的事情......
mv -i .git/refs/new-master .git/refs/master
git checkout master
回答by Alan Haggai Alavi
Rename the branch to master
by:
将分支重命名为master
:
git branch -M branch_name master
回答by fregante
From what I understand, you can branch the current branch into an existing branch. In essence, this will overwrite master
with whatever you have in the current branch:
据我了解,您可以将当前分支分支到现有分支中。本质上,这将覆盖master
您在当前分支中的任何内容:
git branch -f master HEAD
Once you've done that, you can normally push your local master
branch, possibly requiring the forceparameter here as well:
完成后,您通常可以推送本地master
分支,可能还需要此处的force参数:
git push -f origin master
No merges, no long commands. Simply branch
and push
— but, yes, this will rewrite historyof the master
branch, so if you work in a team you have got to know what you're doing.
没有合并,没有长命令。只要branch
和push
-但是,是的,这将改写历史的的master
分支,所以如果你在,你得知道你在做什么,一个团队中工作。
Alternatively, I found that you can push any branch to the any remote branch, so:
或者,我发现您可以将任何分支推送到任何远程分支,因此:
# This will force push the current branch to the remote master
git push -f origin HEAD:master
# Switch current branch to master
git checkout master
# Reset the local master branch to what's on the remote
git reset --hard origin/master
回答by SherylHohman
I found the answer I wanted in the blog post Replace the master branch with another branch in git:
我在博客文章Replace the master branch with another branch in git 中找到了我想要的答案:
git checkout feature_branch
git merge -s ours --no-commit master
git commit # Add a message regarding the replacement that you just did
git checkout master
git merge feature_branch
It's essentially the same as Cascabel's answer. Except that the "option" he added belowhis solution is already embedded in my main code block.
它与Cascabel's answer基本相同。除了他在他的解决方案下面添加的“选项”已经嵌入到我的主代码块中。
It's easier to find this way.
这种方式更容易找到。
I'm adding this as a new answer, because if I need this solution later, I want to have allthe code Iam going to use in one code block.
我加入这是一个新的答案,因为如果我以后需要这个解决方案,我想拥有所有的代码我打算在一个代码块使用。
Otherwise, I may copy-paste, thenread details below to see the line that I shouldhave changed - after I already executed it.
否则,我可能会复制粘贴,然后阅读下面的详细信息以查看我应该更改的行 - 在我已经执行之后。
回答by VonC
The solutions given here (renaming the branch in 'master') don't insist on the consequences for the remote (GitHub) repo:
此处给出的解决方案(重命名“master”中的分支)不坚持远程(GitHub)存储库的后果:
- if you hadn't push anything since making that branch, you can rename it and push it without any problem.
- if you had push master on GitHub, you will need to 'git push -f' the new branch: you can no longer push in a fast forward mode.
- 如果您在创建该分支后没有推送任何内容,则可以重命名并推送它而不会出现任何问题。
- 如果你在 GitHub 上有 push master,你将需要 'git push -f' 新分支:你不能再以快进模式推送。
-f --force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.
通常,该命令拒绝更新不是用于覆盖它的本地 ref 的祖先的远程 ref。此标志禁用检查。这可能会导致远程存储库丢失提交;小心使用它。
If others have already pulled your repo, they won't be able to pull that new master history without replacing their own master with that new GitHub master branch (or dealing with lots of merges).
There are alternatives to a git push --force for public repos.
Jefromi's answer(merging the right changes back to the original master) is one of them.
如果其他人已经拉取了您的 repo,他们将无法拉取新的 master 历史记录,而无需将他们自己的 master 替换为新的 GitHub master 分支(或处理大量合并)。git push --force 对于公共回购
有替代方案。
Jefromi 的答案(将正确的更改合并回原来的主人)就是其中之一。
回答by Mark Dietel
I found this simple method to work the best. It does not rewrite history and all previous check-ins of branch will be appended to the master. Nothing is lost, and you can clearly see what transpired in the commit log.
我发现这个简单的方法效果最好。它不会重写历史记录,并且所有以前的分支签入都将附加到主节点。没有丢失任何东西,您可以清楚地看到提交日志中发生了什么。
Objective: Make current state of "branch" the "master"
目标:使“分支”的当前状态成为“主”
Working on a branch, commit and push your changes to make sure your local and remote repositories are up to date:
在分支上工作,提交并推送您的更改以确保您的本地和远程存储库是最新的:
git checkout master # Set local repository to master
git reset --hard branch # Force working tree and index to branch
git push origin master # Update remote repository
After this, your master will be the exact state of your last commit of branch and your master commit log will show all check-ins of the branch.
在此之后,您的 master 将是您最后一次提交分支的确切状态,并且您的 master 提交日志将显示该分支的所有签入。
回答by user2064284
One can also checkout all files from the other branch into master:
还可以将另一个分支中的所有文件检出到 master 中:
git checkout master
git checkout better_branch -- .
and then commit all changes.
然后提交所有更改。
回答by staafl
To add to Jefromi's answer, if you don't want to place a meaningless merge in the history of the source
branch, you can create a temporary branch for the ours
merge, then throw it away:
要补充 Jefromi 的答案,如果您不想在source
分支的历史记录中放置无意义的合并,则可以为ours
合并创建一个临时分支,然后将其丢弃:
git checkout <source>
git checkout -b temp # temporary branch for merge
git merge -s ours <target> # create merge commit with contents of <source>
git checkout <target> # fast forward <target> to merge commit
git merge temp # ...
git branch -d temp # throw temporary branch away
That way the merge commit will only exist in the history of the target
branch.
这样合并提交将只存在于target
分支的历史记录中。
Alternatively, if you don't want to create a merge at all, you can simply grab the contents of source
and use them for a new commit on 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>