Git Repository and How to Fork Repository

Last updated on May 28 2022
Meetesh Jain

Table of Contents

Git Repository and How to Fork Repository

Git Repository

In Git, the repository is like a data structure used by VCS to store metadata for a set of files and directories. It contains the collection of the files as well as the history of changes made to those files. Repository in Git is considered as your project folder. A repository has all the project-related data. Distinct projects have distinct repositories.

Getting a Git Repository

There are two ways to obtain a repository. They are as follows:
• Create a local repository and make it as Git repository.
• Clone a remote repository (already exists on a server).
In either case, you can start working on a Git repository.

Initializing a Repository

If you want to share your project on a version control system and control it with Git. Then, browse your project’s directory and start the git command line (Git Bash for Windows) here. To initialize a new repository, run the below command:

Syntax:

1. $ git init

Output:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

The above command will create a new subdirectory named .git that holds all necessary repository files. The .git subdirectory can be understood as a Git repository skeleton. Consider the below image:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

An empty repository. git is added to my existing project. If we want to start version-controlling for existing files, we should track these files with git add command, followed by a commit.

We can list all the untracked files by git status command.
1. $ git status
Consider the below output:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

In the above output, the list of all untracked files is displayed by the git status command. To share these files on the version control system, we have to track it with git add command followed by a commit. To track the files, operate git add command as follows:

Syntax:

1. $ git add <filename>

To commit a file, perform the git commit command as follows:

1. $ git commit -m “Commit message.”

Output:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

In the above output, I have added three of my existing files by git add command and commit it for sharing.

We can also create new files. To share the new file, follow the same procedure as described above; add and commit it for sharing. Now, you have a repository to share.

Cloning an Existing Repository

We can clone an existing repository. Suppose we have a repository on a version control system like subversion, GitHub, or any other remote server, and we want to share it with someone to contribute. The git clone command will make a copy for any user to contribute.

We can get nearly all data from server with git clone command. It can be done as:

Syntax:

1. $ git clone <Repository URL>

Suppose one of my friends has a repository on my GitHub account, and I want to contribute to it. So the first thing I will do, make a copy of this project to my local system for a better work interface. The essential element needed for cloning the repository URL. I have a repository URL “https://github.com/ImDwivedi1/Git-Example”. To clone this repository, operate the clone command as:

1. $ git clone https://github.com/ImDwivedi1/Git-Example
Consider the below output:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

In the above output, the repository Git-Example has been cloned. Now this repository is available on your local storage. You can commit it and contribute to the project by pushing it on a remote server.

A single repository can be cloned any number of times. So we can clone a repository on various locations and various systems.

Git Fork

A fork is a rough copy of a repository. Forking a repository allows you to freely test and debug with changes without affecting the original project. One of the excessive use of forking is to propose changes for bug fixing. To resolve an issue for a bug that you found, you can:

• Fork the repository.
• Make the fix.
• Forward a pull request to the project owner.

Forking is not a Git function; it is a feature of Git service like GitHub.

When to Use Git Fork

Generally, forking a repository allows us to experiment on the project without affecting the original project. Following are the reasons for forking the repository:
• Propose changes to someone else’s project.
• Use an existing project as a starting point.
Let’s understand how to fork a repository on GitHub?

How to Fork a Repository?

The forking and branching are excellent ways to contribute to an open-source project. These two features of Git allows the enhanced collaboration on the projects.

Forking is a safe way to contribute. It allows us to make a rough copy of the project. We can freely experiment on the project. After the final version of the project, we can create a pull request for mergingIt is a straight-forward process. Steps for forking the repository are as follows:

• Login to the GitHub account.
• Find the GitHub repository which you want to fork.
• Click the Fork button on the upper right side of the repository’s page.

We can’t fork our own repository. Only shared repositories can be fork. If someone wants to fork the repository, then he must log in with his account. Let’s understand the below scenario in which a user pune2016 wants to contribute to our project GitExample2. When he searches or put the address of our repository, our repository will look like as follows:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

The above image shows the user interface of my repository from other contributors. We can see the fork option at the top right corner of the repository page. By clicking on that, the forking process will start. It will take a while to make a copy of the project for other users. After the forking completed, a copy of the repository will be copied to your GitHub account. It will not affect the original repository. We can freely make changes and then create a pull request for the main project. The owner of the project will see your suggestion and decide whether he wants to merge the changes or not. The fork copy will look like as follows:

Git Repository and How to Fork Repository
Git Repository and How to Fork Repository

As you can see, the forked repository looks like pune2016/GitExample2. At the bottom of the repository name, we can see a description of the repository. At the top right corner, the option fork is increased by 1 number.
Hence one can fork the repository from GitHub.

Fork vs. Clone

Sometimes people considered the fork as clone command because of their property. Both commands are used to create another copy of the repository. But the significant difference is that the fork is used to create a server-side copy, and clone is used to create a local copy of the repository.

There is no particular command for forking the repository; instead, it is a service provided by third-party Git service like GitHub. Comparatively, git clone is a command-line utility that is used to create a local copy of the project.

Generally, people working on the same project clone the repository and the external contributors fork the repository.

A pull request can merge the changes made on the fork repository. We can create a pull request to propose changes to the project. Comparatively, changes made on the cloned repository can be merged by pushing. We can push the changes to our remote repository.

So, this brings us to the end of blog. This Tecklearn ‘Git Repository and How to Fork Repository’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Git and GitHub and build a career in DevOps domain, then check out our interactive, Version control with Git and GitHub Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Version Control with Git and GitHub

Version Control with Git and GitHub Training

About the Course

Tecklearn has specially designed this Git and GitHub Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of important concepts like: branching & merging, how to deal with conflicts, rebasing, merge strategies, Git workflows and so on. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Git.

Why Should you take Git and GitHub Training?

• Average salary of Git and GitHub Professional is $85k – Indeed.com
• Amazon, Google, Facebook, Microsoft, Twitter, & many other MNC’s worldwide use Git across industries.
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025.

DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation

Git and GitHub

• What is version control
• Version Control System (VCS) Products
• Types of VCS
• What is Git
• Why Git for your organization
• Install Git
• Common commands in Git
• Working with Remote Repositories
• GitHub
• Git Installation
• Git Lifecycle
• GitHub (Push, Pull Request)
• GitHub Workflow

 

0 responses on "Git Repository and How to Fork Repository"

Leave a Message

Your email address will not be published. Required fields are marked *