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:
git add .
git commit -m "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.
In the Create new repository page, specify your project name and repository name. After that, set the options for both Include a README and Include .gitignore to No. Doing so will set up an empty remote repo that is ready to be connected to your local repo.
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://firstname.lastname@example.org/username/your-repo.git.
2.2: Creating a New Repo on GitHub
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:
git remote -v
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.
Run the git remote -v command again. The output should look like this:
origin https://email@example.com/username/good-year.git (fetch)
origin https://firstname.lastname@example.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.
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:
git push -u origin controller
The above command should update the state of your remote repo to the same state as the local repo.
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.
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:
git add remote <shortname> <URL>
Replace <shortname> and <URL> with the correct values for your repo, and you're all set!
Stay up to date
We'll never share your email address and you can opt out at any time, we promise.