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:
Verify that we all have a working GitHub account using our school email address
Push our sample repository from Lesson 6 to our GitHub account for safe-keeping
Verify that it has worked, make changes online, and pull those changes back down
Clone ("Fork") a class repository from GitHub Classroom
Make modifications on the development branch
Merge the changes on the main branch
Submit the work by pushing the main branch to GitHub
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:
git config --global credential.helper store
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
Give it a name (I recommend "Using Git")
Mark it as public or private (for this lesson, I recommend private)
Click on Create 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.
Open a new tab and go to your GitHub settings:
Profile Icon > Settings > Developer SettingsGenerate a new classic token:
Personal access tokens > Tokens (classic) > Generate new token > Generate new token (classic)Give it a name (I recommend "Git Access")
Set Expiration to something like 90 days. We'll be moving to VSCode by then.
Click the checkbox next to "repo"
Scroll down and click Generate token
Your new token will be shown to you - DO NOT CLOSE THIS TAB YET! We need that token.
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!
Go and copy the personal access token to your clipboard.
In the terminal, switch to the main branch: git checkout main
Push this branch to the remote and set it for tracking with the -u option: git push -u origin main
(replace origin with whatever name you gave the remote)
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)
If you have other branches, do the same with them:
git checkout dev
git push -u origin dev
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 branch: git 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