Home > Blockchain >  How can I transfer a series of Git commits between two local copies of the repository?
How can I transfer a series of Git commits between two local copies of the repository?

Time:01-29

I currently have two copies of the same Git project checked out on my machine. I'm aware that traditionally one would not do that; it's more typical to have the project checked out once and switch branches as needed. The primary reason I have the second copy so that I can review merge requests locally — within my IDE, as well as by running the code — without having to context switch out of whatever work I might be in the middle of. That work might not be in a state where it's convenient for me to stash, or commit, or stop running a long running process.

I accidentally made some changes to the wrong copy of my project locally, and want to move them to the correct copy.

I know that I can create a branch, push them to the server, check them out on the other branch, and then delete the remote branch. This isn't too terribly onerous.

cd /path/to/source-project

git switch -c myTempBranch
git push -u origin myTempBranch

cd /path/to/other-project

git pull
git merge myTempBranch
git push -d origin myTempBranch

That being said, the changes aren't ready for anyone else's consumption quite yet, and it would be nice if I could do this all locally without having to get the remote server involved.

Is there a way to apply a series of commits from one checked out get project to a different copy of that project without pushing it?

CodePudding user response:

You can add a local repository as a remote and fetch it to get the changes. E.g.

git remote add repoWithChanges file:///<path_to_remote>

git fetch repoWithChanges

Now after the other repository's changes are fetched you can cherry-pick, merge, and so on.

PS: If you need a second worktree for the pull requests you can just use

 git worktree add /some/path/to/checkout someBranch

 
  •  Tags:  
  • Related