Is it possible to cherry-pick a commit from another git repository?
GitCherry PickGit Problem Overview
I'm working with a git repository that needs a commit from another git repository that knows nothing of the first.
Typically I would cherry-pick using the HEAD@{x}
in the reflog, but because this .git
knows nothing of this reflog entry (different physical directory), how can I cherry-pick this, or can I?
I'm using git-svn
. My first branch is using git-svn
of the trunk
of a Subversion repo, and the next branch is using git-svn
on a Subversion branch.
Git Solutions
Solution 1 - Git
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).
Solution 2 - Git
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
Solution 3 - Git
Here's an example of the remote-fetch-merge.
cd /home/you/projectA
git remote add projectB /home/you/projectB
git fetch projectB
Then you can:
git cherry-pick <first_commit>..<last_commit>
or you could even merge the whole branch(only if you really need to merge everything)
git merge projectB/master
Solution 4 - Git
You can do it, but it requires two steps. Here's how:
git fetch <remote-git-url> <branch> && git cherry-pick FETCH_HEAD
Replace <remote-git-url>
with the url or path to the repository you want cherry-pick from.
Replace <branch>
with the branch or tag name you want to cherry-pick from the remote repository.
You can replace FETCH_HEAD
with a git SHA from the branch.
Updated: modified based on @pkalinow's feedback.
Solution 5 - Git
Here are the steps to add a remote, fetch branches, and cherry-pick a commit
# Cloning our fork
$ git clone [email protected]:ifad/rest-client.git
# Adding (as "endel") the repo from we want to cherry-pick
$ git remote add endel git://github.com/endel/rest-client.git
# Fetch their branches
$ git fetch endel
# List their commits
$ git log endel/master
# Cherry-pick the commit we need
$ git cherry-pick 97fedac
Source: https://coderwall.com/p/sgpksw
Solution 6 - Git
See How to create and apply a patch with Git. (From the wording of your question, I assumed that this other repository is for an entirely different codebase. If it's a repository for the same code base, you should add it as a remote as suggested by @CharlesB. Even if it is for another code base, I guess you could still add it as a remote, but you might not want to get the entire branch into your repository...)
Solution 7 - Git
You can do it in one line as following. Hope you are in git repository which need the cherry-picked change and you have checked out to correct branch.
git fetch ssh://[email protected]:7999/repo_to_get_it_from.git branchToPickFrom && git cherry-pick 02a197e9533
#
git fetch [branch URL] [Branch to cherry-pick from] && git cherry-pick [commit ID]
Solution 8 - Git
Yes. Fetch the repository and then cherry-pick from the remote branch.
Solution 9 - Git
Assuming A
is the repo you want to cherry-pick from, and B
is the one you want to cherry-pick to, you can do this by adding </path/to/repo/A/>/.git/objects
to </path/to/repo/B>/.git/objects/info/alternates
. Create this alternates
files if it does not exist.
This will make repo B access all git objects from repo A, and will make cherry-pick work for you.
Solution 10 - Git
For the case, that the commit has to be "cherry-picked" for some localized validations. It could also be, for same repository checkout, but with local commits (i.e. not pushed yet to server).
E.g.
- repo1
similar
to repo2 - repo1 has branch
b1
- HEAD progressed by 2 commits (includingcommit_x
) locally. - repo2 has branch
bb1
- requiredto cherry-pick
commit_x.
For this,
> $ cd repo2
> repo2 $ git fetch
> repo2 $ git cherry-pick
In the above case, the commit_x
will now be identifiable and picked up (by the aid of fetch
).
Solution 11 - Git
My situation was that I have a bare repo that the team pushes to, and a clone of that sitting right next to it. This set of lines in a Makefile work correctly for me:
git reset --hard
git remote update --prune
git pull --rebase --all
git cherry-pick -n remotes/origin/$(BRANCH)
By keeping the master of the bare repo up to date, we are able to cherry-pick a proposed change published to the bare repo. We also have a (more complicated) way to cherry-pick multiple braches for consolidated review and testing.
If "knows nothing" means "can't be used as a remote", then this doesn't help, but this SO question came up as I was googling around to come up with this workflow so I thought I'd contribute back.
Solution 12 - Git
If you want to cherry-pick multiple commits for a given file until you reach a given commit, then use the following.
# Directory from which to cherry-pick
GIT_DIR=...
# Pick changes only for this file
FILE_PATH=...
# Apply changes from this commit
FIST_COMMIT=master
# Apply changes until you reach this commit
LAST_COMMIT=...
for sha in $(git --git-dir=$GIT_DIR log --reverse --topo-order --format=%H $LAST_COMMIT_SHA..master -- $FILE_PATH ) ; do
git --git-dir=$GIT_DIR format-patch -k -1 --stdout $sha -- $FILE_PATH |
git am -3 -k
done