简体   繁体   中英

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. So by pasting that copied hidden git-folder back, you just revert all changes you made to the repository in between.

Fortunately, git provides the comfortable stash command to achieve the same without copying anything by hand.


Use git stash to stash your modifications away (they're stored in the hidden 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

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. No copying involved, full reflog support of all involved operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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