1

Normally I can move pushed commits to another repository using cherry-pick: I fetch the related branch from 'another' repository to my new repo and cherry-pick with commit's ID.

I want to do the same thing for local--unpushed--commits in a corrupt repository (something went wrong and I cannot push my changes, but I need to move them to another repo).

Is there any way to cherry-pick commits from the corrupt repo to another repo?

PS: Both repositories exist on the same machine.

Guildencrantz
  • 1,875
  • 1
  • 16
  • 30
likeachamp
  • 765
  • 4
  • 11
  • 21

3 Answers3

4

Do you just need to push commits from one local repo to another local repo?

git remote add local /path/to/repo.git
git push local master
Guildencrantz
  • 1,875
  • 1
  • 16
  • 30
jaredready
  • 2,398
  • 4
  • 23
  • 50
3

Remotes aren't limited to another machine: If you have one repository at /repo1 and another at /repo2 you can copy commits from repo1 to repo2 by simply adding repo1 as a remote to repo2 using filesystem paths:

cd /repo2
git remote add repo1 /repo1

You can now fetch, pull, and cherry-pick commits from repo1 to repo2.

Guildencrantz
  • 1,875
  • 1
  • 16
  • 30
  • is there anyway to cherry-pick without adding it? one file under .git/objects is corrupted so i cannot add as a remote or push my changes etc. – likeachamp Mar 01 '17 at 17:39
  • 1
    No, you can't do a `cherry-pick` if you can't add the repo as a remote: Your best bet is to try @ElpieKay's answer to export a patch (effectively a manual `cherry-pick`). – Guildencrantz Mar 01 '17 at 17:46
3

Another possible method.

cd repo1_path
git format-patch -1 <commit> --stdout > /tmp/xxx.patch
cd repo2_path
git checkout <branch>
git am /tmp/xxx.patch
ElpieKay
  • 27,194
  • 6
  • 32
  • 53