Git: Committing to a Different Branch
August 10, 2023 —
Gregg Szumowski
Let’s imaging that you’ve made some changes to files in your work directory and then realized that you aren’t in the correct branch. Now you want to get the changes you’ve made to the right branch but no change the current branch. What do you do?
There is no git command to do this but you can use the git stash command to fix this.
This involves:
- Using the git stash command to temporarily store our changes elsewhere,
- Checkout the correct branch, and
- “Unstash” the changes to the correct branch.
Let’s say that we’ve made some changes in our repository’s new-release branch but they should have been made in the new-feature branch.
git stash
git checkout new-feature
git stash pop
We can now perform a commit, and update this branch.
git commit -a -m "Updated functions in new-feature branch."