切换到另一个分支而不更改工作区文件 [英] Switch to another branch without changing the workspace files

查看:434
本文介绍了切换到另一个分支而不更改工作区文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从GitHub克隆了一个git仓库,做了一些更改和一些提交;我做了很多,都很肮脏,所以他们不适合拉请求。现在我从 origin / master 创建了分支 cleanchanges ,所以它很干净,我想在那里提交我的更改为一个提交了一个很好的提交评论。



当我在本地主服务器上时,我想切换到我的 cleanchanges 但不更改文件。然后我就可以提交。



如何在不更改文件的情况下切换分支?



我想要说清楚:我已经在本地 master 中提交了所有更改。

我刚刚注意到你说过你已经创建了一些提交。在这种情况下,使用 git merge --squash 进行一次提交:

  git checkout cleanchanges 
git merge --squash master
git commit -m为我所有的更改提供好评






编辑:)如果您有未提交更改, )



只需使用 git checkout cleanchanges 切换分支。如果分支引用同一个参考,那么当您切换时,所有未提交的更改将保留在您的工作目录中。



唯一一次发生冲突的时间是if版本库中的一些文件在 origin / master cleanchanges 之间是不同的。如果您刚刚创建了分支,那就没有问题。



与往常一样,如果您担心丢失工作,请先备份备份。 Git的设计目的不是要求您不要先丢掉工作。


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.

解决方案

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:

git checkout cleanchanges
git merge --squash master
git commit -m "nice commit comment for all my changes"


(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 and cleanchanges. 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.

这篇关于切换到另一个分支而不更改工作区文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆