Skip to content

Git - Online Repositories

Platforms like GitHub, GitLab, and Bitbucket provide web-based hosting for Git repositories, along with features for collaboration, issue tracking, and more. This section focuses on interacting with a GitHub repository using the command line.

  1. Go to GitHub.
  2. Sign in to your account or create a new one.
  3. Create a new repository by clicking the “New” button or the ’+’ icon in the navigation bar. Follow the on-screen instructions, giving your repository a name (e.g., my_first_repo). You can choose to initialize it with a README, .gitignore, and license.

Pushing an Existing Local Repository to GitHub

Section titled “Pushing an Existing Local Repository to GitHub”

Tom has an existing local Git project and wants to host it on GitHub. First, he creates a new, empty repository on GitHub (e.g., named hello_world_project) without initializing it with a README, .gitignore, or license, as his local project already has its own history.

Tom has a local project with a hello.c file:

[tom@devbox ~]$ mkdir hello_world_project
[tom@devbox ~]$ cd hello_world_project/
[tom@devbox hello_world_project]$ echo '#include <stdio.h>
int main() {
printf("Hello, Modern Git!\n");
return 0;
}' > hello.c
[tom@devbox hello_world_project]$ gcc hello.c -o hello
[tom@devbox hello_world_project]$ ./hello

The above command will produce the following result:

Hello, Modern Git!

After verifying his code, he initializes a Git repository locally, adds, and commits his file:

[tom@devbox hello_world_project]$ git init -b main
Initialized empty Git repository in /home/tom/hello_world_project/.git/
[tom@devbox hello_world_project]$ git status -s
?? hello.c
[tom@devbox hello_world_project]$ git add hello.c
[tom@devbox hello_world_project]$ git status -s
A hello.c
[tom@devbox hello_world_project]$ git commit -m "Initial commit with hello.c"

Next, he adds the GitHub repository URL (provided by GitHub when you create a new repository, e.g., https://github.com/yourusername/hello_world_project.git) as a remote named origin and pushes his main branch:

[tom@devbox hello_world_project]$ git remote add origin https://github.com/yourusername/hello_world_project.git
[tom@devbox hello_world_project]$ git push -u origin main

Authentication: When pushing to GitHub over HTTPS, Git will prompt for credentials. Instead of your password, you must use a Personal Access Token (PAT). You can generate a PAT from your GitHub account’s Developer settings. For SSH URLs (e.g., git@github.com:yourusername/hello_world_project.git), you’d use SSH keys. For more details, see GitHub’s documentation on PATs.

After successful authentication, the output will be similar to:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 250 bytes | 250.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/yourusername/hello_world_project.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

Tom can now push any subsequent changes to this GitHub repository using git push.

Cloning a Repository from GitHub (Pull Operation)

Section titled “Cloning a Repository from GitHub (Pull Operation)”

Tom has successfully pushed his project to GitHub. Now, another developer, Jerry, can get a copy of this project.

Jerry creates a directory and clones the GitHub repository using its HTTPS URL:

[jerry@devbox ~]$ mkdir jerry_projects
[jerry@devbox ~]$ cd jerry_projects/
[jerry@devbox jerry_projects]$ git clone https://github.com/yourusername/hello_world_project.git

The above command produces output like:

Cloning into 'hello_world_project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 250 bytes | 125.00 KiB/s, done.

He verifies the directory contents:

[jerry@devbox jerry_projects]$ ls
hello_world_project
[jerry@devbox jerry_projects]$ cd hello_world_project/
[jerry@devbox hello_world_project]$ ls
hello.c

Jerry now has a local copy and can start contributing. To get future updates from Tom, Jerry would use git pull inside his hello_world_project directory.