1

I'm tired of switching between branches. So, I cloned the repo twice on my PC.

Now, I have two different folders of the same repo.

I want to copy a specific local commit that I have on the older clone to the newer clone. Which set of git commands can help me to do it? (cherry-pick maybe?)

TonyArra
  • 10,607
  • 1
  • 30
  • 46
  • 4
    Does this answer your question? [What would I use git-worktree for?](https://stackoverflow.com/questions/31935776/what-would-i-use-git-worktree-for) Or [How can I have multiple working directories with Git?](https://stackoverflow.com/questions/6270193/how-can-i-have-multiple-working-directories-with-git) – mkrieger1 May 03 '22 at 13:15
  • 1
    Its a possible solution but implementing it will require from me extra work to do. Lets say I have branch master and feature over the same clone. And I say-God dammit, I want each one of the branches in a seperate clone. So I have now 3 clones of the same repo: 1 of the mixed master/feature branches 1 of master only 1 of feature only What I want is to copy the last commit of either of the branches from the mixed clone to these newly clones. And my meaning is the locally last commits, not remoted ones! Uploading them to a remote repo is also a challenge – El Pollo Loco May 03 '22 at 13:53
  • Yes, that's what you would use worktrees for. Instead of making multiple clones. I'm not sure what you mean by "implementing it will require extra work" - it's already implemented by Git, you can just use it. – mkrieger1 May 03 '22 at 14:09
  • I already cloned twice and did a long build for each one of them. Is there a way to "integrate" The 2 clones into the same worktree? – El Pollo Loco May 03 '22 at 14:10

1 Answers1

1

Long term, what you want is to use git worktrees.

In order to repair your current situation, you can cherry-pick between repos by creating remotes from your local directories.

By adding another local repo as a remote and doing a git fetch, you can cherry-pick any commit you want from that repo with just the SHA.

For example, if you want to cherry-pick the commit from old_clone with a SHA starting with e9bee1bc:

cd /projects/new_clone
git remote add old_clone /projects/old_clone
git fetch old_clone
git cherry-pick e9bee1bc

You could also use a similar strategy to convert your multiple cloned remotes into worktrees. For example, if you want to create a worktree from your original project and add a local copy of branch1 from new_clone to your new worktree:

cd /projects/original
git worktree add /projects/worktree_1
cd /projects/worktree_1
git remote add new_clone /projects/new_clone
git fetch new_clone
git switch -c branch1 new_clone/branch1
TonyArra
  • 10,607
  • 1
  • 30
  • 46
  • 1
    Thanks for the help! I utilized you approach and on the way converted myself to worktree! When I'll have more reputation I'll upvote your solution :) – El Pollo Loco May 04 '22 at 12:49