Git Contribute

Add to Someone Else’s Repository

At the heart of Git is collaboration. However, Git does not allow you to add code to someone else’s repository without access rights.

In these next 3 chapters we will show you how to copy a repository, make changes to it, and suggest those changes be implemented to the original repository.

At the end of these chapters, you will have the opportunity to add a message to our public GitHub page: https://learnkro-test.github.io/


Fork a Repository

fork is a copy of a repository. This is useful when you want to contribute to someone else’s project or start your own project based on theirs.

fork is not a command in Git, but something offered in GitHub and other repository hosts. Let’s start by logging in to GitHub, and fork our repository:
https://github.com/learnkro-test/learnkro-test.github.io

GitHub Fork

Now we have our own copy of learnkro-test.github.io:

GitHub Fork Complete

Now let’s look at how we add a local copy of this for us to work with.

Clone a Fork from GitHub

Now we have our own fork, but only on GitHub. We also want a clone on our local Git to keep working on it.

clone is a full copy of a repository, including all logging and versions of files.

Move back to the original repository, and click the green “Code” button to get the URL to clone:

GitHub clone URL

Open your Git bash and clone the repository:

Example

git clone https://github.com/learnkro-test/learnkro-test.github.io.git
Cloning into 'learnkro-test.github.io'...
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 33 (delta 18), reused 33 (delta 18), pack-reused 0
Receiving objects: 100% (33/33), 94.79 KiB | 3.16 MiB/s, done.
Resolving deltas: 100% (18/18), done.

Take a look in your file system, and you will see a new directory named after the cloned project:

Example

ls
learnkro-test.github.io/

Note: To specify a specific folder to clone to, add the name of the folder after the repository URL, like this: git clone https://github.com/learnkro-test/learnkro-test.github.io.git myfolder

Navigate to the new directory, and check the status:

Example

cd learnkro-test.github.io
git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

And check the log to confirm that we have the full repository data:

Example

git log
commit facaeae8fd87dcb63629f108f401aa9c3614d4e6 (HEAD -> master, origin/master, origin/HEAD)
Merge: e7de78f 5a04b6f
Author: learnkro-test 
Date:   Fri Mar 26 15:44:10 2021 +0100

    Merge branch 'master' of https://github.com/learnkro-test/hello-world

commit e7de78fdefdda51f6f961829fcbdf197e9b926b6
Author: learnkro-test 
Date:   Fri Mar 26 15:37:22 2021 +0100

    Updated index.html. Resized image
    
.....

Now we have a full copy of the original repository.


Configuring Remotes

Basically, we have a full copy of a repository, whose origin we are not allowed to make changes to.

Let’s see how the remotes of this Git is set up:

Example

git remote -v
origin  https://github.com/learnkro-test/learnkro-test.github.io.git (fetch)
origin  https://github.com/learnkro-test/learnkro-test.github.io.git (push)

We see that origin is set up to the original “learnkro-test” repository, we also want to add our own fork.

First, we rename the original origin remote:

Example

git remote rename origin upstream
git remote -v
upstream        https://github.com/learnkro-test/learnkro-test.github.io.git (fetch)
upstream        https://github.com/learnkro-test/learnkro-test.github.io.git (push)

Then fetch the URL of our own fork:

GitHub clone URL

And add that as origin:

Example

git remote add origin https://github.com/kaijim/learnkro-test.github.io.git
git remote -v
origin  https://github.com/kaijim/learnkro-test.github.io.git (fetch)
origin  https://github.com/kaijim/learnkro-test.github.io.git (push)
upstream        https://github.com/learnkro-test/learnkro-test.github.io.git (fetch)
upstream        https://github.com/learnkro-test/learnkro-test.github.io.git (push)

Note: According to Git naming conventions, it is recommended to name your own repository origin, and the one you forked for upstream

Now we have 2 remotes:

  • origin – our own fork, where we have read and write access
  • upstream – the original, where we have read-only access

Now we are going to make some changes to the code. In the next chapter, we will cover how we suggest those changes to the original repository.

Push Changes to Our GitHub Fork

We have made a lot of changes to our local Git.

Now we push them to our GitHub fork:

commit the changes:

Example

git push origin
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 16 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 393.96 KiB | 32.83 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/kaijim/learnkro-test.github.io.git
   facaeae..ebb1a5c  master -> master

Go to GitHub, and we see that the repository has a new commit. And we can send a Pull Request to the original repository:

GitHub Fork Pull Request

Click that and create a pull request:

GitHub Fork Create Pull Request

Remember to add an explanation for the administrators.

GitHub Fork Create Pull Request Comment

Pull Request is sent:

GitHub Fork Pull Request Sent

Approving Pull Requests

Now any member with access can see the Pull Request when they see the original repository:

GitHub New Pull Request

And they can see the proposed changes:

GitHub Pull Requests Overview

Comment on the changes and merge:

GitHub Pull Request Merge

Confirm:

GitHub Pull Request Confirm Merge

And changes have been merged with master:

GitHub Pull Request Merged

Now you try!

Leave a Comment