GIT - Tutorials

Master GIT with our comprehensive tutorials. Learn from basics to advanced concepts, build real-world projects. Start coding today!


Working with GitHub Repositories

GitHub is a popular web-based hosting service that supports Git for version control. It offers both a web interface and GUI applications for Windows, Mac, and GNU/Linux, but in this session, we focus on the command-line interface (CLI).

Creating a GitHub Repository

To create a GitHub repository, go to github.com. If you already have an account, log in. Otherwise, create a new account and follow the steps to set up a new repository.

Push Operation: Uploading Code to GitHub

In this scenario, David decides to push his code to GitHub. He begins by creating a new directory for the project, writes some code, and builds it.

Steps to Create and Build the Project

[david@CentOS]$ mkdir github_repo
[david@CentOS]$ cd github_repo/
[david@CentOS]$ vi hello.c
[david@CentOS]$ make hello
cc hello.c -o hello
[david@CentOS]$ ./hello
Output

Hello, World !!!
        

After verifying the program output, David initializes the directory with Git, stages the changes, and commits them locally.

Initialize Git Repository and Commit

[david@CentOS]$ git init
[david@CentOS]$ git status -s
?? hello
?? hello.c
[david@CentOS]$ git add hello.c
[david@CentOS]$ git status -s
A hello.c
[david@CentOS]$ git commit -m 'Initial commit'

Pushing Changes to GitHub

After committing the code locally, David adds the GitHub repository URL as a remote and pushes the changes. This operation will prompt for GitHub credentials.

Push Changes to GitHub

[david@CentOS]$ git remote add origin https://github.com/kangralkar/testing_repo.git
[david@CentOS]$ git push -u origin master
Output

Username for 'https://github.com': kangralkar
Password for 'https://kangralkar@github.com': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/kangralkar/test_repo.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
        

From now on, David can push any future changes to the GitHub repository by following the same steps.

Pull Operation: Cloning the Repository

After David pushes his changes to the GitHub repository, other developers can access the code by cloning the repository. In this example, Tom clones the repository into a new directory on his system.

Cloning the Repository

[tom@CentOS]$ pwd
/home/tom
[tom@CentOS]$ mkdir tom_repo
[tom@CentOS]$ git clone https://github.com/kangralkar/test_repo.git
Output

Cloning into 'test_repo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
        

Tom verifies the contents of the directory to confirm the cloned files.

Verify Directory Contents

[tom@CentOS]$ ls
test_repo
[tom@CentOS]$ ls test_repo/
hello.c