切换到另一个分支而不更改工作区文件
我从 GitHub 克隆了一个 git 存储库,做了一些更改和一些提交;我做了很多,而且都很脏,所以它们不适合拉请求。现在,我从 origin/master
创建了分支 cleanchanges
,所以它是干净的,我想将我的更改作为一次提交提交,并带有良好的提交注释。
当我在本地主机上时,我想切换到我的 cleanchanges
但不更改文件。然后我就可以承诺了。
如何在不更改文件的情况下切换分支?
我想澄清一下:我已在本地 master
中提交了所有更改。没有未提交的更改。
I cloned a git repository from GitHub, made some changes and some commits; I made quite a lot and all are quite dirty, so they're not suitable for a pull request. Now I created the branch cleanchanges
from origin/master
, so it's clean, and I want to commit my changes there as one commit with a nice commit comment.
When I am on the local master, I want to switch to my cleanchanges
but without changing the files. And then I'm able to commit.
How can I switch branches without changing files?
I want to make it clear: I have all the changes committed in the local master
. There are no uncommitted changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编辑:我刚刚注意到您说您已经创建了一些提交。在这种情况下,请使用 git merge --squash 进行一次提交:(
编辑:如果您有未提交的更改,则以下答案适用。 )
只需使用 git checkout cleanchanges 切换分支即可。如果分支引用相同的引用,那么当您切换时,所有未提交的更改都将保留在您的工作目录中。
唯一会发生冲突的情况是存储库中的某些文件在
origin/master
和cleanchanges
之间不同。如果您刚刚创建了分支,那么没问题。与往常一样,如果您担心丢失工作,请先制作备份副本。 Git 的设计初衷是在没有事先询问您的情况下不会放弃工作。
Edit: I just noticed that you said you had already created some commits. In that case, use
git merge --squash
to make a single commit:(Edit: The following answer applies if you have uncommitted changes.)
Just switch branches with
git checkout cleanchanges
. If the branches refer to the same ref, then all your uncommitted changes will be preserved in your working directory when you switch.The only time you would have a conflict is if some file in the repository is different between
origin/master
andcleanchanges
. If you just created the branch, then no problem.As always, if you're at all concerned about losing work, make a backup copy first. Git is designed to not throw away work without asking you first.
吉特.切换到另一个分支
Git. Switch to another branch
最好的选择是存储更改并切换分支。对于切换分支,您需要一个干净的状态。因此,将它们隐藏起来,检出一个新分支,然后在新分支上应用更改并提交
The best bet is to stash the changes and switch branch. For switching branches, you need a clean state. So stash them, checkout a new branch and apply the changes on the new branch and commit it
另一种方式,如果您想创建一个新的提交而不是执行合并:
第一次(硬)重置会将您的工作树设置为与
master
中的最后一次提交相同。第二次重置会将您的 HEAD 放回到原来的位置,指向 cleanchanges 分支的尖端,但不会更改任何文件。所以现在您可以添加并提交它们。
之后,如果您想删除从
master
所做的脏提交(假设您尚未推送它们),您可以:这将丢弃所有新提交,返回您的本地
master
分支到与存储库中的提交相同的提交。Another way, if you want to create a new commit instead of performing a merge:
The first (hard) reset will set your working tree to the same as the last commit in
master
.The second reset will put your HEAD back where it was, pointing to the tip of the
cleanchanges
branch, but without changing any files. So now you can add and commit them.Afterwards, if you want to remove the dirty commits you made from
master
(and assuming you have not already pushed them), you could:This will discard all your new commits, returning your local
master
branch to the same commit as the one in the repository.为什么不直接
git reset --soft
?演示:
结果:
但需要注意的一件事是,当前分支更改为
原始
。处理完成后,您仍然留在上一个分支,但可以轻松地git checkout original
,因为它是相同的状态。如果您不想丢失之前的HEAD
,您应该记下提交引用并执行gitbranch -f;之后<提交>
。Why not just
git reset --soft <branch_name>
?Demonstration:
Result:
One thing to note though, is that the current branch changes to
original
. You’re still left in the previous branch after the process, but can easilygit checkout original
, because it’s the same state. If you do not want to lose the previousHEAD
, you should note the commit reference and dogit branch -f <previous_branch> <commit>
after that.干净地切换到另一个分支
cleanchanges
并保留当前工作区文件 - 没有副作用或中间提交:这甚至允许未提交的更改,并且不会更改文件的时间戳。
为了还保留索引(暂存区域),从而分别查看脏更改的差异,请执行以下操作:
Cleanly switching to another branch
cleanchanges
with current workspace files preserved - without side effects or intermediate commits:This allows even uncommited changes, and doesn't change the timestamps of files.
In order to also preserve the index (staging area) and thus see the diffs of dirty changes separately, do like this:
最简单的方法如下:
Simplest way to do is as follows: