diff --git a/1-git-basics-slides.html b/1-git-basics-slides.html index 8174301..b86ea19 100644 --- a/1-git-basics-slides.html +++ b/1-git-basics-slides.html @@ -13,89 +13,171 @@ /* buutti.css */ /* @theme buutti */div#\:\$p>svg>foreignObject>section .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns12{display:grid;grid-template-columns:1fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns21{display:grid;grid-template-columns:2fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns32{display:grid;grid-template-columns:3fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns23{display:grid;grid-template-columns:2fr 3fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns111{display:grid;grid-template-columns:1fr 1fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .centered{display:flex;flex-direction:column;justify-content:center;text-align:center}div#\:\$p>svg>foreignObject>section .tableborderless td,div#\:\$p>svg>foreignObject>section th{border:none!important;border-collapse:collapse}div#\:\$p>svg>foreignObject>section.extra{background-color:#5d275d;background-image:linear-gradient(to bottom,#401a40,#1d0c1d);color:white}div#\:\$p>svg>foreignObject>section.extra a{color:rgb(145,255,209)}div#\:\$p>svg>foreignObject>section.exercise{background-color:#29366f;background-image:linear-gradient(to bottom,#20636a,#173742);color:white}div#\:\$p>svg>foreignObject>section.exercise a{color:rgb(211,173,255)} -/* @theme ldpmc9yhmkn5tf04e6isl4g2ad1vz1nu72znsmr9zdkd */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}
+/* @theme hx2bjmr0u38rtagpqfn6g0kd17u6g0pxbu23gu02oxd */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}

Project management 1. Git basics

-
-

Basic Git workflow

-
    -
  • You have to tell everything you want to happen to Git explicitly by using Git commands
  • -
  • A common Git workflow consists of these four steps:
  • -
-
    -
  1. Edit: You make changes in some code file
  2. -
  3. Stage: You tell Git what files you've changed (git add)
  4. -
  5. Commit: You tell Git what changes you made in that file (git commit)
  6. -
  7. Push: You upload those changes to a remote repository (git push)
  8. -
-
-
+

Repositories

    -
  • Repository is essentially a place to store code files in
  • +
  • Repository ("repo") is essentially a place to store code files in
  • Usually, we are dealing with two repositories: a local and a remote one
    • A local repository is the one on your computer
    • A remote repository is stored in a cloud service like GitHub
    • -
    • Here is the remote repository for the materials of this course
    • +
    • Here is an example of a remote repository on GitHub
    • Changes are synced between these two repositories manually
-
-

git init

+
+

Git commands

    -
  • Initialization needs to be done for every new Git repository
  • -
  • There are two ways to initialize a repo: +
  • You have to tell everything you want to happen to Git explicitly by using Git commands
  • +
  • Commands are of the form git commandname
  • +
  • Some commands include handles that give extra modifiers to the commands: +
      +
    • git commandname --handle or a shorthand git commandname -h
    • +
    +
  • +
+
+
+

Getting started with GitLab

    -
  • a) Creating the local repository first: +
  • Go to www.gitlab.com and create a new account, if you don't already have one
  • +
  • If you have an existing GitHub account, you can use it to log into GitLab
      -
    • Use git init to make your current folder a new Git repository
    • +
    • (You'll need to set a password to GitLab after logging in)
  • -
  • b) Creating the remote repository first / continuing an existing project: +
  • After logging in, you can create a new project
      -
    • Clone a remote repository into your computer (See: git clone)
    • +
    • This is what GitLab calls repositories
  • +
  • In GitLab, projects are located in groups or namespaces
  • +
  • You should have separate repositories for different programs +
      +
    • We will store future home assignments in a single repository
    • +
    • Larger projects will have their own repositories
  • -
  • Note: Never initialize a Git repository in a cloud storage folder!
-
-

.git folder

+
+

Creating a repository

+
    +
  1. In GitLab, New Project > Create blank project
  2. +
  3. Give a name to the project, which automatically becomes its URL (see: Project slug)
  4. +
  5. Use your own user namespace (Good for personal projects)
  6. +
  7. You can set project visibility to Private (only you can see it) or Public (anyone can see it)
  8. +
  9. If you want, you can initialize the repository with a README.md file +
      +
    • It's a Markdown file, essentially a text file containing information about the project.
    • +
    • Note: These lectures are also written in Markdown, and you can use Markdown syntax to format text on Discord. It's neat!
    • +
    +
  10. +
+
+
+
+

+
+
+
+

Initializing a repository

+
    +
  • Initialization needs to be done for every new Git repository
  • +
  • There are two ways to initialize a repo: +
      +
    • a) Creating the remote repository first / continuing an existing project: +
        +
      • Clone a remote repository into your computer (See: git clone)
      • +
      +
    • +
    • b) Creating the local repository first with git init
    • +
    +
  • +
  • Note: Never initialize a Git repository in a cloud storage folder!
  • +
+
+
+

.git folder

  • Initialization adds a hidden .git folder inside a directory
    • This is where Git stores all its repository data.
    • -
    • Do not touch it!
    • +
    • Do not touch it! Let Git handle its contents on its own.
  • -
  • The .git folder ONLY appears in the root directory of the repository +
  • The .git folder ONLY appears in the root directory of the repository +
      +
    • If you see the .git folder in any of the subdirectories of the repository, something has gone wrong!
        -
      • If you see the .git folder in any of the subdirectories of the repository, something has gone wrong!
      • -
      • You have initialized a repository inside repository
      • +
      • You might have initialized a repository inside repository
      • +
      • This should never happen!
      • +
      +
-
width:
-

git status

+
+

git clone

+
    +
  • Remote repositories have an URL address that can be used to download the code to your machine +
      +
    • The address can be found under the Clone button in the repository webpage
    • +
    +
  • +
  • Use the URL to download a remote repository: +
      +
    • Run git clone <URL>
    • +
    • Example: git clone https://github.com/borbware/unity-basics-course.git
    • +
    • Remember: this initializes the repo, so git init not needed.
    • +
    +
  • +
  • Simplest way to create a new, empty repository: +
      +
    • 1 - Initialize the repo in the Github/Gitlab/etc website
    • +
    • 2 - Clone the repo.
    • +
    +
  • +
+
+
+

Exercise 1a. Cloning a repo

+ +

Create a new repository on GitLab and clone it with HTTPS to your machine.

+
+
+

Basic Git workflow

+
    +
  • A common Git workflow consists of three steps:
  • +
  • These are the steps you have to go through every time you want Git to know about the changes you've made!
  • +
  • So, after writing some changes, you do this:
  • +
+
    +
  1. Stage: You tell Git what files you've changed (git add)
  2. +
  3. Commit: You tell Git what changes you made in that file (git commit)
  4. +
  5. Push: You upload those changes to a remote repository (git push)
  6. +
+
+
width:
+

Note: git status

    -
  • Using the command git status shows your current situation.
  • +
  • When inside a repository, using the command git status shows your current situation.
  • If you're unsure what to do, it's never a bad idea to run git status
-
-

git add

+
+

git add

    -
  • Uploading changes to GitHub from your local machine takes three steps (see Basic Git workflow from earlier)
  • -
  • The first phase is staging with git add: +
  • The first step is staging with git add:
      -
    • The command git add readme.md stages the file readme.md
    • -
    • After doing it, git status tells this:
      Changes to be committed:
      +
    • The command git add readme.md stages the file readme.md
    • +
    • After doing it, git status tells this:
      Changes to be committed:
         (use "git restore --staged <file>..." to unstage)
               modified:   readme.md
       
      @@ -104,50 +186,56 @@
-
+
    -
  • Basically, with git add file you tell Git that you want to do something with file.
  • -
  • Note: If you just want to push all the changes you've made, you can use git add . to stage all the files in your current folder and its subfolders +
  • With git add file, you tell Git that you have done something to file, and want to record those changes into Git history.
      -
    • Be careful! Only do this after checking what changes have been made with git status
    • +
    • This doesn't record those changes yet! This is just a preparation step.
    • +
    +
  • +
  • To undo git add readme.md, use git restore --staged readme.md
  • +
  • Note 1: Use git add <filename> -p if you only want to stage some lines from a file you've worked on.
  • +
  • Note 2: You can use git add . to stage all the files in your current folder and its subfolders at once +
      +
    • This is a neat way to shoot yourself in the foot and stage something you didn't intend to!
  • -
  • Note 2: Use git add <filename> -p if you only want to stage some lines from a file you've worked on.
  • -
  • Note 3: The counterpart to git add is git rm, which removes previously added files from Git's perspective
-
-

git commit

+
+

git commit

    -
  • After you have staged all the files you want, the second step is to commit your changes +
  • After you have staged all the files you want, the second step is to commit your changes
  • +
  • In the commit, you will explain what changes you've made
      -
    • In the commit, you will explain what changes you've made
    • -
    • This message will be visible in GitHub
    • -
    • git commit -m "add new enemy"
    • +
    • This message will be visible in the Git history
    • +
    • git commit -m "add new feature"
    • +
    • Commit message should clearly, concisely tell what kind of changes you have made
    • +
    • Prefer present tense ("Do x" instead of "Did x")
  • -
  • Every new commit creates a new point in the project timeline. +
  • Every new commit creates a new point in the project timeline.
      -
    • You can always jump back between different points (See git checkout)
    • +
    • You can always jump between different points in timeline (See git checkout)
  • -
  • Commit message should clearly, concisely tell what kind of changes you have made
-
-

git push

+
+

git push

    -
  • The third step is pushing the changes to GitHub
  • -
  • The previously-made commit action is local +
  • The third step is pushing the changes to a remote repository
  • +
  • The previously-made commit action is local
    • No changes have gone to the remote repository yet!
    • To upload changes to the remote repository, use git push
    • -
    • Then you're done!
    • +
    • Then you're done, and you can check your changes are visible in the remote repository
-
+
+

Setting upstream

  • Note: When pushing for the first time, Git might nag you:
    fatal: The current branch master has no upstream branch.
     To push the current branch and set the remote as upstream, use
    @@ -159,109 +247,100 @@ To push the current branch and set the remote as upstream, use
     
  • Generally, when Git gives you a warning, error, or some other message, it's a good idea to read it and follow the orders.
-
+
+

Exercise 1b. Pushing changes

+ +

Continue the previous exercise.

+
    +
  1. In your local repository, create a new file GitTest.md with some lines of text in it.
  2. +
  3. Commit and push changes to GitLab.
  4. +
  5. Go to GitLab and see that the file is there!
  6. +
+
+
+

git init

+
    +
  • git clone always creates a new folder for a project
  • +
  • If we have started working on a project without using Git, and have a project folder locally, it might be easier to use git init to initialize the project instead
  • +
  • To do so, navigate to your project folder in the command line, and use git init to make your current folder a new Git repository
  • +
+
+

git remote

    -
  • If you have initialized a Git repo already with git init, don't use git clone! +
  • Now, your local repo needs to be connected to a remote repository +
      +
    • Create an empty remote repo with no Readme file, so your remote repository doesn't clash with your local one.
    • +
    +
  • +
  • If you have initialized a Git repo with git init, you can't use git clone to connect your local repo to a remote one
      -
    • Instead, run git remote add origin <URL>
    • -
    • Here, the name origin refers to the remote repository
    • -
    • origin is just the default name for a remote: other names can be used instead!
    • +
    • Instead, run git remote add origin <URL>
    • +
    • Here, the name origin refers to the remote repository
    • +
    • origin is just the default name for a remote: other names can be used as well
  • -
  • To check which remote repository the current local repository is linked to, use +
  • To check which remote repository the current local repository is linked to, use
      -
    • git remote -v
    • +
    • git remote -v
A repository can have multiple remotes. Use git remote to list them all.
-
-

Exercise 1a. Initializing a repo

+
+

Exercise 2. Initializing a repo the local way

-
    -
  • Initialize a new repository locally.
  • +
      +
    1. Initialize a new repository locally with git init.
    2. Create a new file GitTest.md with some lines of text in it.
    3. Commit changes.
    4. Create a new empty repository on GitHub.
    5. Use git remote add origin <url> to connect your local repo to the GitHub repo.
    6. Push changes to GitHub. Go to GitHub and see that GitTest.md is there!
    7. -
+
-
+

git pull

  • git pull applies changes from the remote repository into the local repository
      -
    • Counterpart to push
    • +
    • Counterpart to push, but does actually two commands in one
    • +
    • It's the same thing as doing git fetch <branch>, and then git merge <branch>"
  • -
  • Very common when working in a team +
  • Using git pull is very common when working in a team
      -
    • Note: During teamwork it's a good idea to always pull before pushing
    • +
    • Note: During teamwork it's a good idea to always pull before pushing
  • -
  • If you only work alone on a single computer, seldom needed
  • +
  • If you only work alone on a single computer, seldom needed
-
git pull is actually two commands in one. It's the same thing as doing git fetch <branch> + git merge <branch>
-
-

git clone

-
    -
  • Remote repositories have an URL address that can be used to download the code to your machine -
      -
    • The address can be found under the Clone button in the repository webpage
    • -
    -
  • -
  • Use the URL to download a remote repository: -
      -
    • Run git clone <URL>
    • -
    • Example: git clone https://github.com/borbware/unity-basics-course.git
    • -
    • Remember: this initializes the repo, so git init not needed.
    • -
    -
  • -
  • Simplest way to create a new, empty repository: -
      -
    • 1 - Initialize the repo in the Github/Gitlab/etc website
    • -
    • 2 - Clone the repo.
    • -
    -
  • -
-
-
-

Exercise 1b. Cloning a repo

- -
    -
  • Create a new repository on GitHub and clone it with HTTPS to your machine.
  • -
  • Then, create a new file GitTest.md with some lines of text in it.
  • -
  • Commit and push changes to GitHub.
  • -
  • Go to GitHub and see that the file is there!
  • -
-
-
+

Extra: HTTPS vs SSH

  • There are two ways to communicate with GitHub, HTTPS and SSH
  • HTTPS: https://github.com/borbware/unity-basics-course.git
      -
    • the easier way
    • -
    • you log in with your GitHub credentials that get stored in the Git Credential Manager
    • +
    • The easier way
    • +
    • You log in with your GitLab credentials that get stored in the Git Credential Manager
  • SSH: git@github.com:borbware/unity-basics-course.git
-
+

.gitignore

    -
  • Sometimes your project has local files that should NOT be uploaded to GitHub +
  • Sometimes your project has local files that should NOT be uploaded to GitLab
    • List those files in a .gitignore file in your Git project folder
    @@ -271,14 +350,14 @@ To push the current branch and set the remote as upstream, use *.html
  • -
  • Note: If you add a file to .gitignore that was committed earlier, Git doesn't "forget" it automatically. +
  • Note: If you add a file to .gitignore that was committed earlier, Git doesn't "forget" it automatically
    • You can make Git forget it with git rm --cached filename
-
width:
+
width:

VS code: Source control

  • The source control tab is very useful for managing your Git workflow
  • @@ -297,45 +376,54 @@ To push the current branch and set the remote as upstream, use
  • Press "" to discard the changes made to the file (be careful!)
-
-
-

Exercise 2. Git collaboration

+
+
+

Exercise 3. Git collaboration

+

Work on this exercise with your group.

+
    +
  1. Choose someone's test repository from Exercise 1a to use in this exercise.
  2. +
  3. While others clone the repo to their machine, the owner should add other group members as collaborators of the repo in the repository's settings on GitLab.
      -
    • Work on this exercise with your group.
    • -
    • Choose someone's test repository from Exercise 1b to use in this exercise.
    • -
    • While others clone the repo to their machine, the owner should add other group members as collaborators of the repo in the repository's settings on GitHub.
    • -
    • Then, everyone should make changes to the markdown file in the repository!
    • +
    • Left sidebar: Manage > Members
    • +
    • Click Invite members and add others as Developer or Maintainer.
    +
  4. +
  5. Then, everyone should make changes to the markdown file in the repository!
  6. +

What happens if you make changes to the same line simultaneously?

-
In GitLab, the setting is under Manage > Members
-
-
-

Troubleshooting: master vs. main

-
error: failed to push some refs to [your-url]
-
-
    -
  • Possible reason: GitHub has a main branch, while your local repository has master
  • -
  • Fix: Run git branch -m main to rename your local master to main
  • -
-
+

Note about longer push and pull commands

    -
  • After initialization, why do I have to use longer push and pull commands? +
  • After initialization, why don't push and pull commands work? +
      +
    • Answer: because when you don't clone the repo, but add the remote into an empty repo with git remote add origin, the remote and local branches aren't connected yet.
        -
      • Short answer: because we didn't clone the repo, but rather added the remote into an empty one with git remote add origin.
      • -
      • git pull origin main: After adding a remote, we don't yet know which branch to pull from. main (or master!) is the default branch
      • -
      • git push --set-upstream origin main: In the first push, we decide which branch to link our current branch into.
      • -
      • Afterwards, we can just use git push and git pull.
      • +
      • Thus, Git doesn't yet know which branch to pull from
      • +
      • You can decide it manually on each pull with git pull origin main
    • +
    • The branches can be connected in the first push with git push --set-upstream origin main
    • +
    • Afterwards, we can just use git push and git pull.
    • +
    +
  • +
+
+
+

Troubleshooting: master vs. main

+
error: failed to push some refs to [your-url]
+
+
    +
  • Possible reason: GitLab has a main branch, while your local repository has master
  • +
  • Fix: Run git branch -m main to rename your local master to main
-
+

Reading