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

发布于 2024-12-22 20:58:34 字数 322 浏览 1 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

落在眉间の轻吻 2024-12-29 20:58:34

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

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

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

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

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

与往常一样,如果您担心丢失工作,请先制作备份副本。 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:

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.

拥醉 2024-12-29 20:58:34

吉特.切换到另一个分支

git checkout branch_name

Git. Switch to another branch

git checkout branch_name
心碎的声音 2024-12-29 20:58:34

最好的选择是存储更改并切换分支。对于切换分支,您需要一个干净的状态。因此,将它们隐藏起来,检出一个新分支,然后在新分支上应用更改并提交

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

ζ澈沫 2024-12-29 20:58:34

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

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

git status
git add .
git commit

第一次(硬)重置会将您的工作树设置为与 master 中的最后一次提交相同。

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


之后,如果您想删除从 master 所做的脏提交(假设您尚未推送它们),您可以:

git checkout master
git reset --hard origin/master

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

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.

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:

git checkout master
git reset --hard origin/master

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

若沐 2024-12-29 20:58:34

为什么不直接git reset --soft

演示:

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

结果:

$ 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

但需要注意的一件事是,当前分支更改为原始。处理完成后,您仍然留在上一个分支,但可以轻松地git checkout original,因为它是相同的状态。如果您不想丢失之前的 HEAD,您应该记下提交引用并执行 gitbranch -f;之后<提交>

Why not just 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.

深陷 2024-12-29 20:58:34

干净地切换到另一个分支 cleanchanges 并保留当前工作区文件 - 没有副作用或中间提交:

# starting from branch master
git switch --detach               # don't move the current branch
git reset cleanchanges
git switch cleanchanges

这甚至允许未提交的更改,并且不会更改文件的时间戳。

为了还保留索引(暂存区域),从而分别查看脏更改的差异,请执行以下操作:

# starting from master
git switch --detach               # don't move the current branch
git reset --soft cleanchanges
git switch cleanchanges

Cleanly switching to another branch cleanchanges with current workspace files preserved - without side effects or intermediate commits:

# starting from branch master
git switch --detach               # don't move the current branch
git reset cleanchanges
git switch cleanchanges

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:

# starting from master
git switch --detach               # don't move the current branch
git reset --soft cleanchanges
git switch cleanchanges
小傻瓜 2024-12-29 20:58:34

最简单的方法如下:

git fetch && git checkout <branch_name>

Simplest way to do is as follows:

git fetch && git checkout <branch_name>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文