Adding a remote repository in Git?
In this tutorial, I will explain how to add a new remote repository, a branch, and file in the remote repository from the terminal (Git Bash).
Adding the remote repo
If you are working on Git terminal then you may check the current repo (if any is added/set) by using this command:
$ git remote –v
This should display the URL of the current remote repository as shown below for our test purpose:
If you require adding a new repository then use the git remote add command as follows:
$ git remote add origin https://github.com/user_name/remote_repo.git
For example, this command adds our test remote repository (bootstrap.git):
$ git remote add origin https://github.com/git-test-jaz/bootstrap.git
If Git terminal returns a fatal error message like this:
fatal: remote origin already exists.
You may use some other name than the origin. For example:
$ git remote add my_bootstrap https://github.com/git-test-jaz/bootstrap.git
For testing, if the remote repo is added or not, run the remote –v command again i.e.
$ git remote –v
The result with our test:
You can see the remote repo with the name of my_bootstrap is added.
How to add a branch while having multiple repositories?
In this section, I will show you how to add a branch in a remote repository while having multiple branches.
For that, first I added another repository by using this command:
$ git remote add my_test https://github.com/git-test-jaz/hello-git.git
So, we have now three repositories as shown below:
1 2 3 4 5 6 7 8 9 10 11 |
my_bootstrap https://github.com/git-test-jaz/bootstrap.git (fetch) my_bootstrap https://github.com/git-test-jaz/bootstrap.git (push) my_test https://github.com/git-test-jaz/hello-git.git (fetch) my_test https://github.com/git-test-jaz/hello-git.git (push) origin https://github.com/git-test-jaz/tst-pull-2.git (fetch) origin https://github.com/git-test-jaz/tst-pull-2.git (push) |
Our target is to create a new branch, adding a file in the my_test repository. The branch name is “tst_multiple_br” and I will add a text file (tst1.txt).
First, creating the branch locally:
$ git branch tst_multiple_br
This is followed by checking out this branch:
$ git checkout tst_multiple_br
Now add the text file in that branch locally:
$ git add tst1.txt
Committing the file added:
$ git commit -m “File added for multiple repo test”
And finally pushing this branch and file in our target remote repository i.e. my_test
$ git push my_test tst_multiple_br
All these commands in the terminal with output:
This is the online view of the remote repository after adding the branch/file: