简体   繁体   English

如何使用GIT将'master'分支完全重置为'dev'的内容?

[英]How do I use GIT to completely reset the 'master' branch to the contents of 'dev'?

Git is something that I have always had difficulties comprehending. Git是我一直难以理解的东西。 A common mistake that I often make when working on a personal project is committing work to 'master' instead of 'dev'. 在进行个人项目时,我经常犯的一个常见错误是将工作委托给“主”而不是“开发”。

I would like to know how to use Git 'reset' the master branch. 我想知道如何使用Git'重置'master分支。 Specifically, if 'master' is ahead of the 'dev' branch by some number of commits, how do I reset 'master' to the contents of 'dev'. 具体来说,如果“ master”比“ dev”分支领先了一定数量的提交,我如何将“ master”重置为“ dev”的内容。 This is assuming the commits in master are not needed. 这是假设不需要master中的提交。

Thank you. 谢谢。

So, if the commits in master are really not needed, the git reset answers are correct (well, assuming you have not pushed master yet). 因此,如果确实不需要master中的提交,则git reset答案是正确的(假设您尚未推送master)。

However, you say: 但是,您说:

A common mistake that I often make when working on a personal project is committing work to 'master' instead of 'dev'. 在进行个人项目时,我经常犯的一个常见错误是将工作委托给“主”而不是“开发”。

If this is the case, why throw away the commits you made on master ? 如果是这种情况,为什么要丢弃您对master所做的提交? I would just swap the branches (if master is strictly ahead of dev ): 我只交换分支(如果master严格在dev ):

git checkout master
git branch temp
git reset --hard dev
git checkout dev
git reset --hard temp
git branch -d temp

You're asking how to "reset" the master branch. 您在问如何“重置”主分支。 Good news for you, 对你来说是个好消息,

git checkout master
git reset --hard dev # loses commits on master

Assuming you are on the master branch, this command will set the master branch to match the dev branch. 假设您在master分支上,此命令将设置master分支以匹配dev分支。

git reset --hard dev

Please note that this will erase any of the work that you have committed in the master branch that is not included in the dev branch. 请注意,这将删除您在master分支中提交的,未包含在dev分支中的任何工作。 If you want to save the state of the files that you have changed in the master branch but remove those commits in master, you can run this command which will reset the commits to the current state of dev but will not remove the changes that you have made to the files locally. 如果要在master分支中保存已更改文件的状态,但在master中删除这些提交,则可以运行此命令,该命令会将提交重置为dev的当前状态,但不会删除您拥有的更改本地制作文件。 These changes can then be committed. 然后可以提交这些更改。

git reset --soft dev

master is only the default branch name in git(in fact, only a reference), why bother with commit to it. master只是git中的默认分支名称(实际上,只是一个引用),为什么还要花时间来提交它。 you can rename it to any other name, and create a new branch named master from any start-pointer as you will. 您可以将其重命名为任何其他名称,然后根据需要从任何起始指针创建一个名为master的新分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM