A Beginners Guide to Contributing to an Existing Git Repository
Tags: beginners, git, github
Published: 2022-10-17 11:18:51

A Git repository helps an individual and organizations to store a collection of files of various different versions of a Project. These files are usually imported from the repository into the local server of the user for update and modifications. Git is also used for coordinating work among programmers collaboratively developing source code during software development.

As an inexperienced newcomer, new to an organization, working with git command can be very overwhelming. This article is a guide to help a newbie make their first contribution in an existing organization Git repository. It can also be used by an expert as a reference guide.  Let's get into it.

## Prerequisites

- Git installed in your computer
- GitHub account or any other software repository

**NOTE:** For the purpose of this blog we will be working with this repository: https://github.com/MalonzaElkanah/To-Do-Flask on a Linux distro PC.

##Step 1: Initialize the project folder
The first step is to create a folder for the project and give it the same name as the project. For example, I will open the terminal and create a folder called _‘to-do app’_. I use the command _‘**mkdir**’_ to create a folder in Ubuntu OS. After creating the folder, use the _‘**cd**’_ command to navigate into the folder.  

![Terminal screen of mkdir and cd commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkusy738dschuqy0k5av.png)  

We need to turn the new folder to a local repository for the project. Using the ‘`git init`’ command, we will create an empty Git repository. This command will create a .git directory with sub-directories for objects , refs/heads , refs/tags , and template files.

![git init terminal screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8g58t80sm1bdpqv3ulep.png)

##Step 2: Connect the local repo to the remote repo
So far your local repository is not linked to the remote project repository. To connect to the project remote repository, use the ‘`git remote add`’ command on the terminal of your local repository. The git remote add command takes two arguments:

- A **remote name**, for example, origin     
- A **remote URL**, for example, https://github.com/user/repo.git  

The easiest way to get the remote-url is to add ‘_.git_’ to the remote repository link, For Example: [https://github.com/MalonzaElkanah/To-Do-Flask] repo, add ‘.git’ at the end to create the remote url, [https://github.com/MalonzaElkanah/To-Do-Flask.git ]. 
You can also get the remote-url from the repo page:

![github repository page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ji3ijckbd5cwwbtjfxcr.png)

After using the ‘`git remote add`’ command, use the ‘`git remote -v`’ command to verify the remote repo connection:

![git remote terminal screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jc4l1l3se8p0caimzpdz.png)

##Step 3: Get the files from remote repo
We need to download the remote repository files in order to start making contributions. The ‘`git fetch --all`’ command is a primary command used to download all contents from a remote repository. 

![git fetch --all command terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/farj2cca15oul7xg5c7y.png)

The files have been downloaded to the local repository and stored in the .git folder. The files are not available in our project folder because we have not selected any remote branch. Assuming the remote repository you will be working in contains a couple of branches, you can switch between these branches using ‘`git checkout`’ command. 
For instance, currently the to-do app has one branch, ’main’ branch, so the command to switch to that branch will be: `git checkout main`

![git checkout main terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdnuu53iy6t1jauhv9cx.png)

Now all the files that belong to the main branch are available in your local project folder.
To find out what branches are available in your local repository and what the current branch name is, execute ‘`git branch`’ command.

##Step 4: Updating Remote repo from local repo changes
If you have made modifications to the content of the project file and/or added new files to the project folder, you can update the changes to the remote repository. To view the modified files use the ‘`git status`’ command. For example, I have modified the _README.md_ file and added _requirements.txt_ file.

![git status terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1okmnk4vc7lhdywkxym8.png)

To push the changes:

1. Use the ‘`git add .`’ command to make all new files(in this case, _requirements.txt_) to be tracked by git. To add a specific file/folder use ‘`git add <file/folder-name>`’, for instance, in our example we will use: `git add requirements.txt` 
2. Then, use the ‘`git commit`’ to 'stage' changes to the project that will be stored in a commit. That is, the command captures a snapshot of the project's currently staged changes. The command has several option
  - **-a option** for committing a snapshot of all changes in the working directory.
  - **-m option** for passing a commit message.
  - **--amend option** for modifying the last commit. Instead of creating a new commit, staged changes will be added to the previous commit.
For instance, the commit statement for our to-do app will be: 
`git commit -a -m "Added Project requirements, updated README.md"`

3. ‘`git push`’ command will upload local repository content to a remote repository. It will transfer all commits from your local repository to a remote repository. 

For instance, to push our to-do app local changes:

![git add, commit and push terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwq90dubukmm4lmgo3oe.png)

##Step 5: update the local repo to match remote repo content 
When different people are working on the same project or you are working on the same project from multiple PCs, sometimes the remote repository is ahead in terms of commits in comparison to local repository. 
The ‘`git pull`’ command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.

![git pull terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oj2ddzhvrgokzshk5bmq.png)

##Step 6: Creating remote branches
In a Collaboration project, programmers don’t usually work on the main branch. When a programmer wants to add a new feature or fix a bug, no matter how big or how small, they spawn a new branch to encapsulate the changes. When the features are complete or bugs fixed they merge the branch to the main.  

In order to create a new branch locally, use the ‘`git checkout -b`’ command. To create a branch from another branch using git checkout, use the following syntax: `git checkout -b <new_branch> <old_branch>`
In our project I will create a ‘_sqlalchemy_orm_’ branch from the _main_ branch.  

![git checkout -b terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3xuw7ab7l9natnu46gq.png)

For now the new branch only exists in the local repository. To update the remote repository about the new branch, use the ‘`git push –set-upstream origin <new_branch>`’ command.

![git push –set-upstream origin terminal window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/symxmrsr07uf06dbpi54.png)

Every change you commit will be pushed to the new branch unless you merge the branch with another or you switch to another branch.

##End Note
The objective of this article was to give step by step instruction to contribute to an existing repository for a beginner. There are other git commands not mentioned above that were irrelevant to the guide but are as much important. It's wise to know at least what they do just in case you get any conflict when working with above git commands. 

HAPPY CODING!