1041

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.

0

14 Answers 14

1078
+200

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).

21
  • 37
    This is spot on, but it would be great if anyone could expand on this - a breakdown of exactly what's going on (especially with those flags) would be incredibly useful.
    – Nick F
    May 7, 2014 at 17:56
  • 66
    @NickF, the 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). Hope that explains.
    – Cong Ma
    Aug 28, 2014 at 9:37
  • 3
    error: patch failed: somefile.cs:85 error: somefile.cs: patch does not apply Did you hand edit your patch? It does not apply to blobs recorded in its index. Cannot fall back to three-way merge. Patch failed at 0001 Added GUI parts. The copy of the patch that failed is found in: <some_other_repo>/.git/rebase-apply/patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".
    – Tom
    Nov 6, 2015 at 18:36
  • 16
    @Tom try using --ignore-whitespace. Full command: git --git-dir=../<some_other_repo>/.git format-patch -k -1 --stdout <commit SHA> | git am -3 -k --ignore-whitespace Feb 3, 2016 at 10:49
  • 10
    @BoomShadow Because it's way simpler. Adding the remote and fetching brings in all of the other repo's changes. This command line is a one-time action. Jan 4, 2017 at 15:05
951

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

15
  • 1
    What if I'm using git-svn? my first branch is using git-svn of the trunk and the next is using the git-svn on a branch (thanks for the quick reply) Feb 25, 2011 at 16:54
  • 51
    If you're using Github, you can pull the patch by appending .patch to the commit URL, and then applying it with git am < d821j8djd2dj812.patch. Outside of GH, similar concepts could be done as referenced in the alternative answer below.
    – radicand
    May 12, 2012 at 19:29
  • 2
    @radicand which answer below is the "alternative" one? Please link to it.
    – user456814
    Jul 13, 2013 at 21:52
  • 7
    Detailed steps to cherry pick from another repo: coderwall.com/p/sgpksw/git-cherry-pick-from-another-repository Jul 19, 2016 at 13:29
  • 3
    You don't really need to add the remote. fetching is enough. And ../<some_other_repo>/ is a perfectly fine uri, if you have the repo on your disk already.
    – geon
    Feb 7, 2018 at 14:44
204

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
8
  • 74
    git merge projectB/master is very, very wrong, because you're not applying the changes from a single commit (like a cherry-pick would), you're actually merging in all changes in projectB/master that aren't contained in your own master branch.
    – user456814
    Jul 13, 2013 at 23:12
  • 6
    It was my assumption that this was the original poster's intent. Otherwise, yes, this is not the right option for them.
    – Brian
    Jul 17, 2013 at 14:32
  • 8
    This works brilliantly when the two repositories are related. Nov 25, 2014 at 10:47
  • 3
    I have created a copy from a git repository (just for "playing around" without breaking the original repo) and to keep it up-to-date with its source, the answer from Brian is exactly what I needed, so, Cupcake, I have to say, it's not "wrong", but another use case. But it is nice from you to point out the potential disaster :D
    – ferrari2k
    Oct 21, 2015 at 9:19
  • 11
    IMO this needs to be the accepted solution. In addition, if you want to delete the remote after you're done cherry-picking from it, use git remote rm projectB. Also use git tag -d tag-name to remove any tags fetched from the remote repo. The remote commits will no longer show in your history, and pruning will remove them from storage eventually.
    – ADTC
    Jul 15, 2016 at 7:44
194
+100

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.

8
  • 13
    It works with a branch name, but not with SHA. If you want to cherry-pick a commit denoted by its hash, use this instead: git fetch <repo-url> <branch> && git cherry-pick <sha>.
    – pkalinow
    Jan 17, 2014 at 16:38
  • Thanks. This was just what I needed to insert a number of commits from one repository to another, which I created for this purpose.
    – wojciii
    Aug 19, 2014 at 12:16
  • This was exactly what I needed with many custom implementation of our code for different clients (who each have their own repositories/forks) we needed a way to get specific commits into our base/trunk. THANKS!
    – RedSands
    Oct 15, 2016 at 18:57
  • 2
    This should be the accepted answer for a one-shot cherry pick across repos. I use it all the time when picking between repos that are already local, the remote URL is then just a local filesystem path. Apr 29, 2019 at 11:51
  • If you are mostly familiar with cherry-pick this is probably the simplest. You don't need to add the full remote (although you can, if you are going to do this a lot). Just fetch the branch you need to get a local reference (can be from another local directory, or remote URL), and then cherry-pick the commits. Dec 1, 2021 at 21:29
68

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

0
18

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...)

13

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]

0
5

Yes. Fetch the repository and then cherry-pick from the remote branch.

5

If the other repo is present on the same machine, you could achieve a similar effect as cherry-pick by applying a patch and then committing the original message. Here is an example:

$ git apply <(git -C "$PATH_TO_OTHER_REPO" show "$COMMIT_HASH")
$ MSG="$(git -C "$PATH_TO_OTHER_REPO" log -n 1 --pretty=format:'%s' "$COMMIT_HASH")"
$ git commit -m "$MSG"

I don't have to do it very often so I'm fine with this workflow. However, it should be fairly easy to compose a customized Git command for this and have it nicer and more automated.

Notice that the command inside <(...) can be anything that generates a valid patch for Git: git show, git diff, using wget or curl to fetch raw diff contents from a remote such as Github (so you can skip cloning), cat from a file... That line, by itself, is extremely useful.

4

Here is one easy to type out from memory, inspired by the comment of @radicand. It is dependent on the capabilities of the forge, but Github, Gitlab and Gitea definitely support it.

You append .patch to the commit URL and apply it via git am:

curl --location URL.patch | git am

--location makes it follow redirects, which can happen when e.g. copying a patch from a pull request

2

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.

2

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 (including commit_x) locally.
  • repo2 has branch bb1 - required to cherry-pick commit_x.

For this,

$ cd repo2

repo2 $ git fetch <path_to_repo1>

repo2 $ git cherry-pick <commit_x>

In the above case, the commit_x will now be identifiable and picked up (by the aid of fetch).

0

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.

1
  • -n means no-commit according to git docs and I thing very important to see the changes before making a commit
    – canbax
    Jul 12, 2019 at 11:30
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.