简体   繁体   English

将未提交的更改从当前分支移动到与那些更改冲突的另一个分支

[英]Move uncommitted changes from current branch to another branch that conflicts with those changes

Suppose I am on branch master and I start making some changes. 假设我在分支master并且开始进行一些更改。

I make the changes to a file which is already opened in Emacs (so under the hood, as checkouts happen, Emacs is unaware unless I revert-buffer constantly). 我对已经在Emacs中打开的文件进行了更改(因此,在实际操作中,随着结帐的发生,除非我不断还原缓冲区,否则Emacs不会意识到)。

The file did exist in branch other_branch which was intended to be merged into master later on. 该文件确实存在于分支other_branch ,该分支other_branch打算合并到master中。 But the file did not exist in master until I accidentally saved it from Emacs. 但是,直到我不小心从Emacs中保存了文件之后,该文件才在master不存在。

The changes are uncommitted, but I realize that I shouldn't have been making the changes on master and had intended to checkout a different branch before starting on the changes. 更改未提交,但是我意识到我不应该在master上进行更改,而是打算在开始更改之前签出其他分支。

I don't want to lose the current work, but also don't want to commit it to master . 我不想丢失当前的工作,但也不想将其提交给master

I have tried using git stash followed by git checkout other_branch but this gives me an error saying that, because of the uncommitted changes, I am not allowed to switch branches to other_branch : 我已经尝试过使用git stash然后使用git checkout other_branch但这给我一个错误,说由于未提交的更改,不允许将分支切换到other_branch

ems@computer:~$ git checkout other_branch
error: The following untracked working tree files would be overwritten by checkout:
    some_file
Please move or remove them before you can switch branches.
Aborting

The files that I began (accidentally) modifying in master did already exist in other_branch , so just checking out other_branch would destroy my working directory copy, with the changes, correct? 我开始(偶然地)在master进行修改的文件已经存在于other_branch ,因此仅检出other_branch会破坏我的工作目录副本,并进行更改,对吗?

I tried stash , which did seem to stash the changes, but then I got the error message above when I tried to checkout. 我尝试了stash ,它似乎确实隐藏了更改,但是当我尝试结帐时,出现了以上错误消息。

Summary 摘要

  1. checkout some new branch, other_branch tracking master . 结帐一些新分支, other_branch跟踪master

  2. create a new file test.txt in other_branch and open it for editing in Emacs. other_branch创建一个新文件test.txt ,然后将其打开以在Emacs中进行编辑。 Commit to other_branch . 提交到other_branch

  3. back in the terminal, checkout master. 回到终端,结帐主人。

  4. Back in Emacs, you forget you're now on master, start making some changes to test.txt and save the file. 回到Emacs,您会忘记自己现在已经掌握了,开始对test.txt进行一些更改并保存文件。 It's a totally new file as far as master is concerned. master而言,这是一个全新的文件。

How do you get those changes over into other_branch ? 您如何将这些更改转移到other_branch

If the untracked files are an issue during the checkout, maybe you can try: 如果在结帐期间未跟踪的文件是一个问题,也许您可​​以尝试:

git stash --include-untracked

From the git stash man page : git stash手册页

If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean , leaving the working directory in a very clean state 如果使用了--include-untracked选项,则所有未跟踪的文件也会被保存,然后使用git clean ,使工作目录处于非常干净的状态


Since that doesn't work either (on the stash pop), you could imply: 由于这也不起作用(在隐藏弹出窗口中),您可能暗示:

  • Make the commmit (in master branch) 进行提交(在master分支中)
  • checkout develop and cherry-pick that commmit 结帐发展和承诺的挑剔
  • checkout to master and reset it to HEAD~ 结帐以掌握并重置为HEAD~

Temporarily rename the file ? 临时重命名文件?

$ mv test.txt test.txt.tmp
$ git checkout other_branch
$ mv test.txt.tmp test.txt

Inspect changes & commit 检查变更并提交

What worked for me was the following: 对我有用的是:

From master do git add for the untracked, conflicting file. mastergit add为未跟踪的冲突文件git add文件。 Now that it is tracked, do git stash and checkout other_branch . 现在已经跟踪了,执行git stash并检出other_branch Now, git stash pop will attempt to merge the tracked-in-stash version with the tracked-in- other_branch version. 现在, git stash pop将尝试将other_branch -in- other_branch版本与other_branch -in- other_branch版本other_branch This probably will create a merge conflict, so you have to open the file(s) and resolve any conflicts, and be sure to git add and commit them on other_branch . 这可能会导致合并冲突,因此您必须打开文件并解决所有冲突,并确保在other_branchgit addcommit它们。 Then go back to master and just verify that the files are not staged for commit and are not present in that branch. 然后回到master并验证文件是否未暂存以提交并且在该分支中不存在。

This allows them to be treated as if they were tracked within the stash, but without needing to actually commit and merge from master. 这样,就可以将它们视为已在存储中进行了跟踪,而无需实际从master提交和合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Git上将未提交的更改从一个分支移动到另一个分支 - How to move changes as uncommitted from one branch to another on Git 当前分支上有未提交的更改时签出另一个分支 - Checkout another branch when there are uncommitted changes on the current branch 使用git,如何将一些未提交的更改从一个分支移动到另一个文件夹中的另一个分支? - Using git, how do you move some uncommitted changes from one branch to another branch in a different folder? 将分支提交到当前目录作为对工作目录的未提交更改? - Move branch commits to current one as uncommitted changes to working directory? 移至另一个分支并“保存”当前更改 - move to another branch and 'saving' current changes Git(无分支)具有未提交的更改 - Git (no branch) with uncommitted changes 将master分支中未提交的更改移至dev分支并还原master - Move uncommitted changes in master branch to dev branch and revert master 即使当前分支中有未提交的更改,如何以编程方式编辑另一个分支中的文件? - How to programmatically edit files in another branch even when there are uncommitted changes in the current branch? 如何从当前分支的先前分支移动更改? - How to move changes from previous branch of the current branch? 将更改从一个分支移动到另一个分支 - Move changes from one branch to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM