7 - Version Control Part 2

Local version control and branches is essential for a good workflow. But what if you wanted to collaborate with other developers or save your project online so that it can be transferred between computers?

What are we learning?

In this lesson we will:


Prepare for Today's Lesson

Make sure you're caught up by reviewing Lesson 6. Work with a classmate if you need assistance.

1. Configure Git to Store Credentials

By default, Git will ask for a username and password every time we connect to GitHub. To stop this, we will ask Git to store our credentials:

2. Create a Remote Repository on GitHub

Go to GitHub.com and with your @stu.ocsb.ca account or create a new GitHub account using your school email.

Once you're logged-in to GitHub, create a new repository

After creation, you will be given the web URL for this repository. We're going to need that for later! Leave this tab open.

3. Create a Personal Access Token

Git does not allow passwords for remote connections - it is not considered secure enough. Instead, we have to create an access token in GitHub.

4. Connect Git to the Remote

The repo we just made in GitHub is called the remote repo because it is not on our computer. The folder on our computer is called the local repo. We will connect our local to the remote by using the command:
git remote add origin https://github.com/remote_repo/URL.git

In that command, we are giving the remote repository the name "origin". That is a historical standard. Technically, you can call it something more meaningful like "github".

5. Push Local to Remote

We are now ready to push (upload) our main branch to the remote repo. We are going to need the personal access token we created!

You will be asked for your username - type in your GitHub username perfectly
You will then be asked for your password - paste the Personal Access Token here! (you won't see what you paste)

Congratulations! You've connected Git to GitHub!

6. Pushing Local Changes

You can continue to make changes, add new files and work normally inside your terminal.

To push (upload) updates to GitHub (the remote), you need to commit all your local changes in the branch you want to push.
git commit -a -m "My awesome commit message"

Then use the push command to update GitHub:  git push <remote name> <branch>
Examples: git push origin dev

git push github main

If you want to push all changes in all branches, they need to all be committed first and then run this command:
git push --all

7. Pulling Remote Changes

Sometimes you make changes from another computer or in GitHub. That's ok, we can pull (download) those down to our local repo.

MAKE SURE YOU DIDN'T MODIFY THE FILE(S) IN BOTH LOCATIONS!

First, check to see what sort of changes we might have:
git fetch --all

Then use the pull command to update your current branchgit pull <remote name> <branch>
Examples: git push origin dev
git push github main

If you want to push all changes in all branches, they need to all be committed first and then run this command:
git push --all