Open In App

Creating a Pull Request on any Public Repository from GitHub using VS Code

Improve
Improve
Like Article
Like
Save
Share
Report

Free and open-source, Git is a distributed version control system that makes collaborative software projects more manageable. Many projects maintain their files in a Git repository, and sites like GitHub have made sharing and contributing to code simple, valuable, and effective.

You have to create a free account on https://github.com/ if you have then login into it.

Create a Copy of the Repository

In order to work on an open-source project, you will first need to make your own copy of the repository. To do this, you should fork the repository and then clone it so that you have a local working copy.

Fork the Repository

We can fork any public repository from Github by clicking on the “Fork” button on your upper right-hand side of the page, underneath your user icon:

Now the repository will be forked to your Github account.

Clone the Repository

To make your own local copy of the repository you would like to contribute to, first, make a folder in open in VS Code.

Copy the URL of the git repository as the instruction given below:

Now in VS Code terminal write the following command:

$ git clone https://github.com/pallavisharma26/Node.js-Express-MongoDB-CRUD.git

Now that we have a local copy of the code, we can move on to creating a new branch on which to work with the code.

Create a New Branch

The default main branch of a project repository is usually called the master branch. A common best practice is to consider anything on the master branch as being deployable for others to use at any time.

Now, we’ll create our new branch with the git branch command. Make sure you name it descriptively so that others working on the project understand what you are working on.

$ git branch new-branch

Now that our new branch is created, we can switch to make sure that we are working on that branch by using the git checkout command:

$ git checkout new-branch

When you run this command you will get the Output:

Switched to branch ‘new-branch’

If you want to go back to master again then the command is:

$ git checkout master

At this point, you can now modify existing files or add new files to the project on your own branch.

Once you have modified an existing file or added a new file to the project, you can add it to your local repository, which we can do with the git add command:

 $ git add filename.md

If you want to add all modified or added file then use this command:

$ git add .

Now commit the changes you made in the repository with the help of git commit command.

$ git commit

If you have a very short message, we can record that with the -m flag and the message in quotes

 $ git commit -m “your messages”

If you would like to configure your default text editor, you can do so with the git config command, and set nano as the default editor, for example:

$ git config –global core.editor “nano”

Once you have saved and exited the commit message text file, you can verify what git will be committing with the following command:

$ git status

After all this now it is ready to push our changes to the current branch of your forked repository:

 $ git push –set-upstream origin new-branch

Output of all the command done above is:

Update Local Repository

While you are working with many developers on the same project, it is important for you to keep your local repository up-to-date with the project as you don’t want to make a pull request for code that will cause conflicts. To keep your local copy of the codebase updated, you’ll need to sync changes.

Configure a Remote for the Fork

Remote repositories make it possible for you to collaborate with others on a Git project. Each remote repository is a version of the project that is hosted on the Internet or a network you have access to. Each remote repository should be accessible to you as either read-only or read-write, depending on your user privileges.

$ git remote -v

Output will be:

origin  https://github.com/pallavisharma26/Node.js-Express-MongoDB-CRUD.git (fetch)

origin  https://github.com/pallavisharma26/Node.js-Express-MongoDB-CRUD.git (push)

Next, we’ll specify a new remote upstream repository for us to sync with the fork. This will be the original repository that we forked from. We’ll do this with the git remote add command.

$ git remote add upstream https://github.com/CodAffection/Node.js-Express-MongoDB-CRUD.git

We can verify that our remote pointer to the upstream repository was properly added by using the git remote -v command again from the repository directory:

$ git remote -v

Output will be:

origin https://github.com/pallavisharma26/Node.js-Express-MongoDB-CRUD.git (fetch)

origin https://github.com/pallavisharma26/Node.js-Express-MongoDB-CRUD.git (push)

stream https://github.com/CodAffection/Node.js-Express-MongoDB-CRUD.git(fetch)

stream https://github.com/CodAffection/Node.js-Express-MongoDB-CRUD.git(push)

Now you can refer to upstream on the command line instead of writing the entire URL, and you are ready to sync your fork with the original repository.

Sync the Fork

To sync our fork, from the directory of our local repository in a terminal window, we’ll use the git fetch command to fetch the branches along with their respective commits from the stream repository. Since we used the shortname “upstream” to refer to the upstream repository, we’ll pass that to the command:

$ git fetch upstream

Output will be:

From https://github.com/CodAffection/Node.js-Express-MongoDB-CRUD.git

* [new branch]      master     -> upstream/master

Now, commits to the master branch will be stored in a local branch called upstream/master.

Let’s switch to the local master branch of our repository:

$ git checkout master

We’ll now merge any changes that were made in the original repository’s master branch, which we will access through our local upstream/master branch, with our local master branch:

$ git merge upstream/master

After this, it starts updating the changes made.

Create Pull Request

Now if you open a public repository you get this:

After clicking on Compare & pull request you get:

GitHub will alert you that you are able to merge the two branches because there is no competing code. You should add in a title, a comment, and then press the “Create pull request” button.


Last Updated : 22 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads