Suppose I have a linear git history of 8 commits and one branch (master):
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> [8=master]
I want to move master to 4 (which I can do with git branch -f master 4
):
1 -> 2 -> 3 -> [4=master] -> 5 -> 6 -> 7 -> 8
Now the working tree is in state 4.
I now want to apply the changes from 4 -> 8
to my working tree as a patch.
That is, without effecting the state of the .git folder I want to apply the changes from 4->8
unstaged to my working tree. After this the working tree should be in state 8 but the committed state and master branch should be in state 4.
Another way to say it: Pretend after moving master to 4, I made the changes from 4->8 manually to my working tree without adding them to the index. The result should be the same.
What is the easiest way to do this?
You can cherry-pick a commit on one branch to create a copy of the commit with the same changes on another branch. If you commit changes to the wrong branch or want to make the same changes to another branch, you can cherry-pick the commit to apply the changes to another branch.
git diff --cached This is the difference between what is in the index and the last commit. It shows you that changes that will be in the next commit. git diff This shows the difference in between the index and the working tree.
If you want to keep changes made with a detached HEAD, just create a new branch and switch to it. You can create it right after arriving at a detached HEAD or after creating one or more commits. The result is the same. The only restriction is that you should do it before returning to your normal branch.
Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want. "Cherry pick" the commits you want into this branch. Run this command: git cherry-pick super-long-hash-here . That will pull just this commit into your current branch.
I know I'm late to the party, but for others reference, I think the simplest way is:
git cherry-pick --no-commit 4..8
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