3

I currently have a repo, but I want to temporarily use another repo to push the changes to and then when I choose to, change back to the other repo and push to that one again (this is due to reasons of access to the main repo).

So I'm wondering, if I want to change the repo the pushes go to, is all I have to do is change the origin in my git config file, such as:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
    hideDotFiles = dotGitOnly
[remote "origin"]
    url = git@bitbucket.org:myname/my-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

Do I just change this line:

url = git@bitbucket.org:myname/my-repo.git

...to the new value and then back again to go back to the main repo? ...or is there something else to do and if so, what!?

Brett
  • 19,449
  • 54
  • 157
  • 290
  • 2
    I don't fully understand your use case, but personally I'd just set up another remote. You can `git push origin` or `git push whatever` to decide where you're pushing. (Note that you _never_ commit to a remote with Git. Commits always happen locally.) – ChrisGPT was on strike Mar 19 '19 at 18:32
  • @Chris Sorry, my bad. I wasn't thinking, meant pushes go to, not commits. I have updated my question. – Brett Mar 19 '19 at 20:08

2 Answers2

5

you can push and pull directly to/from a remote repository:

git push git@bitbucket.org:myname/my-other-repo.git HEAD:refs/heads/foo
git pull git@bitbucket.org:myname/my-other-repo.git refs/heads/foo
ensc
  • 6,704
  • 14
  • 22
4

As a distributed version control system, Git allows you to manage multiple remote repositories. If it's just temporary, don't touch your origin - just add a separate remote. And don't bother with editing config files, use the command line:

git remote add temp git@bitbucket.org:myname/my-other-repo.git

Push to a new remote using:

git push temp

After access issues are solved synchronize origin:

git push origin

Read more about git remote here: https://git-scm.com/docs/git-remote.

Kombajn zbożowy
  • 8,755
  • 3
  • 28
  • 60
  • Just tried this and when I did `git push -u temp master` it changed `remote = origin` to `remote = temp` under `[branch "master"]` in the config file. Is that ok? – Brett Mar 19 '19 at 20:24
  • 1
    Yes, it's just a default remote to use if you don't specify it explicitly for git push/pull/fetch. Check [here](https://stackoverflow.com/questions/43317872/understanding-git-configs-remote-and-branch-sections) for detailed explanation of what various options in `branch` and `remote` sections mean. – Kombajn zbożowy Mar 19 '19 at 21:40
  • Great, and thanks for the link, I'm sure it will be helpful! :) – Brett Mar 19 '19 at 22:13