How to git remote add
How to git remote add
Follow These Steps To Add a New Remote To Your Git Repo
December 16, 2020
Stay connected
Git remote is an important part of Git, a tool that provides an easy-to-use system for tracking changes in source code during software development. With Git, you can save the state of your code at regular intervals (determined by you). Git is available for Windows, Mac OS, and Linux.
With Git remote, you can share your code to a remote repository. The repository could be private, public, or on some server you control. Git remote makes it easy for developers to collaborate.
In this post, you will learn how to set up remotes for your local Git repo in three steps. We will start by adding Git to a new or existing project and conclude by sharing the project to a Git hosting service like Bitbucket and GitHub.
In the first two steps in this post, we will set up some of the basic things necessary before we can add a new remote to a Git repo. The first requirement is the local Git repo that we’ll be adding a new remote to. The second requirement is a remote repo.
This post assumes that you already have Git installed on your local machine.
Step 1: Initializing Git in Your Project
We will need a local Git repo to which we will be adding the new remote. If you already have a local Git repo and just want to learn how to add a new remote to it, you can skip this step and jump to step 2. Otherwise, follow the instructions below to get started.
Open a new command prompt or terminal window and change the working directory to the root directory of your project. For example, if your project is stored in «/Users/you/document/hello-world,» you will run the following command to change the working directory:
Change «/Users/you/document/hello-world» to the current directory for your project. Next, run the following command to initialize Git:
The above command should create a local Git repo for your project if executed successfully.
A few further actions. Run the following commands to add your project files to the Git repo and make your first commit:
The first command adds all your project files to Git, while the second command creates a commit.
At this point, you have initialized Git in your project and made your first commit. Commits are like save points in a video game. Git does not auto-save changes to your project. Instead, changes are saved in commits.
Step 2: Creating a Remote Repo
In the previous step, we created a new local Git repo. Now let’s create a remote repo and retrieve the Git URL for the remote repo. This step is important if you do not already have a remote that you wish to add to your repo.
The process for setting up a new repo in Bitbucket and GitHub is described below.
2.1: Creating a New Repo on Bitbucket
Log in to Bitbucket, go to the repository section in the user dashboard, and click on the Create repository link.
When you are done, click the Create repository button to finish the process. Once your empty remote repo is created, Bitbucket should greet you with a page that shows the remote URL for the new repo.
Bitbucket provides a remote URL that looks like the following link: https://username@bitbucket.org/username/your-repo.git.
2.2: Creating a New Repo on GitHub
Log in to GitHub and click on the + icon in the top navigation bar, then click on New repository.
On the Create new repository page, enter a repository name of your choice. Leave all other options unchanged and click on the green Create repository button at the bottom of the page.
An empty GitHub repo will be set up. Just like Bitbucket, you should also be greeted with a page that shows a remote URL for the new repo. The remote URL for a GitHub repo looks like this: https://github.com/username/your-repo.git.
Please make sure to note the remote URL (GitHub and Bitbucket) for your repo. You will be required to enter the remote URL in the next step.
Step 3: Adding Remote URL To a Local Repo
At this point, we have a local Git repo, a remote Git repo, and the URL for our remote. Now let’s add the remote URL to our local repo.
Go to the command prompt or terminal, and from the root directory of your project, run the following command (replace your-remote-url with the valid URL for your repo):
git remote add origin your-remote-url
The command above should add a new remote to your local repo. The word «origin» in the command above serves as a shortname for the new remote. Git stores remote URLs in shortnames.
To verify that everything ran successfully, run this command:
You should get an output that looks like this:
origin https://github.com/username/your-repo.git (fetch)
origin https://github.com/username/your-repo.git (push)
You can add more remotes to the same Git repo by specifying a different shortname for the new remote. For example, to add a remote from Bitbucket to your repo, run the following command:
git remote add second your-remote-url
This time, replace your-remote-url with the Bitbucket remote URL.
origin https://username@bitbucket.org/username/good-year.git (fetch)
origin https://username@bitbucket.org/username/good-year.git (push)
second https://github.com/username/good-year.git (fetch)
second https://github.com/username/good-year.git (push)
Syncing Local and Remote Repos
In the previous step, we added a new remote to our local repo. Because of that, we can perform actions like pushing local changes to remote and pulling changes in remote to the local repo. With the git push and git pull features, we can keep our local and remote repos in sync. Let’s take a look at how that can be done.
Push Changes
At this point, our remote repo is still empty, so let’s push files and code to it from the local repo.
Open the command prompt or terminal and run the following command from the project directory:
The above command should update the state of your remote repo to the same state as the local repo.
Pull Changes
Let’s assume you are not the only person working on your project or that the state of your remote repo has changed since the last time you pushed to it. To sync your local repo with the new state of the remote repo, run the following command:
After running the command, your local repo should be up to date with the latest state of the remote repo.
Conclusion
We have covered how to create a new Git repo on a local machine. We have also walked through the process of creating remote Git repos on two popular Git hosting services (GitHub and Bitbucket). Our goal was to add a new remote to our local Git repo, and we did that by adding remotes hosted on GitHub and Bitbucket.
There are more ways that you can host your remote Git repos outside of the two Git hosting services mentioned in this post. The process is similar once you are able to get the remote URL. All that will be left to do after getting the URL is to run the following command in the command prompt or terminal:
Replace and with the correct values for your repo, and you’re all set!
For more posts in our Git series, visit :
Setting Up Your Remote Repository With Git
3 Minutes, 36 Seconds to Read
You can use Git locally to manage file versions, but more power comes when you distribute your work and allows other to collaborate. You can accomplish this by “pushing” and “pulling” content to and from a remote server locations. In this example, of course, we will be using an InMotion Hosting server to demonstrate setting up your remote repository with Git.
As stated before in the Introduction to Git, there are free Git platforms available on the web, but you are forced to use their resources and follow their rules. For some users, managing a private server location provides more attractive options as far as customization goes.
Be sure to check out our full guide on how to install Git, if you have not completed that step yet.
Using Git to Publish Files
Using Git as a publishing vehicle can allow for easy file transfer and also allow others to use your project files. This can be very helpful if you want to host files that others will need to have access to, and thus require the latest version with most recent changes applied. With Git, you can manage the files locally, commit changes, and upload the changes without ever leaving a single command line.
In this case, we will be using the Git “Push” command. This exact use case is detailed completely in our full guide on how to publish files with Git.
How to Add a Remote Repository to your Server Using Git
The most important step for distributing your Git project is to establish a remote server location. We can refer to this as the “remote” repository as opposed to your “local” repository, which we have been using thus far.
To use a remote repository, you must only make sure that you have SSH access to your server. Once there, you can follow these steps to create your remote repository.
“master” being the name of the branch (we will discuss branches later, but, for now, using the master branch exclusively is fine)
How to Pull Recent Files from your Remote Repository
Our full guide on how to publish files with Git uses a “checkout” command to copy all of the files associated with a project to a convenient location.
Let’s suppose that you eventually have a collaborator working on a project with you. They will want to always make sure they have the most recent iteration of the project files.
To accomplish this, we will use a Git “clone” command to grab the project from the remote repository and, once cloned, use a “pull” command to make sure we have incorporated all recent changes.
Using the example above, there will now be a directory called “production” in this location. This is effectively a local Git repository.
Supposing that changes are made to this project in the future, in order for the user to make sure they have the most recent updates, they will need to run a “pull” command:
We now know how to set up a Git repository at a remote server location, how to “push” information to the server, how to clone a repository, and how to “pull” information from the server. We covered a lot here, but you should be getting a solid foundation in Git by now. Next, we will cover “tagging” various commits for record-keeping purposes.
Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.
How to git remote add
Check your version of git by running
SYNOPSIS
DESCRIPTION
Manage the set of repositories («remotes») whose branches you track.
OPTIONS
Be a little more verbose and show remote url after name. NOTE: This must be placed between remote and subcommand.
COMMANDS
With no arguments, shows a list of existing remotes. Several subcommands are available to perform operations on the remotes.
By default, only tags on fetched branches are imported (see git-fetch[1]).
Changes the list of branches tracked by the named remote. This can be used to track a subset of the available remote branches after the initial setup for a remote.
Retrieves the URLs for a remote. Configurations for insteadOf and pushInsteadOf are expanded here. By default, only the first URL is listed.
Note that the push URL and the fetch URL, even though they can be set differently, must still refer to the same place. What you pushed to the push URL should be what you would see if you immediately fetched from the fetch URL. If you are trying to fetch from one place (e.g. your upstream) and push to another (e.g. your publishing repository), use two separate remotes.
See the PRUNING section of git-fetch[1] for what it’ll prune depending on various configuration.
DISCUSSION
The remote configuration is achieved using the remote.origin.url and remote.origin.fetch configuration variables. (See git-config[1]).
EXAMPLES
Add a new remote, fetch, and check out a branch from it
Imitate git clone but track only selected branches
How to use the git remote add origin command to push remotely
By itself, Git is an effective open source technology to keep track of local changes as you develop code, especially as you build sophisticated software. And while distributed facilities are built into the tool, many developers use Git purely as a mechanism to locally and privately track changes.
When developers want to take their local Git repository and share it with another developer or push the code into a cloud-based distributed version control service, such as GitHub or GitLab, they can use the git remote add origin command.
Three files and two commits
Before you follow along in this git remote add origin tutorial, set up a local Git installation, a locally initialized repository with at least one Git commit, and an account on GitHub or GitLab. For this tutorial, we will use GitHub, but the process is almost identical on GitLab.
Start off with a local repository, stored in a folder named my-local-repo that has three files in it:
The repository itself only has two commits, which you can see if you execute the git reflog command, as shown in the image below. Reflog is an abbreviation of reference logs.
Create the remote origin on GitHub
After the local repository is validated, the next step is to create a remote repository that the local repository will connect to. It’s easy to create a remote repository. Log into GitHub and use the «Create a new repository» wizard.
In this example, I named the GitHub repository my-github-repo to clearly differentiate it from the Git repository that is stored locally in a folder named my-local-repo.
Set up the remote origin repository on the GitHub server.
Copy and edit GitHub’s remote add URL
When GitHub creates your repository, it presents an HTTP link, which is required as part of the git remote add origin command. The URL provided that uniquely identifies the GitHub repository I created is:
Copy and paste this URL into a text editor and then add your username and password to the start of the URL:
http://cameronmcnz:[email protected]github.com/cameronmcnz/my-github-repo.git
This URL setup lets you authenticate the file without using a credential manager or other password management tool.
Run the git remote add origin command
With the GitHub URL saved to the clipboard in the folder that contains your local Git repository, open a terminal window and run the following git remote add origin command:
git remote add origin http://cameronmcnz:[email protected]github.com/cameronmcnz/my-github-repo.git
This command will execute, but the system won’t provide any feedback to the terminal window. To verify that the remote repo was added to your configuration, use the git remote –v command. This command will show that GitHub is the fetch and push targets of the local repository.
Perform a git push to the remote
Push changes to the remote server with the git push command.
As this action completes, the terminal window lists the number of objects pushed to the server and indicates that your local repository is set to track to a branch named master on the GitHub server.
Verify the git remote add push on GitHub
After the git remote add and push commands complete, go back to the GitHub instance and look at the contents of the recently created repository.
The result of the local git remote add and push command is reflected in the remote GitHub repository.
Overview of git remote add origin and push steps
In review, these are the five steps to successfully perform a git remote add origin and push to a remote repository:
Now that the remote and local repositories can interact seamlessly, you can continue to commit code locally, push changes to the remote GitHub server, and begin to manage your source code in a distributed manner.
Microsoft’s Azure Advisor service offers recommendations based on five categories. Learn these categories and the roles they play.
Researchers with Palo Alto Networks took the stage at Black Hat to explain how configurations and system privileges in Kubernetes.
git remote add explained [Easy Examples]
Table of Contents
How git remote add works
Git remote add is a git command that enables developers to work on a central remote repo by creating remote duplicates. Remote add command serves as a means through which collaborators for a project can independently make commits for a shared project. The remote add function in git also allows fetching changes made from the remote server to local. When using git, you must learn how to switch between the central remote to your own. That way, you can maintain all the status updates and make contributions through the push and fetch git methods.
In this tutorial about git remote add, we shall learn how to add a new remote repository, view remotes, and erase remotes in an active project.
Setting up the lab environment
To practice how git remote add function works, you will prepare a lab environment to run the experiments. I will be using git version 2.32.0.windows.2 and windows 10 pro as my local workstation to run experiments for this tutorial.
To begin, let’s clone a remote repository remote-add-demo a collaborated project and clone to the local server as follows;
Sample Output:
git remote add vs git remote set-url
Running git remote add will create a new remote repository while git remote set-url will modify an existing remote repository URL. Let’s use an example to further illustrate how the two commands comparably works.
We shall start with the git remote add command in the example below.
Next, we will apply git remote add command as shown below:
You will notice that a new remote origin has been added to the list of remotes. Now let’s run git remote set-url to see what happen.
The output shows an error because the git remote set-url command doesn’t create a new remote like what the git remote add function does hence the error. Git remote set-url when run it tends to locate the remote name specified among the existing ones and assigns it a new url.
Create “remote repo” from the “central remote repo”
To create a remote repository that you can use to test your codes or committing changes away from the central remote project use the git remote add command.
We shall practice how to add a remote repository in the following example:
Step-1: List available remote repositories
First, let’s display the current remotes in the active repository remote-add-demo by running the git remote –v command. –v stands for verbose that enables outputting all remotes with their URL’s.
We only have origin as remote in the current repository.
Step-2: Use git remote add to create a new remote repository
Now, we are going to add a new remote myremote to help make and track changes that we make before merging into a central repository.
Let’s use the git remote add function responsible for creating new remotes as follows;
To view the new remote myremote we shall run the git remote –v command as demonstrated below:
Sample Output:
Step-3: Fetch all the refs and remote tracking information
While working independently from your remote branch you can fetch updates from the upstream to your local repository using the git fetch command.
You can fetch updates from upstream to your remote myremote in this case by running git fetch commands as shown:
Sample Output:
How to push changes to a remote repository
Git allows you to push changes any time from your local server to the remotes found on your project. Use the git push
command as demonstrated in the example below to push your changes to remote:
Using the active project remote-add-demo we shall check out a new branch mybranch and push the changes upstream to the remote myremote :
Sample Output:
According to the output a new remote branch mybrnach was generated and changes pushed to myremote remote.
How to rename your remote repository
To rename your remote you will use the git remote rename
- command.
We shall run git remote –v command to views all the remotes in the active repository remote-add-demo as a start.
Sample Output:
To view the renamed remote m_remote we will run git remote –v command as follows:
Sample Output:
Note that renaming a remote also affects the tracking remote of the renamed.
How to delete remote repository
To erase a remote you will use the git remote remove or git remote rm command.
First, let’s display the remotes as shown below:
In this example, we will remove remote m_remote from the active repo r emote-add-demo as follows:
Remote m_remote has been erased from the list of remotes as displayed in the above output. NOTE that this operation erases the tracking remote as well from the remote repository.
Summary
We have covered the following topics regarding the git remote add function:
Further Reading
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!