I'm on branch feature-x
doing some work, when I suddenly encounter an issue which should really be solved on another branch, hotfix
.
I want to create a commit with that change, but on the hotfix
branch.
From this question I understand that the standard procedure would be to
# On branch feature-x
git stash
git checkout hotfix
git stash pop
git add -A
git commit -m "Fixed issue #562"
That would work if I didn't have many changes on branch feature-x
already underway, which would throw a conflict with the branch hotfix
. I want to avoid having to resolve unnecessary conflicts.
To avoid that, I was thinking that I could only extract a single file from the stash, as noted on this answer. So the procedure would be:
# On branch feature-x
git stash
git checkout hotfix
git checkout stash@{0} -- <src/buggyfile.xxx>
git add -A
git commit -m "Fixed issue #562"
And then I should go back to feature-x
git checkout feature-x
git stash pop
While there's a way to bring files from another branch directly, I want to know if there's a way to send files to another branch, without all this hassle. The actual modification is only a couple characters.
To commit a file from feature-x
to hotfix
, there is an easy way to do that (assume the commit you just added on feature-x
branch is 4712df727f7303bc95545d0f6a024129be97c5c2
):
# on branch hotfix
git checkout 4712d filename
git commit
You can commit the change in feature-x
and cherry-pick it in hotfix
, as follows:
# On branch feature-x
git add <file>
git commit -m "Fixed issue #562" // Note commit-id
git checkout hotfix
git cherry-pick <commit-id>
git push origin hotfix
Extending the answer as per @torek comment, to use git-worktree, as follows:
git worktree add -b hotfix ../hotfix origin/master
cd ../hotfix/
git add <file>
git commit -m "Fixed issue #562"
git push origin hotfix
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With