Learn how to use Git and Github to collaborate on a project or contribute on
a open-source project

Learn how to use Git and Github to collaborate on a project or contribute on a open-source project

Git is a distributed version control system built to handle small and very large projects easily with speed and efficiency as it allows many users to work on the same project without disturbing the main work with the help of branches.

Branches are used within the development process of the project which involves creating a new feature, and fixing bugs it acts as a copy of your main work but changes in a branch do not reflect on your main work and you can use it for trial and testing and after getting the desired output you can open a pull request to merge the changes on your branch to the main branch ie - update your main branch with the changes.

Steps for pushing your project to GitHub-

First thing first you'll need to create an empty GitHub repository inside where you would put your project files.

After creating the repo you will be displayed the following screen

  • The first step is initialising git inside your project folder.

  • The second step is creating a remote to your GitHub repo.

  • The third step is to add your project files in the staging area using the command

    " git add . " This will add the project files from your local directory to the staging area.

  • The fourth step is to commit your changes with a message using the command

    git commit -m "message"

  • Create a main branch using the command,

    git branch -M main and push your staged files to the main using the command

    git push -u origin main

Collaborate on an existing GitHub project using branches and pull requests

Now to collaborate on an existing project first you'll need to clone the project on your local directory using the command git clone project_url

After, it is a great practice to create your branch for modifications, to create a branch use the command git checkout -b <branch-name>

Now inside your branch, you can add your feature and after finishing the feature you can add the changes to the staging area using the command

git add .

After adding, commit the changes with an appropriate message describing the feature you have changed or updated to do so use the command

git commit -m "message"

The above steps keep on repeating while changes are done.

Now the final step is to push the changes on your branch and open a pull request for the reviewer to review it and merge it with the main to do so following sets of commands are used -

  • git push origin <branch name>

  • Now to open a pull request for your changes do the following steps

  • Navigate to your GitHub Repo and select your branch which consists of your commits -

  • Above the list of files, click Pull request.

  • Type a title and description for your pull request.

  • Click on create pull request button

After creating the pull request the owner of the project will review your code and merge it to the master branch if there are no conflicts.

Note - before doing any changes on your branch you must pull the changes done on the main so that there are no merge conflicts or issues to do so -

  • Before pulling changes switch to your branch using get checkout <branch-name>
  • fetch the current origin using git fetch origin

  • use git merge origin/main to pull changes from the main branch to your local branch another way to do this is to use the command git rebase origin/main

  • You can check whether you are behind commits or not using the git status

  • Now you can simply pull the changes from the master branch to the local branch.

Congrats!! now you can collaborate easily on projects with your team mates without disturbing the existing work flow and also create your own projects and push it to github to collaborate with others or work on a open source project.

Did you find this article valuable?

Support Devcon blogs by becoming a sponsor. Any amount is appreciated!