git 切换到另一个分支而不更改工作区文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8650310/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:36:25  来源:igfitidea点击:

Switch to another branch without changing the workspace files

gitbranchgit-branch

提问by amorfis

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 cleanchangesfrom origin/master, so it's clean, and I want to commit my changes there as one commit with a nice commit comment.

我从 GitHub 克隆了一个 git 存储库,进行了一些更改和一些提交;我做了很多,而且都很脏,所以它们不适合拉请求。现在我cleanchanges从 中创建了分支origin/master,所以它很干净,我想在那里提交我的更改,作为一个带有漂亮提交注释的提交。

When I am on the local master, I want to switch to my cleanchangesbut without changing the files. And then I'm able to commit.

当我在本地 master 上时,我想切换到我的cleanchanges但不更改文件。然后我就可以承诺了。

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.

我想说清楚:我在本地master. 没有未提交的更改。

采纳答案by Greg Hewgill

Edit:I just noticed that you said you had already created some commits. In that case, use git merge --squashto make a single commit:

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

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 uncommittedchanges.)

编辑:如果您有未提交的更改,则以下答案适用。)

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.

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

The only time you would have a conflict is if some file in the repository is different between origin/masterand cleanchanges. If you just created the branch, then no problem.

唯一会发生冲突的情况是,如果存储库中的某个文件在origin/master和之间不同cleanchanges。如果你刚刚创建了分支,那么没问题。

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 旨在不先询问您就放弃工作。

回答by Surendra Kumar Ahir

Git. Switch to another branch

吉特。切换到另一个分支

git checkout branch_name

回答by vivek85

The best bet is to stashthe 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

最好的办法是隐藏更改并切换分支。对于切换分支,您需要一个干净的状态。所以把它们藏起来,签出一个新分支并将更改应用到新分支上并提交它

回答by timdev

It sounds like you made changes, committing them to master along the way, and now you want to combine them into a single commit.

听起来您进行了更改,并在此过程中将它们提交给 master,现在您想将它们合并为一个提交。

If so, you want to rebaseyour commits, squashing them into a single commit.

如果是这样,你要变基您的提交,挤压成一个单一的提交。

I'm not entirely sure of what exactly you want, so I'm not going to tempt you with a script. But I suggest you read up on git rebaseand the options for "squash"ing, and try a few things out.

我不完全确定你到底想要什么,所以我不会用脚本来诱惑你。但我建议你仔细阅读git rebase和“挤压”的选项,并尝试一些事情。

回答by joeytwiddle

Another way, if you want to create a new commit instead of performing a merge:

另一种方式,如果你想创建一个新的提交而不是执行合并:

git checkout cleanchanges
git reset --hard master
git reset cleanchanges

git status
git add .
git commit

The first (hard) reset will set your working tree to the same as the last commit in master.

第一次(硬)重置会将您的工作树设置为与master.

The second reset will put your HEAD back where it was, pointing to the tip of the cleanchangesbranch, but without changing any files. So now you can add and commit them.

第二次重置会将您的 HEAD 放回原来的位置,指向cleanchanges分支的尖端,但不会更改任何文件。所以现在你可以添加和提交它们。



Afterwards, if you want to remove the dirty commits you made from master(and assuming you have not already pushed them), you could:

之后,如果你想删除你所做的脏提交master(假设你还没有推送它们),你可以:

git checkout master
git reset --hard origin/master

This will discard all your new commits, returning your local masterbranch to the same commit as the one in the repository.

这将丢弃您所有的新提交,将您的本地master分支返回到与存储库中相同的提交。

回答by Mandeep Singh

Simplest way to do is as follows:

最简单的方法如下:

git fetch && git checkout <branch_name>

回答by Yushin Washio

Why not just git reset --soft <branch_name>?

为什么不只是git reset --soft <branch_name>

Demonstration:

示范:

mkdir myrepo; cd myrepo; git init
touch poem; git add poem; git commit -m 'add poem'  # first commit
git branch original
echo bananas > poem; git commit -am 'change poem'  # second commit
echo are tasty >> poem  # unstaged change
git reset --soft original

Result:

结果:

$ git diff --cached
diff --git a/poem b/poem
index e69de29..9baf85e 100644
--- a/poem
+++ b/poem
@@ -0,0 +1 @@
+bananas
$ git diff
diff --git a/poem b/poem
index 9baf85e..ac01489 100644
--- a/poem
+++ b/poem
@@ -1 +1,2 @@
 bananas
+are tasty

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 easily git checkout original, because it's the same state. If you do not want to lose the previous HEAD, you should note the commit reference and do git branch -f <previous_branch> <commit>after that.

不过要注意的一件事是,当前分支更改为original. 在这个过程之后,你仍然留在上一个分支,但很容易git checkout original,因为它是相同的状态。如果您不想丢失以前的HEAD,则应注意提交参考并在此git branch -f <previous_branch> <commit>之后执行。

回答by Jwala Kumar

git fetch && git checkout branch_name( "branch_name" here is the name of the branch )

git fetch && git checkout branch_name(“branch_name”这里是分支的名称)

Then you will see message, Switched to a new branch 'branch_name' Branch 'branch_name' set up to track remote branch 'branch_name' from 'origin'.

然后你会看到消息,切换到一个新的分支 'branch_name' 分支 'branch_name' 设置为跟踪来自 'origin' 的远程分支 'branch_name'。