将选择的更改提交到另一个分支,然后恢复当前分支的工作?

发布于 2024-09-26 03:40:35 字数 357 浏览 6 评论 0原文

这种情况经常发生在我的工作流程中:我正在一个单独的分支中开发一个功能,在执行此操作时,我会遇到需要修复但来自框架或站点布局较高层的小问题。

我想切换回主 develop 分支并从那里选择的文件提交更改,然后返回到 feature 分支,并重新设置基准,以便我可以继续那里不相关的调整/错误修复。

我正在使用 git stash 和 git stash pop 来执行此操作,但我从我修改但不需要的一堆文件中遇到了很多冲突无论如何都要致力于父分支。

是否有另一种方法可以避免冲突或以某种方式保存当前状态,并且仅将选择的工作树更改拉到另一个分支以进行提交? (有点像 git-stash-cherry-pick ;-))

This happens often in my work flow: I'm working on a feature in a separate branch, and while doing this I'll come across small things that need fixing but are from higher up in the framework or site layout for example.

I want to switch back to the main develop branch and commit changes from select files there, then go back to a feature branch, and rebase so that I can continue there with the unrelated tweaks/bugfixes out of the way.

I'm using git stash and git stash pop to do this, but I'm getting a lot of conflicts from a bunch of files I modifed but don't need to be committed to the parent branch anyway.

Is there another way to avoid conflicts or somehow save the current state, and only pull select working tree changes to another branch for commiting ? (sort of like git-stash-cherry-pick ;-))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

与君绝 2024-10-03 03:40:35
  1. 在当前分支的 master 中提交您想要的更改。
  2. 隐藏剩余的更改
  3. 切换到 master 分支并使用 gitcherry-pick 将更改移动到 master
  4. 移回您的 Feather 分支,然后 < code>rebase (可选)
  5. 取消存储原始功能更改
  1. Commit the changes that you want in master on your current branch.
  2. Stash your remaining changes
  3. Switch to master branch and use git cherry-pick to move the changes onto master
  4. Move back to your feather branch, and rebase (optional)
  5. Unstash the original feature changes
雨巷深深 2024-10-03 03:40:35

我通常会反过来做。我继续在我的功能分支中工作,直到我准备好在那里提交。一旦我这样做,我就会将属于该分支的新提交的所有更改添加到索引,但不添加属于 master 的更改。 git add -p 等让这变得非常简单。一旦索引中的所有相关更改,我都会提交到分支。所有剩余的脏更改都属于 master,一旦我切换到 master,就会很好地进行,这样我就可以在那里提交它。

I usually do it the other way around. I continue working in my feature branch until I'm ready to make a commit there. As soon as I am, I add all changes that belong into the new commit for the branch to the index, but not those that belong into master. git add -p et.al make that really easy. Once all relevant changes in the index, I commit to the branch. All remaining remaining dirty changes belong to master and will be carried along just fine once I switch to that so I can commit it there.

寒江雪… 2024-10-03 03:40:35

尝试使用 --merge 选项切换回 master 分支。它将尝试在两个分支之间进行三向合并。 git 文档有一个很好的例子:

<前><代码>2。在错误的分支工作后,切换到正确的分支
分支将使用以下方式完成:

$ git checkout 我的主题

但是,您的“错误”分支和正确的“mytopic”分支
您在本地修改的文件可能有所不同,其中
如果上面的结账会失败,如下所示:

$ git checkout 我的主题
错误:您对“froz”进行了本地更改;不切换分支。

您可以为该命令提供 -m 标志,该命令将尝试
三路合并:

$ git checkout -m mytopic
自动合并 frotz

经过三路合并后,本地修改不再
在你的索引文件中注册,所以 git diff 会告诉你什么
自新分支尖端以来您所做的更改。

Try switching back to master branch using --merge option. It will try to do a three way merge between the two branches. The git documentation has a good example:

2. After working in the wrong branch, switching to the correct
   branch would be done using:

      $ git checkout mytopic

   However, your "wrong" branch and correct "mytopic" branch
   may differ in files that you have modified locally, in which 
   case the above checkout would fail like this:

      $ git checkout mytopic
      error: You have local changes to 'frotz'; not switching branches.

   You can give the -m flag to the command, which would try a
   three-way merge:

      $ git checkout -m mytopic
      Auto-merging frotz

   After this three-way merge, the local modifications are not
   registered in your index file, so git diff would show you what
   changes you made since the tip of the new branch.
瑾兮 2024-10-03 03:40:35

在 MacOS 上, GitX 使得执行 rafl 描述的那种选择性提交变得非常容易,所以这是一个好方法如果这就是您所处的环境,请接近它。

也可以/实用地在单独的提交中提交分支 y 更改和主 y 更改,然后使用 git format-patch 将分支中的提交导出为文件和 git am 来拉取它们到主人那里。

这里的危险是,如果更改周围的文件差异太大,在这种情况下,将提交拉入主服务器时可能会发生冲突。

On MacOS, GitX makes it very easy to do the kind of selective committing rafl describes, so that's a good way to approach it if that's the environment you're in.

It's also possible/practical to commit branch-y changes and master-y changes in separate commits, then use git format-patch to export the commits from the branch as files and git am to pull them in to the master.

The danger here is if the files around the changes are too different, in which case there may be conflicts when pulling the commits in to the master.

踏月而来 2024-10-03 03:40:35

创建一个临时分支怎么样?

像这样的东西:

- oh crap need to do somethning else now
- git checkout -b ResumeLater
- git add .
- git commit
- git checkout ImportantStuff
- ....
- git checkout ResumeLater

What about creating a temporary branch?

Something like:

- oh crap need to do somethning else now
- git checkout -b ResumeLater
- git add .
- git commit
- git checkout ImportantStuff
- ....
- git checkout ResumeLater
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文