30

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.

Community
  • 1
  • 1
Jorjon
  • 5,316
  • 1
  • 41
  • 58
  • 3
    If by "without all this hassle" you mean "without checking out `hotfix` and later `feature-x` again", then this question is a duplicate of [Commit a file to a Different Branch Without Checkout](http://stackoverflow.com/questions/7933044/commit-a-file-to-a-different-branch-without-checkout). (You'll find some interesting answers, there, too. Whether any satisfactory ones, I dunno.) – das-g Feb 26 '17 at 10:17
  • Possible duplicate of [Commit a file to a Different Branch Without Checkout](http://stackoverflow.com/questions/7933044/commit-a-file-to-a-different-branch-without-checkout) – das-g Feb 26 '17 at 10:17
  • I apologize for the vague language. I was more referring to less commands (without involving an alias). – Jorjon Feb 26 '17 at 10:43
  • 2
    From a practical point of view, the way to do this in modern (post-2.6) Git is with `git worktree add`, as I noted on the possible-duplicate question @das-g linked. The cherry-pick method Arpit describes will work but being able to open a separate Terminal or IDE window, move to a nominally-separate (but same underlying repository) work-tree, and work there, is convenient and cheap (takes only one checkout's worth of extra space). You can keep the hotfix work-tree around as long (or short) as you like. – torek Feb 26 '17 at 10:45

2 Answers2

14

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
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
13

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
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108