Home > OS >  How to checkout to another remote without merging
How to checkout to another remote without merging

Time:08-03

I don't know if it is duplicate, but I didn't find someone describing exactly what I want to do.

I have a local repository with an origin remote URL. I needed to make some more commits to push to another URL (which I'll call test_remote). I don't want those new commits from test_remote in the origin remote, and I want to keep working from the last commit of origin remote (Origin/master). How do I checkout my local repository to the origin without messing with both remote repositories? I know I could simply clone from origin and ignore the old local repository, but I'm curious if it's possible to just checkout to different remotes.

CodePudding user response:

If I understand correctly, you basically want 2 things:

  1. to push some of your commits only to one of the remotes
  2. to keep those changes only in the test_remote, while origin doesn't know about them at all.

Well, looks like it's a combination of actions. First, you have to use branches to keep your changes separate. And likely the single main branch to avoid conflicts when you need to merge. Second, you push each branch to the remote of your choice. Like so:

I want to fix a function from main and push it to origin. To do that, I do:

  • git switch main
  • git pull
  • git switch -c fix-func-origin
  • (fix the code)
  • git commit -am "my comment"
  • git push -u origin fix-func-origin

Fine, now I want to experiment with this function so that no one who has access to origin can see it. For that I do:

  • git switch main
  • git pull
  • git switch -c experiment-func-test
  • (fix the code)
  • git commit -am "my comment"
  • git push -u test_remote experiment-func-test

I wonder if it makes sense.

CodePudding user response:

Maybe you can use 'git stash' on your local repository.

You can read the documentation here : https://git-scm.com/docs/git-stash

  • Related