Move one file to another branch

I'm on the feature-x branch doing some work when I suddenly encounter a problem that really needs to be resolved on another branch, hotfix .

I want to create a commit with this change, but in the hotfix branches.

From this question I understand that the standard procedure will be

 # On branch feature-x git stash git checkout hotfix git stash pop git add -A git commit -m "Fixed issue #562" 

This would work if there weren’t already a lot of changes in the feature-x branch, which would cause a conflict with the hotfix branch. I want to avoid the need to resolve unnecessary conflicts.

To avoid this, I thought that I can only extract one file from stash, as indicated in this answer . Thus, the procedure will be as follows:

 # 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 have to go back to feature-x

 git checkout feature-x git stash pop 

While there is a way to directly output files from another branch, I want to know if there is a way to send files to another branch, without any of this hassle. The actual modification is just a couple of characters.

+6
source share
2 answers

You can commit changes to feature-x and cherry-pick in hotfix , as shown below:

 # 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 

Response extension according to @torek comment to use git-worktree as below:

 git worktree add -b hotfix ../hotfix origin/master cd ../hotfix/ git add <file> git commit -m "Fixed issue #562" git push origin hotfix 
+4
source

To commit a file from feature-x to hotfix , there is an easy way to do this (suppose the commit you just added on the feature-x branch has the value 4712df727f7303bc95545d0f6a024129be97c5c2 ):

 # on branch hotfix git checkout 4712d filename git commit 
+2
source

Source: https://habr.com/ru/post/1015291/


All Articles