Getting Started with Remote Repositories

Getting Started with Remote Repositories

Getting-Started-with-Remote-Repositories

Here in this article we will how we can make our local GIT repositories available on remote network so users can collaborate. We will see how we can push a local git repository to a remote hosting service (eg. Github) and try to sync up our local repository content with remote repository using push and pull command line utilities.

Test Environment

Fedora 37 workstation
Git v2.40.0

What are Remote Repositories

Remote repositories are projects that are hosted on the Internet or network somewhere (eg. On github, gitlab) wherein users collaborate. We can leverage the Distributed nature of Git Repositories by making our repositories available on a remote server for collaboration. GitHub, GilLab and BitBucket are some of the popular code hosting platform for version control and collaboration. These platforms let us and others to work together on projects from anywhere and provide additional feautures like CICD pipeline, issue management, artifactory scanning to name a few.

Procedure

Step1: Signup and Create a Remote Repository on GitHub

You can follow the GitHub Signup Documentation to register your self.

Once your signup process is completed. You can login with your username or email address and create a new empty repository on GitHub.

Once the empty repository is create you will get the details of the steps to follow push your content from local repository to remote repository as shown below.

Step2: Create GitHub Personal Access Token

Navigate to Your Profile – Settings – Developer Settings – Personal Access Tokens – Tokens and Generate new token. Save the Generated token in a secure location as its visible only once.

NOTE : GitHub Support for password authentication was removed on August 13, 2021

Step3: Create a Reference to Remote Repository

For an already existing local repository that we want to push to a remote repository we need to first add the reference to the remote repository and then push to remote repository branch.

Let us add the remote repository with a reference name “origin” as shown below.Let us add the remote repository with a reference name “origin” as shown below.

[admin@fedser learngit]$ git remote add origin https://github.com/novicejava1/learngit.git

Once we add the remote repository the .git internal repository config file is updated with the remote configuration as shown below.

[admin@fedser learngit]$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[user]
	name = novicejava1
	email = sudhirbhoga@gmail.com
[init]
	defaultBranch = main
[remote "origin"]
	url = https://github.com/novicejava1/learngit.git
	fetch = +refs/heads/*:refs/remotes/origin/*

Step4: Validate Remote Configuration

Instead of checking the configuration files for the remote repositories configuraiton. We can use the “git remote” command line utility to get the remote repositories configuration details as shown below. The output with “-v” option shows the verbose output showing the details of the push and pull remote endpoints

[admin@fedser learngit]$ git remote
origin
[admin@fedser learngit]$ git remote -v
origin	https://github.com/novicejava1/learngit.git (fetch)
origin	https://github.com/novicejava1/learngit.git (push)

Step5: Rename the remote reference

We can also rename our remote repository reference as shown below to a convenient name that we want to provide.

[admin@fedser learngit]$ git remote rename origin learngit
[admin@fedser learngit]$ git remote -v
learngit	https://github.com/novicejava1/learngit.git (fetch)
learngit	https://github.com/novicejava1/learngit.git (push)

Step6: Remove the remote reference

We can also remove the remote repository reference if its no longer required as shown below.

[admin@fedser learngit]$ git remote rm learngit
[admin@fedser learngit]$ git remote -v
[admin@fedser learngit]$ 

But for our demo let us re-add our remote reference with name “origin” as shown below.

[admin@fedser learngit]$ git remote add origin https://github.com/novicejava1/learngit.git

Step7: Get the details of Remote Repository

We can get the details of our remote repository which is reference using the shortname “origin” as shown below. Currently our remote repository is empty so you can see from the output that the HEAD reference is having “unknown” as the pointer.

[admin@fedser learngit]$ git remote show origin
* remote origin
  Fetch URL: https://github.com/novicejava1/learngit.git
  Push  URL: https://github.com/novicejava1/learngit.git
  HEAD branch: (unknown)

Step8: Rename master to main (optional)

The following article “Renaming the default branch from master” is reason we are renaming our branch from master to main. This is an optional step and can be ignored if you if want to continue with the master branch only.

[admin@fedser learngit]$ git branch -M main
[admin@fedser learngit]$ git branch -a
* main

Step9: Push to remote repository

Now let us try to push to the remote repository main branch as shown below. The push activity will require you to authenticaite.

[admin@fedser learngit]$ git push origin main
/usr/bin/gh auth git-credential get: line 1: /usr/bin/gh: No such file or directory
Username for 'https://github.com': novicejava1
Password for 'https://novicejava1@github.com': 
/usr/bin/gh auth git-credential store: line 1: /usr/bin/gh: No such file or directory
Enumerating objects: 60, done.
Counting objects: 100% (60/60), done.
Delta compression using up to 8 threads
Compressing objects: 100% (38/38), done.
Writing objects: 100% (60/60), 4.53 KiB | 231.00 KiB/s, done.
Total 60 (delta 13), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (13/13), done.
To https://github.com/novicejava1/learngit.git
 * [new branch]      main -> main

Now if we look at the remote repository “origin” details we can see from the output that the HEAD branch is pointing to main branch reference and its tracked

[admin@fedser learngit]$ git remote show origin
* remote origin
  Fetch URL: https://github.com/novicejava1/learngit.git
  Push  URL: https://github.com/novicejava1/learngit.git
  HEAD branch: main
  Remote branch:
    main tracked
  Local ref configured for 'git push':
    main pushes to main (up to date)

But by default git does not assume that you want to push to the same branch name on the remote repository from the same local branch that you are trying to push. Let see this in action.

[admin@fedser learngit]$ git push origin
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

Step10: Push to remote repository and set-upstream

Let us now try to set the upstream to which the current branch need to push as shown below.

[admin@fedser learngit]$ git push --set-upstream origin main
/usr/bin/gh auth git-credential get: line 1: /usr/bin/gh: No such file or directory
Username for 'https://github.com': novicejava1
Password for 'https://novicejava1@github.com': 
/usr/bin/gh auth git-credential store: line 1: /usr/bin/gh: No such file or directory
branch 'main' set up to track 'origin/main'.
Everything up-to-date

Now you can try to push without the remote branch name as shown below.

[admin@fedser learngit]$ git push origin
/usr/bin/gh auth git-credential get: line 1: /usr/bin/gh: No such file or directory
Username for 'https://github.com': novicejava1
Password for 'https://novicejava1@github.com': 
/usr/bin/gh auth git-credential store: line 1: /usr/bin/gh: No such file or directory
Everything up-to-date

You can check the commit id that the remote main branch that we just pushed points to as shown below which is the last commit on that branch locally.

[admin@fedser learngit]$ cat .git/refs/remotes/origin/main 
762f6a031fe6c19e3ead49d7693f9951288c4451

You can also check the local branch main pointer reference as shown below.

[admin@fedser learngit]$ cat .git/refs/heads/main 
762f6a031fe6c19e3ead49d7693f9951288c4451
[admin@fedser learngit]$ git log --oneline
762f6a0 (HEAD -> main, origin/main) This is an update to same file with conflict resolved
1704b5c This is an update to same file on edition branch
6a3ea27 This is an update to same file on master branch
caa75f0 created chapter4
23bb8bc created chapter4
5623dae created chapter4
f9c7919 moviing license file to markdown file type
c423e39 removing dummpy file
acf06e8 dummy file to be removed
fa38ac8 updated contributing file
f0a11ab update .gitignore file to ignore log and bin files
2f9fb33 adding contributing and updating readme
5d430bb update files commit 2
e2d9213 Initial Commit

Step11: Create a new branch

[admin@fedser learngit]$ git switch -c edition1
Switched to a new branch 'edition1'

Let us update our .gitignore file to ignore bash scripts “.sh” file from tracking.

[admin@fedser learngit]$ git add .gitignore 
[admin@fedser learngit]$ git commit -m "update .gitignore file"

Step12: Push new branch to remote

Now we can try to push the local “edition1” branch to remote repository as shown below.

[admin@fedser learngit]$ git push -u origin edition1
/usr/bin/gh auth git-credential get: line 1: /usr/bin/gh: No such file or directory
Username for 'https://github.com': novicejava1
Password for 'https://novicejava1@github.com': 
/usr/bin/gh auth git-credential store: line 1: /usr/bin/gh: No such file or directory
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 294 bytes | 294.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: 
remote: Create a pull request for 'edition1' on GitHub by visiting:
remote:      https://github.com/novicejava1/learngit/pull/new/edition1
remote: 
To https://github.com/novicejava1/learngit.git
 * [new branch]      edition1 -> edition1
branch 'edition1' set up to track 'origin/edition1'.

Now if you look at .git/config configuration it will be updated with the new remote branch “edition1” configuration as shown below.

[admin@fedser learngit]$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[user]
	name = novicejava1
	email = sudhirbhoga@gmail.com
[init]
	defaultBranch = main
[remote "origin"]
	url = https://github.com/novicejava1/learngit.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main
[branch "edition1"]
	remote = origin
	merge = refs/heads/edition1

You can also look at the same details using the below “git remote” utility as shown below.

[admin@fedser learngit]$ git remote show origin
* remote origin
  Fetch URL: https://github.com/novicejava1/learngit.git
  Push  URL: https://github.com/novicejava1/learngit.git
  HEAD branch: main
  Remote branches:
    edition1 tracked
    main     tracked
  Local branches configured for 'git pull':
    edition1 merges with remote edition1
    main     merges with remote main
  Local refs configured for 'git push':
    edition1 pushes to edition1 (up to date)
    main     pushes to main     (up to date)

Hope you enjoyed reading this article. Thank you..