Work with remote in Git to share your code

In last few posts of series of articles on the Git, we discussed several ways to work with code in our local repository. We learned about commits, branches, merge, rebase, stash and whole lot of other commands. If you want to see all those posts, just filter using Git category appearing in left pane in this site. However for most of the time, while working for an complex software, you would be working along with other developers. Therefore, you need a central place where you could host all of the source code and then you need some ability to download/upload your part of the code. This is where in the cloud-based Git repository providers like BitBucket, GitLab, GitHub, Azure Repos etc or On-Premise based Git repository providers like Azure DevOps / TFS, GitHub Enterprise , etc fits in. We already have learned the ability to segregate code for different features/issues by using concept of branches and tags.

As there are too many choices for Git Providers, we would focus on only one i.e. GitHub (github.com or GitHub Enterprise) and learn about the concepts. These concepts apply in almost identical ways on all the other repositories providers.

The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories. Instead of providing real-time access to another repository, they serve as convenient names that can be used to reference a not-so-convenient URL.

Create a Repository in GitHub

For this, we need to login to https://github.com. If you do not already have an account, you may sign-up for one. It’s free and does not charge anything for public repositories. However for hosting private repositories, you need to pay a fee. Anyhow, let’s go to repositories section and create a New repository:

click on new to create a public repositories in github

Once you click the New button, it will ask for some basic information. The important of which is the repository name, an optional description and whether to you want to create a public repository or a private repository. Note that if you create a public repository, it is available to all users across the world.

For now, we’ll leave repository as Public and also uncheck the box for including README.md file and create repo:

provide details for creating a repository

It will now bring you to the page containing repository and some other information:

new blank repository in github

Add Remote information to local Git Repository

Let’s add the remote information to one of the local git repositories that we have. For this, we need to run below command on the local repo:

git remote add origin https://github.com/goyalmohit/working-with-remotes.git

add a remote to the local repository

where it means that the remote url is the url of the repository i.e. https://github.com/goyalmohit/working-with-remotes.git

We can then view list of remotes stored and their information using

git remote –verbose

viewing list of remotes and their information

The origin Remote

In above command, the word origin refers to the alias that can be used instead of using the repository URI. So when we say origin, it will refer to the URI stored for this remote. Generally, the default is to add remote name as origin, but you can keep any name of your choice.

When you clone a repository with git clone, it automatically creates a remote connection called origin pointing back to the cloned repository. This is useful for developers creating a local copy of a central repository, since it provides an easy way to pull upstream changes or publish local commits. This behavior is also why most Git-based projects call their central repository origin.

Add as many Remotes as you need

Also note that one can add as many remotes as one wants. For example, you may pull code from an server located in Brazil and push code to an server located in the Singapore. However, you need to be extra mindful when pushing or pulling the code changes.

Push code to the Remote

Let’s upload all items and histories to remote. For this, we need to run:

git push -u origin master

upload source code into github

When you push, Git adds special branch called remote tracking branch, which is the counterpart of remote repository’s branch. -u option connects local master repository to the special branch. By doing this, you can simply run git push next time without specifying remote and branch name. Then Git copies all items to the remote repository.

We can run below command to see your local branches as well as remote tracking branches:

git branch -a

viewing list of branches after adding remote

The red one, remotes/origin/master is the remote tracking branch Git adds when we ran git push command. Remote-tracking branch has commits from remote repository. By having these special branches, git avoid overriding local branch items directly.

If we want to upload all local branches, we can use below command:

git push -u origin –all

Verify code has been pushed to Remote

If we now go to GitHub, refresh our repository page, we should be able to see our code:

verify the code is uploaded into github repo

We can navigate around, see commit history and all to verify further that required changes has been pushed.

Receive changes from Remote

For this, we need to have some changes in the remote, which are not present in the local repository. For demo purpose, I’ll make changes in the github code directly so that it differs from local repo code by adding some files say Class01.cs:

create some code change directly in the remote

Most of the time, when you have multiple developers working on the same branch, some one will keep pushing their code to master branch in remote and hence it will become different from your local version of the master branch.

Now, to pull these change in the local repo, we need to run:

git pull origin master

pull changes in the local master branch

Again in above command, master is the name of the branch for which we want the code from remote.

Resolve Code conflicts while pull/push from Remotes

This is the same behavior that you would see while merging branches across branches. If there is a file edited in the both branches, it is likely to create a merge conflict. We will observe same behavior while trying to pull/push changes from remote. Since git treats our local branch and remote branch separately as demonstrated above, we need to resolve conflict in the same way as we would resolve merge conflict otherwise.

For example, let’s modify Class01.cs file in the local code and commit our changes:

Modify Class01.cs in the local branch

And modify same file in the remote:

Modify Class01.cs in the remote branch

Now, let’s go ahead and try to push our changes to remote by using git push command. We would now see something like below error:

observe push conflict while trying to push our changes

As its pretty evident from message that we need to pull changes from remote in to our local code. So let’s do that using git pull and resolve conflict:

resolve merge conflicts and push code changes to remote

Verify that our change is now available in the remote:

verify the code is uploaded into github repo 2

Renaming Remotes

To rename the alias for remote, we can use below command:

git remote rename old-name new-name

The command git remote rename is self-explanatory. When executed, this command will rename a remote connection from old-name to new-name. Additionally, this will modify the contents of ./.git/config to rename the record for the remote there as well.

Remove Remotes

To remove the remote named origin, we can use below command:

git remote rm origin

delete remote information from the local repo

The command git remote rm will remove the connection to the remote repository specified by the parameter. To demonstrate let us ‘undo’ the remote addition from our last example. If we execute git remote rm origin, and then examine the contents of ./.git/config we can see that the [remote “origin”] record is no longer there.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s