简体   繁体   English

复制的git目录的文件更改丢失

[英]File changes of copied git directory are lost

Ok, so I'm pretty sure I totally hosed this up, but I'm hoping someone can tell me what I did wrong and the proper way to do this moving forward. 好的,我可以肯定地说,我完全可以解决这个问题,但是我希望有人可以告诉我我做错了什么,以及继续进行下去的正确方法。

Basically, I made some changes and then realized I was in the wrong "branch". 基本上,我进行了一些更改,然后意识到自己处于错误的“分支”中。 So, I thought I could simply copy the folder with my changes to another location, then discard my uncomitted changes, switch to the correct branch, then past my copied folder over the "new" directory. 因此,我认为我可以将包含更改的文件夹简单地复制到另一个位置,然后丢弃未提交的更改,切换到正确的分支,然后将复制的文件夹放在“新”目录上。 However, it looks like none of my pending changes were copied over with the folder. 但是,看来我所有未完成的更改都没有复制到该文件夹​​中。 Instead, the files all match the original state. 而是,所有文件都匹配原始状态。 ie when I past the copied folder onto the "correct" branch, nothing is changed... 即当我将复制的文件夹移到“正确”分支上时,什么都没有改变...

I lost about an hours worth of work. 我损失了大约一个小时的工作时间。 Can someone please explain what happened? 有人可以解释发生了什么吗?

It is absolutely possible to copy modified files over to a different branch. 绝对有可能将修改后的文件复制到另一个分支。

But take care! 但是要小心! Depending on the way how you're copying it, you might also copy the hidden .git -folder, which contains the current state of your local git-repository. 根据您是如何复制的方式,您可能还复制隐藏.git -folder,其中包含本地的git的仓库的当前状态。 So by pasting that copied hidden git-folder back, you just revert all changes you made to the repository in between. 因此,通过将复制的隐藏git-folder粘贴回去,您只需还原对存储库之间所做的所有更改。

Fortunately, git provides the comfortable stash command to achieve the same without copying anything by hand. 幸运的是,git提供了舒适的stash命令来实现相同功能,而无需手动复制任何内容。


Use git stash to stash your modifications away (they're stored in the hidden git-backend): 使用git stash来保存您的修改(它们存储在隐藏的git-backend中):

$ git stash
Saved working directory and index state WIP on master: 8ba311c Initial Commit
HEAD is now at 8ba311c Initial commit
$

Then change to your desired branch: 然后转到所需的分支:

$ git checkout desired-branch
Switched to branch 'desired-branch'
$

And finally reapply your modifications: 最后重新应用您的修改:

$ git stash apply
On branch desired-branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

     new file:   my-changes.txt

$

Ok... Figured it out. 好吧...想通了。 I "stashed" my uncomitted changes, then switched to the correct branch and "applied" my stashed changes. 我“隐藏”了未完成的更改,然后切换到正确的分支并“应用”了我的未更改。

https://answers.atlassian.com/questions/181773/change-commit-code-to-other-branch-sourcetree-for-windows https://answers.atlassian.com/questions/181773/change-commit-code-to-other-branch-sourcetree-for-windows

A git way to do this is to commit the changes on the wrong branch, cherry-pick the commit in the correct branch, and reset the other branch to before that commit. git的方法是在错误的分支上提交更改,在正确的分支中选择该提交,然后将另一个分支重置为该提交之前。 No copying involved, full reflog support of all involved operations. 不涉及复制,对所有相关操作均提供全面的支持。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM