How to maintain a project in 2 repositories?

Asked

Viewed 1,730 times

17

I have a project and wanted to keep it in Bitbucket/Gitlab and Github at the same time. This is possible?

I need them to work simultaneously so that if I want to do one push only in Bitbucket can I.

  • 6

    On your machine you will have your codes and your local repository. Just add as many remote you need. In my projects I always have a remote origin and a remote deploy. So when I want to update the repository I use git push origin master when I want to update the use site git push deploy master

4 answers

10

Within your project’s local repository, add external repositories using the command git remote

git remote add github https://github.com/usuario/repositorio

git remote add bitbucket https://bitbucket.com/usuario/repositorio

After an eventual commit, you can push by specifying which repository will receive the updates.

git push gibhub master

or

git push bitbucket master

Working with remote repository

9

You can configure how many remotes you need it, and when you push to a repository just choose which one remotes you want to push.

Ex.:

List configured remote repositories:

git remote -v
# origin    [email protected]:meus-projetos/projeto-joinha.git (fetch)
# origin    [email protected]:meus-projetos/projeto-joinha.git (push)

Add new remote repository:

git remote add deploy [email protected]:/srv/git/meu-repositorio.git

In the documentation of git remote add explains the syntax of the command:

git remote add nome-do-remote [email protected]:/caminho/do/repositorio

Check if the repository has been added

git remote -v
# deploy    [email protected]:/srv/git/meu-repositorio.git (fetch)
# deploy    [email protected]:/srv/git/meu-repositorio (push)
# origin    [email protected]:meus-projetos/projeto-joinha.git (fetch)
# origin    [email protected]:meus-projetos/projeto-joinha.git (push)

Looks OK, now just choose which remote and branch you will push or pull:

git push origin master
# ou
git push deploy master
  • Take my mind off something young, imagine that I already have a Github project running smoothly etc., how can I give a git remote add origin to a repository in Gitlab? Then I can keep giving git push origin github or git push origin github as I want ?

  • You just need to add the remote with the name you want... If you already have the project in Github and already have the remote set up for it (probably the origin) just add the Gitlab remote.

  • Create a blank repository in Gitlab and then click on "Clone", it will show something how that which are the paths to the remote repository. Then just do git remote add gitlab [email protected]:fernandosavio/teste.git that the repository will be configured

  • Young tendi, it’s good to know :)

  • 1

    Now I saw that it was you Hugo! : D After doing the steps above, just choose where to push. Just one important detail git push remote branch, so your first comment would be git push github master instead of git push origin github

  • Got it, I’m learning Git lightly, just the basics even to climb a Gitpage rss Other than the fact that the other day I gave an Add on my entire desktop, so far you’re good to understand things! [] s

  • How could I push into a specific branch and not the master?

Show 2 more comments

9


The other answers suggest creating a remote for each remote repository (e.g., one for Github, one for Bitbucket), and then do push for each of them.

It is a perfectly valid solution, but it is also possible, with only one push, update all remote repositories.


For starters, let’s assume I made one git clone url.github and the project is already on my machine. When a clone, by default it already creates a remote called origin, as we can see with the command git remote. Ex:

$ git remote -v
origin url.github (fetch)
origin url.github (push)

We can see that the URL to fetch and push are the same, indicating that these will be the Urls used for the respective commands.

But the git remote also has options which allow you to add several different Urls to push. If you want, you can do all this on your own origin, but as this is a remote kind of "standard" (which many use without thinking about when they do copy-Paste of commands to solve your problems, and any different configuration in it can mess up more than help), I’d rather create another remote separate for this (but nothing prevents you from doing the procedure below in origin).

So first let’s create a new remote, with the creative name of "all" (since he’ll be concentrating all the remote repositories):

# cria um remote chamado "all", apontando para a URL do GitHub
$ git remote add all url.github

# mostra os remotes atuais
$ git remote -v
all url.github (fetch)
all url.github (push)
origin url.github (fetch)
origin url.github (push)

As we can see, both the all as to the origin point to the same URL. But now let’s configure the all to have several Urls of push different.

For this, we use the command git remote set-url, using the options --add (to add a new URL) and --push (to indicate that I am adding a push), in addition to the name of remote (in the case, all) and finally the URL:

$ git remote set-url --add --push all url.bitbucket
$ git remote -v
all url.github (fetch)
all url.bitbucket (push)
origin url.github (fetch)
origin url.github (push)

Note that now the URL of push replaced by the Bitbucket URL. This means that if I make a push to the all, it will be sent only to Bitbucket:

$ git push all master 
Counting objects: 3, done.
Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To url.bitbucket
 * [new branch]      master -> master

It may seem strange that the --add overwritten the default URL, but this is the "expected" behavior for the first URL added, as described in this reply by Soen (see the comment in "UPDATE 2"). But okay, just add the Github URL again, with the same command:

$ git remote set-url --add --push all url.github

$ git remote -v
all url.github (fetch)
all url.bitbucket (push)
all url.github (push)
origin url.github (fetch)
origin url.github (push)

Notice that now the remote all has two Urls of push: one from Github and one from Bitbucket. This means that when a push, he will be sent to both at once:

# fazer push do branch master para o remote "all"
$ git push all master 
Counting objects: 3, done.
Writing objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To url.bitbucket
   3f5d6dc..78ac9a1  master -> master
Counting objects: 3, done.
Writing objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To url.github
   3f5d6dc..78ac9a1  master -> master

Notice that the push for both Bitbucket and Github.


This option allows you to add as many remote repositories as you need. For example, I could add a repository to Gitlab:

$ git remote set-url --add --push all url.gitlab

$ git remote -v
all url.github (fetch)
all url.bitbucket (push)
all url.github (push)
all url.gitlab (push)
origin url.github (fetch)
origin url.github (push)

See that there are now 3 Urls of push (Bitbucket, Github and Gitlab). By push:

$ git push all master
Everything up-to-date
Everything up-to-date
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 439 bytes | 439.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To url.gitlab
 * [new branch]      master -> master

Twice came the message "Everything up-to-date", indicating that two remote repositories were already updated, and finally, the push for Gitlab. If you want to know which repositories were already updated, you can use the option -v:

$ git push -v all master
Pushing to url.bitbucket
To url.bitbucket
 = [up to date]      master -> master
updating local tracking ref 'refs/remotes/all/master'
Everything up-to-date
Pushing to url.github
To url.github
 = [up to date]      master -> master
updating local tracking ref 'refs/remotes/all/master'
Everything up-to-date
Pushing to url.gitlab
Counting objects: 3, done.
Writing objects: 100% (3/3), 256 bytes | 256.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To url.gitlab
   78ac9a1..95f90db  master -> master
updating local tracking ref 'refs/remotes/all/master'

In this case, the URL that is being made is shown push, and the message "Everything up-to-date" is shown to those who are already updated.


If you want, you can do it all on origin, just need to remember that all push will be done in all remote repositories.

  • What if I have the same project on two github accounts with different usernames and email? I tried to follow your explanation that by the way is fantastic but that for different github accounts it didn’t work. Can you help me?

  • @Clebernandi I saw the your question on the subject, but I didn’t answer it because I really don’t know how to do it (I never had to use 2 different accounts and I have no idea what the best way would be)

  • Thank you, I continue researching and trying to find a better way.

0

For others who can still search.

I did it that way:

1 - After cloning or initializing a repository, you will create a folder called:

.git

2 - inside the . git folder will have a file called:

config

3 - Open the config file with notepad and inside it you will have it:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = https://[email protected]/seunome-seunome/repositorioQueCriei.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

4 - just add the url of a new repository inside the [remote "origin"] that would look like this:

[remote "origin"]
    url = https://[email protected]/seunome-seunome/repositorioQueCriei.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = https://github.com/seunome/repositorioqueCrieinogithub.git
    fetch = +refs/heads/*:refs/remotes/origin/*

I hope I’ve been helping.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.