Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post "Manage fork repository locally and remotely" article #44

Merged
merged 13 commits into from
Jun 10, 2024
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
title: "Manage fork repository locally and remotely"
summary: "TODO"
date: 2024-04-16T20:00:00-02:00
lastUpdate: 2024-06-03T23:04:17+0200
tags: ["git", "github"]
author: Zyno
ZynoXelek marked this conversation as resolved.
Show resolved Hide resolved
draft: false
---

## What are forks ?

Expand All @@ -9,88 +18,103 @@ On many repositories you will not have the write access. Yet, you can still prop

### How to make one

To make a fork from a repository you want to work on, nothing more simple :
You can simply go on the github repository page, an press the designated button!
To make a fork of a repository you want to work on, simply go on the github repository page, and press the designated button!

For instance, if you want to propose changes for our game [**Haunted Chronicles**](https://github.com/iScsc/Haunted-Chronicles), you can do it this way :
For instance, if you want to propose changes for our game [**Haunted Chronicles**](https://github.com/iScsc/Haunted-Chronicles), you can do it this way:

![](fork-creation-1.png)

They will ask to name the fork repository but it have no importance, you can keep the original name.
GitHub will ask you to name the fork repository but it have no importance, you can keep the original name.

![](fork-creation-2.png)

When it is done, you will have created a new repository on your own github :
When it's done, you will have created a new repository on your own GitHub account:

![](fork-creation-3.png)

We can see the original repository it has been forked from, and you can quickly access it through this link.

## Working with forks

Now that you have forked the repository you want to work on, you simply have to experiment on your fork!
Now that you have created your own fork, you simply have to experiment on your fork!

First, clone your fork on your computer to work on it with the link of your repository.
First, clone your fork on your computer with the link of your repository.

```git clone <link_to_your_fork>```
```
git clone <link_to_your_fork>
```

In this example, `<link_to_your_fork>` would be `[email protected]:ZynoXelek/Haunted-Chronicles.git` :
In this example, `<link_to_your_fork>` would be `[email protected]:ZynoXelek/Haunted-Chronicles.git`:
![](working-on-your-fork-1.png)
Using the ssh url first requires to have set up your SSH key with your github account. If you have not done it yet, you should probably read [Github - Create a SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) and [GitHub - add a new SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account) first.

> Using the SSH protocol first requires to have set up your SSH key with your github account. If you have not done it yet, you should probably read [Github - Create a SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) and [GitHub - Add a new SSH key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account) first.

Then, you can create a new branch by using the command :
```git branch <your_branch_name>```
```
git branch <your_branch_name>
```

You can then place your git `HEAD` on this new branch by using the command :
```git checkout <your_branch_name>```
```
git checkout <your_branch_name>
```

When this is done, you can then work on your code !
When this is done, you can start working on your code!

Once you have finished your work - or your session - don't forget to commit your changes after adding your new changes! To do so, first check the differences between your local repository and the remote one with ```git status```
Once you have finished your work - or your session - don't forget to commit your changes after adding your new changes! To do so, first check the differences between your local repository and the remote one with `git status` and `git diff`

Then, you can selectively chose which files you want to add to your commit with ```git add <file1> <file2> ...``` or you can add them all with ```git add .```
Then, you can selectively chose which files you want to add to your commit with `git add <file1> <file2> ...` or you can add them all with `git add .` (but you shouldn't, this is really bad practice).

If you have made a mistake, you can revert a `git add` by using `git reset <file1> <file2> ...` or `git reset` to unstage them all. This will keep your local changes.

Don't hesitate to use `git status` between each command to clearly see the actual state of your commit! You can also use the magic command `git log --graph --oneline --all --decorate` to see the actual state of your local and distant repositories in the nicest way!
Don't hesitate to use `git status` between each command to clearly see the actual state of your changes. You can also use the magic command `git log --graph --oneline --all --decorate` to see the actual state of your local and distant repositories in the nicest way!

Be careful, `git rm <file>` exists but it will permanently remove the file from the repository!

When you have finished staging all your changes, you can commit by using the `commit` command :
```git commit -m <your_commit_name>```
When you have finished staging all your changes, you can commit by using the `commit` command:
```
git commit -m <your_commit_name>
```
> Note that commits are intended to be meaningful **little** piece of code, generally you should create several commits per Pull Request and **avoid** fat commits
> As one says "commit early and commit often"

Note that this will just commit your changes on your local branch.
When you want to send these changes to the remote repository, you shall use the `push` command :

```git push origin <your_branch>```
When you want to send these changes to the remote repository, you shall use the `push` command:
```
git push origin <your_branch>
```

(Git commands are also integrated into some code editors such as `vs code`, but you should know that it may sometimes do some strange things, and using the terminal commands may make it easier to understand what really happens.)
> `git` commands are also integrated into some code editors such as `VS code`, but you should know that it may sometimes do some strange things, and using the terminal commands may make it easier to understand what really happens

## Pull requesting

When you have finished coding and you want to propose your changes to the origin repository, you will need to pull request.
When you have a first version of your code and you want to propose your changes to the origin repository, you will need to pull request.

To do so, you must first push your branch to the remote repository.
When it is done, simply go on the origin repository github page, and you will have the possibility to pull request your changes.
To do so, you must first push your branch to the remote repository.
When it is done, go on the origin repository GitHub page, and you will have the possibility to Pull Request your changes.

[Insert image here]
![](create-pull-request-1.png)
> You should see this banner on the GitHub page of your fork and the original repository, it doesn't matter
ctmbl marked this conversation as resolved.
Show resolved Hide resolved

In fact, you should definitely pull request **before** finishing your code. It helps with your work's transparency, but it also allow to receive feedback sooner, which could prevent working several hours on something that is wrong from scratch. You will be able to change your code immediately and it could prevent a lot of frustration on your side.
In fact, you should definitely Pull Request **before** finishing your code, and there is numerous reason to that.
It helps with your work's transparency, but it also allows you to receive feedback sooner, which could prevent working several hours on something that is wrong from scratch. You will be able to change your code immediately and it could prevent a lot of frustration on your side.
Similarly, when working on open source repositories, the owner have the complete right to refuse your change, even if you think your change is right and legitimate. Knowing that it will be refused whatsoever after working an hour on it is definitely easier to accept than after working weeks on your code.

And don't worry, you will be able to keep working on your code even after pull-requesting. You only have to mark your PR as a draft to tell your reviewers you are still working on it by clicking there :
And don't worry, you will be able to keep working on your code even after pull-requesting, just create other commits and push as we did before.

You could also mark your PR as a draft to tell your reviewers that you are still working on it by clicking there:

[Insert image here]
![](convert-to-draft-1.png)

## Update your fork

Once your pull request have been accepted on the remote origin repository, you may update your fork to the new state of the origin repository.
Once your Pull Request have been accepted on the remote origin repository, you may update your fork to the new state of the origin repository.

To do so, you shall :

* 1 - Go on your fork page on github and press the update fork button :
[Insert image here]
* 2 - Then, to update your local copy, you should use these 3 commands :
* 2.1 - `git fetch origin` To download the remote state of the repository to your local github.
* 2.2 - `git checkout main` To put your git `HEAD` on your `main` branch before pulling the changes.
* 2.3 - `git rebase origin/main` To move your local main to remote reference you have just downloaded.
* 1 - Go on your fork page on github and press the "Update branch" button:
![](sync-your-fork-1.png)
* 2 - Then, to update your local copy, you should use these 3 commands:
* 2.1 - `git fetch origin`: To download the remote state of the repository to your local github (you may now see it with the git log command we talked about above)
* 2.2 - `git checkout main`: To put your git `HEAD` on your `main` branch
* 2.3 - `git rebase origin/main`: To move your local main to remote reference you have just downloaded
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading