changed 3 months ago
Published Linked with GitHub

GitHub

Wojciech Hardy

link: https://hackmd.io/@WHardy/RR25-git3


Let's take a look at GitHub

github.com


Notably, it's not only GitHub

GitHub - think: Microsoft, tons of repositories managed remotely, mostly for open repositories.

GitLab - think: DevOps and CI/CD.
(But people list various differences)


Atlassian's BitBucket - think: private firm solutions and Jira.

Etc. We'll use GitHub as an example.


Working with GitHub

There's several ways you can interact with GitHub. Let's cover a few cases:

  1. Start with a GitHub repository, create a local version.
  2. Start with a local repository, put it on GitHub.
  3. Fork someone's repository on GitHub, create a local version, grab updates from the original version.
  4. Use GitHub Desktop (or another tool)
  5. Run an RStudio project connected to GitHub.
  6. etc.

Happy Git With R has nice step-by-step guides - not only about R and GitHub. Check it out.


Password, authentication, etc.

We might have to go through some connection issues on our first go. Here's a good reference.

In principle, Git might ask you for authentication when you first connect with GitHub from your computer. It might be enough to log in via browser (depends on git version).

But if it asks for username and password, note that the 'password' might refer to a Personal Access Token.

How to generate one?

  1. On GitHub, go: Account settings -> Developer settings -> Personal Access Tokens -> Generate new token.

  2. Check user, workflow and repo and it should cover most basic usage. Copy the token and place it in the prompt instead of the password.


Case 1. GitHub to local

  1. Go to GitHub and create a new repository.

Click the button at GitHub.

Let's start with a Readme.md file (check the relevant box).

This will initiate a non-empty repository.


  1. Copy the HTTPS URL.

At your Repository, click the green code button and copy the HTTPS URL.

Example:


  1. Prepare some space (a folder) for your repository and clone it.

mkdir repositories
cd repositories
git clone <copied_url>


  1. Do some checking

(e.g.:
cd MyRepoName
ls
git status
head README.md
git remote show origin
)


  1. Create a simple .txt file. Stage it, commit it, put it back in the remote repository.

echo "some text" > hello.txt
git add hello.txt
git commit -m "Added a hello.txt file"
git push

Note: this might be the moment when you run into an authentication request.


  1. Check the repository on GitHub.

Refresh the page. Your hello.txt file should appear.


Case 2. Local to GitHub

  1. Create a new repository on your local machine.

cd ..
mkdir Ex2Repo
cd Ex2Repo
git init


  1. Create a simple .txt file. Stage it and commit it.

echo "some text" > hello.txt
git add hello.txt
git commit -m "Added a hello.txt file"


  1. Go to GitHub and create a new (empty!) repository.

Click the button at GitHub.

Don't start with any of the standard files! You will then initiate a repository without a commit history (avoidng a conflict of two unrelated commit histories).


  1. Copy the clone URL.

At your bare repository, click the green code button and copy the HTTPS URL.

Example:


  1. Set up a connection with the remote repository.

git remote add origin <copied_url>


  1. Push your repository contents.

git branch -M main will rename the master branch to main
git push -u origin main

Note: you have to set the upstream for your branch (you can also do it with --set-upstream)


Case 3. Forking and getting updates

  1. Choose an existing GitHub repository (someone else's!) and fork it.

Example (press the "Fork" button):


  1. Clone the fork to a local repository.

Repeat Case 1 steps for your new repository.


  1. Doing pull requests

In your local repository create a new file, stage it, commit it and push it.

In GitHub you should see you're '1 commit ahead'. Try opening a pull request (you don't need to follow through).


  1. If there's an edit to the repository you forked -> execute a pull

We need to establish a link:

a) Check the configured remote repo.

git remote -v

b) Add a path to the forked remote repo.

git remote add newUpstream <copied_url>

c) Check again.

git remote -v


  1. Fetch the information from the new upstream.

git fetch newUpstream


  1. Merge with the new information. Then push it back to your GitHub repository.

git merge newUpstream/main


Case 4. Using e.g. GitHub for Desktop

  1. Install and log in.
  2. Try replaying what you did in previous cases:
  • clone a repository from GitHub.
  • put a local repository on GitHub.
  • check how to stage, commit, push, pull, merge, etc.

(it's just clicking on names you already know).


Case 5. Using GitHub with RStudio

  1. Open RStudio.
  2. Create a new project with version control (Git).

File -> New project -> Version control -> Git

  1. Use the URL for one of your repositories at GitHub.
  2. Check how to stage, commit, push and pull.

You should now have a "Git" tab in top-right corner. Check it out.

  1. Use the terminal for more nuanced operations.

You can use the terminal the same way you used Bash.


ASSIGNMENT

  1. Register at GitHub (or use an existing account).
  2. Fork the course repository.
  3. If your GitHub nickname allows me to identify who you are in USOS, move to point 4. If not, please send me an e-mail to let me know who you are at GitHub - thanks!
  4. Clone your fork repository to your local computer, so you can work on it (use the option you prefer).
  5. Go to the "Assignments/Assignment 3" folder, edit the hello.txt file inside (e.g. write the date inside, or whatever you want to).
  6. Stage the change, commit it and push it.

That's it. That's the assignment.


Contributing to another repository

Sometimes you'll want the owner of the original repository to include your changes. E.g.:

  1. you think you've upgraded the original project.
  2. you're part of a team and the process of updating a central repository involves reviews, etc.

Go to your forked repository. You should see the information that you're ahead of the repository that you forked.


ASSIGNMENT: Initiating a pull request

In your repository you can grab the changes from its source. But you can also try to pass your commits to it.

You can do so through the pull requests tab or by clicking the contribute button.

Do it now with the course repository.

Select a repo