DEV Community

ShulyAvraham
ShulyAvraham

Posted on

OSDC Lesson 5:

Work locally on a project not owned by me

git clone git@github.com:ShulyAvraham/LIMS_results_validation.git
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

It is advised to add my username in case someone else will use the same name.

git add requirements.txt
Enter fullscreen mode Exit fullscreen mode
git commit -m'Add requirements file #2'
Enter fullscreen mode Exit fullscreen mode

It is advised to add the issue number to the commit message.

git log
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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 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 code->ssh->Copy the URL

git remote add <any-name> <the-fork-url-in-github>
Enter fullscreen mode Exit fullscreen mode
git remote add fork git@github.com:ShulyAvraham/osdc-site-generator.git
Enter fullscreen mode Exit fullscreen mode

It adds the mapping to the .git/config file

git push --set-upstream fork <the-fork-url-in-github>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If I want to work on something else

git checkout -b <another-branch-name>
Enter fullscreen mode Exit fullscreen mode

See all the branches

git branch -a
Enter fullscreen mode Exit fullscreen mode
git remote set-url origin --push <the remote git project not mine> 
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Will show us graphically the changes

git fetch
Enter fullscreen mode Exit fullscreen mode

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

gitk --all
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Merge takes both changes, remote and local and merges them.

Rebase

[pull]
    rebase=true
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)