1

I have a set of unpushed commits in a local branch... and want to move them into another local branch created out of a new fresh clone. Is that possible?

torek
  • 448,244
  • 59
  • 642
  • 775
j3d
  • 9,492
  • 22
  • 88
  • 172
  • 1
    Set repo1 (original) as a new remote from repo2 (newly cloned), then pull from repo1. – Romain Valeri Oct 26 '22 at 14:28
  • Does this answer your question? [Move the most recent commit(s) to a new branch with Git](https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git) – Malice Oct 26 '22 at 15:00

1 Answers1

2

You need to set the first project as remote branche of the second then get your commits references by using git log and cherry pick merge them to the new branch

cd /home/projectB
git remote add projectA /home/you/projectA
git fetch projectA
git cherry-pick <first_commit>..<last_commit>
git remote remove projectA
storm
  • 379
  • 1
  • 9
  • 23
  • This doesn't work because the new branch is in another directory where I cloned the remote branch again. – j3d Oct 26 '22 at 14:31
  • Okay I have edited my answer, assuming projectA is the original clone – storm Oct 26 '22 at 14:43
  • 1
    To add on that, you can push and fetch(pull) between to local repositories which many people don't really know about. – Torge Rosendahl Oct 27 '22 at 00:53
  • Also, don't think of "clone" as a thing, but an action: when you clone a repository, the result is a repository that is just as much a "real" repository as the thing you cloned. – chepner Oct 27 '22 at 13:00