tags:

views:

65

answers:

2

I've started playing with git locally, and have come up against a use case that I'm not sure how best to handle.

I'm working on a spike in a branch, but then come across a simple correction that should also apply to master. The best I've found for the moment is:

git stash
git checkout master
//do my correction manually
git commit -a
git checkout spike
git pop

Seems a bit long winded, just for one line, and it involves me doing the same correction twice. I can't help feeling there must be a better way of doing this. In your answer, please also consider the case where there are other changes in the same file which I don't want to take.

+1  A: 

If the change that you want to apply to master consists of one complete commit on the spike branch, then the cherry-pick command was made for this situation.

git stash
git checkout master
git cherry-pick <hash>
git checkout spike
git stash pop

If you just need part of the commit, then use:

git stash
git checkout master
git cherry-pick -n <hash>
# tinker with the index until it contains the changes that you want to apply to master
git commit
git checkout spike
git stash pop
Daniel Schilling
Just tried it, thanks. Is there a way to import just relevant lines from within a file?
Benjol
# after cherry-pick -n git reset HEAD <file> # to unstage the change git add -p # to select the lines that you want to commit git commit
Daniel Schilling
The comment all ran together. Let's try that again. After "git cherry-pick -n": 1) "git reset HEAD <file>" to unstage the change. 2) "git add -p" to select the lines that you want to commit. 3) "git commit".
Daniel Schilling
+1  A: 
Jakub Narębski