It happens that in a branch where I was making code fixes by mistake I started to make some changes (modify and create files) that corresponded to another branch. How could you pass some specific files from one branch to another? Thank you!
Asked
Active
Viewed 28 times
1 Answers
0
The simplest way would be to stash
the changes, move to the correct branch, and apply the stashed changes. Please note that stash
defaults to the push
subcommand, so git stash
and git stash push
are equivalent.
git stash [<pathspec>...] # store the changes in the current tree
git switch <correct_branch> # move to the proper branch (use checkout if git < 2.23)
# apply the changes to the correct branch
git stash apply
git stash drop
You could combine the last two lines into git stash pop
, the only difference being that the stash is automatically dropped and if you need to use it again for some reason, you will have lost access to it.

joshmeranda
- 3,001
- 2
- 10
- 24
-
Yes! But I want stash only some files and moves to another branch, not all files – Alfonso Piedrasanta Aug 26 '20 at 14:38
-
You can simply specify the paths you want to stash after the command, you can read more [here](https://stackoverflow.com/questions/5506339/how-can-i-git-stash-a-specific-file) – joshmeranda Aug 26 '20 at 14:52
-
Awesome! Happy it help – joshmeranda Aug 26 '20 at 15:50