Git : GitHub and working with remote repositories

Git : GitHub and working with remote repositories

What is GitHub ?

GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command-line tool, GitHub provides a Web-based graphical interface.

Creating a GitHub Account

Go to this Link in a web browser. Enter your details and create your account. For more details and a step-by-step guide, visit this wikihow page

An Image of GitHub Login Page


Making your first repository and your first commit

Follow this guide to create a new repository

Congratulations! You have now created a repository, including a README file, and created your first commit on GitHub.

Now let's work with remote repositories on our local machine.

Forking a Repository

You might fork a project to propose changes to the upstream, or original, repository. In this case, it's good practice to regularly sync your fork with the upstream repository.

Follow these steps:

  1. On GitHub, navigate to the desired repository, example repo

  2. In the top-right corner of the page, click Fork. An Image of fork icon on github

  3. Congratulations! You now have your own copy of the forked repository.

Cloning the Forked Repository

  1. On GitHub, navigate to your copy of the forked repository. The name of the repo will be of format Your-Usename/repo-name.

  2. Above the list of files, click Code. An Image of code icon on github

  3. To clone the repository using HTTPS, under "Clone with HTTPS", click the copy button. To clone the repository using an SSH key, including a certificate issued by your organization's SSH certificate authority, click Use SSH, then click the copy button. To clone a repository using GitHub CLI, click Use GitHub CLI, then click the copy button. An Image of copy URL for clone icon on github

  4. Open Terminal/Git Bash.

  5. Change the current working directory to the location where you want the cloned directory.

  6. Type git clone, and then paste the URL you copied earlier. It will look like this, with your GitHub username instead of YOUR-USERNAME:

    $ git clone https://github.com/YOUR-USERNAME/repo-name
    
  7. Press Enter. Your local clone will be created.

Great!! You have created a clone of the repo on your local machine.

Adding upstream

Upstream generally refers to the original repo that you have forked.

  1. Check the remote repos(origin and upstream) for the clone using the command:
    git remote -v
    
  2. In the output you will see two origin and two upstream repository urls the fetch and push at the end. Example:
origin    https://github.com/YOUR-USERNAME/repo-name.git (fetch)
origin    https://github.com/YOUR-USERNAME/repo-name.git (push)
upstream    https://github.com/RepoOwner-Name/repo-name.git (fetch)
upstream    https://github.com/RepoOwner-Name/repo-name.git (push)
  1. If you don't see upstream, add it using
    git remote add upstream https://github.com/RepoOwners-Name/repo-name.git
    

Contributing to repository

  1. Now create a branch using

    git branch [branch-name]
    
  2. Make desired changes

  3. Stage the changes using

    git add .
    
  4. Commit the staged files using
    git commit -m "message-about-changes"
    
  5. Push the changes to origin repository using
    git push origin [branch name]
    

Making a Pull Request

  1. On GitHub, navigate to the main page of the repository.
  2. In the "Branch" menu, choose the branch that contains your commits. An images of branches listed on a repository in github
  3. Above the list of files, click Pull request An Image of pull request button on github
  4. Use the base branch dropdown menu to select the branch you'd like to merge your changes into, then use the compare branch drop-down menu to choose the topic branch you made your changes in. An image showing branches in a pull request on github
  5. Type a title and description for your pull request.
  6. To create a pull request that is ready for review, click Create Pull Request. To create a draft pull request, use the drop-down and select Create Draft Pull Request, then click Draft Pull Request. An Image of create a pull request button

Great!! You have forked and cloned a GitHub repository and have opened a pull request in it. Now, if the repository's owner find your changes useful, they will merge your pull request.

Conclusion

We have learned:

  1. Making a GitHub account.
  2. Creating a repository.
  3. Forking and cloning a remote repository.
  4. Configuring the remotes of the repository.
  5. Making and pushing changes to the repository.
  6. Creating a pull request in the repository.