The answer, as given, is to use format-patch but since the question was how to cherry-pick from another folder, here is a piece of code to do just that:
$ git --git-dir=../<some_other_repo>/.git \
format-patch -k -1 --stdout <commit SHA> | \
git am -3 -k
Explanation from Cong Ma comment Aug 28 '14
Answer from Robert Wahler on Stack Overflowgit format-patch command creates a patch from some_other_repo's commit specified by its SHA (-1 for one single commit alone). This patch is piped to git am, which applies the patch locally (-3 means trying the three-way merge if the patch fails to apply cleanly).
The answer, as given, is to use format-patch but since the question was how to cherry-pick from another folder, here is a piece of code to do just that:
$ git --git-dir=../<some_other_repo>/.git \
format-patch -k -1 --stdout <commit SHA> | \
git am -3 -k
Explanation from Cong Ma comment Aug 28 '14
git format-patch command creates a patch from some_other_repo's commit specified by its SHA (-1 for one single commit alone). This patch is piped to git am, which applies the patch locally (-3 means trying the three-way merge if the patch fails to apply cleanly).
You'll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.
Like that:
git remote add other https://example.link/repository.git
git fetch other
Now you have all the information to simply do git cherry-pick.
When done, you may want to remove the remote again, if you don't need it any more, with
git remote remove other
More info about working with remotes here: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
?use cherry-pick to copy a file to another repo, preserving commit history?
Can I cherry pick from a repository/project with a different folder structure?
Our team has been forced into using git cherry-pick in order to promote changes to other environments, and it's not going well, any alternatives?
How to copy commits from one branch to another?
Don't copy anything. There are a few ways to achieve this:
-
Rebase
experimentontodev. This will "move"experimentso that it "starts" wheredevcurrently is; that is to say, it will put the entiredevbranch in the history ofexperiment. Then you can test, and if everything works ok you can mergeexperimentintodev. -
Merge
devintoexperiment. Test. When everything is ok, mergeexperimentintodev.
EDIT: Ignore this question, and move there, where I rephrased the problem in better way -> https://www.reddit.com/r/git/comments/1bq5mv6/how_to_move_file_with_commit_history_to_another/
PROBLEM X: move file to another repo preserving its commit history
I did it once successfully within the repo and into another repo, but git mv doesn't work when I'm trying to move to another repo.
PROBLEM Y: solve merging conflict in cherry-picking method (guess it's a wrong method to solve X)
I want to move a file from one repo to another without losing its commit history. I tried git mv and got sth like: no, because destination path is outside of the repository. Chat GPT told me to use cherry-pick.
-
git remote add source <source_repository_url>
-
git fetch source
-
git cherry-pick <commit_hash> Cherry-pick the File: Cherry-pick the commit that introduced the file you want to move from the source repository.
-
Remove the File from the Source Repository (Optional): If you want to remove the file from the source repository after moving it, you can push a deletion commit. git push source :<branch_name>
-
In another response, there just 1., 2. and the 3. step is: git checkout source/main -- path/to/file
There is some conflict. What was fetched cannot be merged due to this conflict. But I don't know where it is, because not between the state of file and the commit hash - it's the latest change and in both repos everything is up to date. If it's a conflict between the sourced repo and the destination repo it's correct, because I don't wanna clone. I want to move or copy just one file with its commit history.
I didn't use cherry-pick before and I will now start reading about it, but I predict the battle is lost.