OSDC Lesson 5:
Tags: opensource, osdc
Published: 2023-02-05 13:15:47

---
series: OSDC
---
# Work locally on a project not owned by me
```
git clone git@github.com:ShulyAvraham/LIMS_results_validation.git
```

We see that there's an issue, the file *requirements.txt* is missing.

We can create it. Edit it to add the missing modules. In this case we're missing the `pyinstaller` module. We'll add it to the file according to the instructions. 

## Create a new branch
```
git checkout -b <username>/<branch-name>
```
It is advised to add my username in case someone else will use the same name.

```
git add requirements.txt
```

```
git commit -m'Add requirements file #2'
```
It is advised to add the issue number to the commit message.
```
git log
```

Git doesn't know where to push because the new branch is not mapped to remote, so the push needs to include the remote and the branch name.
```
git push origin <username>/<branch-name>
```
But it doesn't work because I don't have permissions on the project. Hence, I need to create my own fork of the project in my git.

I'll go to the [project's git](https://github.com/OSDC-Code-Maven/osdc-site-generator) and create a fork on my own git by clicking `fork`->`create a new fork`-> I'll be the owner of the form -> `Create fork`

So now I have the project in my own user's git, on which I have write permissions.

Now I need to add the remote of my fork

I'll got to [my fork](https://github.com/ShulyAvraham/osdc-site-generator) `code`->`ssh`->`Copy the URL`
```
git remote add <any-name> <the-fork-url-in-github>
```
```
git remote add fork git@github.com:ShulyAvraham/osdc-site-generator.git
```

It adds the mapping to the `.git/config` file
```
git push --set-upstream fork <the-fork-url-in-github>
```
The flag `--set-upstream` adds to the `.git/config` lines that tell git to map this branch to the remote fork

The changes were pushed to my fork. 

Going to the git website, 

Gabor doesn't know about my changes. In order for him to see it, I need to create a pull request.

I go to the project on the GitHub site, and create a pull request

Gabor, on his project now sees the `Pull request`.

If Gabor is not happy with my changes, he can ask to fix it. Now every change that I will make to the branch and push out, will create a new pull request.



## Pull request
`Merge ...`
Select one of 3 options... 
* `Commit and merge` - 
* `Squize and merge` - will squeeze all Gabor's commits to one
* `Rebase and merge` - 

Now I need to sync my local git with the remote main git that merged my changes.
```
git checkout main
git pull
```

If I want to work on something else
```
git checkout -b <another-branch-name>
```

See all the branches
```
git branch -a
```

```
git remote set-url origin --push <the remote git project not mine> 
```

## Merge and rebase
Are related to the behavior of `git pull`

The result of the merge and rebase are the same, it's only the commits history that is different.

I will make a local change on my local main branch.
```
git add
git commit -m'add comment'
```
Now I will accept a pull request which someone sent me a pull, and the change reached the main branch.

Now locally I have a change. I cannot push out, as what's in GitHub already changed. 

I need to locally merge the remote changes with my local changes.

First I need to `git pull` in order to merge changes on the remote with my local changes. 

```
gitk --all
```
Will show us graphically the changes

```
git fetch
```
Gets the changes from remote to my local copy, but will not merge with my local changes.

```
gitk --all
```

How to mix the two?
* Merge
* Rebase

Pull can either merge or rebase.

We can look at `.git/config` to see the behavior.

Merge
```
[pull]
    rebase=false
```
Merge takes both changes, remote and local and merges them.

Rebase
```
[pull]
    rebase=true
```

Normally git pull first does fetch and then merge.

The rebase takes my local changes and places them after the changes on the remote git. Only for the commits history purpose.