简体   繁体   中英

Git. How to save changes in wrong branch

I have 2 branches in my git project( Let it be branch A and B). I made some changes in branch A, but I need it in B, not A. How I can switch to other branch? When I try "git checkout B" git says that I made some changes and cant switch without commit. It is not a good way to save files manually, then remove them, then change branch and paste modified files.

It sounds like you started working in branch A , made some changes, but then realized that you really should have been working in branch B . Don't worry, git stash will come to the rescue to save you.

Just type git stash from the Bash prompt and the changes from your current working directory will be stashed away. Note that Git also stashes your current stage, but I will assume that you have not yet staged anything.

Then, just switch to the B branch as you normally would:

git checkout B

And apply the stash:

git stash apply

Now if you type git status , you should see all the changes you made in the A branch.

If you then also want to commit this work, then git add the appropriate files and do a git commit into the B branch.

Note that there is another option here involving making a format commit in the A branch, then moving it later to the B branch, but that solution is more complex, and I would probably use git stash here.

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