Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying git commits to working tree unadded?

Tags:

git

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?

like image 650
Andrew Tomazos Avatar asked Jan 09 '13 22:01

Andrew Tomazos


People also ask

Can I apply a commit to another branch?

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.

Which helps in showing changes between commits and working tree?

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.

How do you push changes from detached head to branch?

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.

How do I commit a current 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.


1 Answers

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

like image 198
GabeSullice Avatar answered Oct 08 '22 19:10

GabeSullice