3. How do I use git offline

3.1. Todays Goals

  • just enough bash

  • offline git basics

  • practice with issues as something to do while we work with git offline

3.3. Getting Set up Locally

Opening different terminals

  • default terminal on mac, ue the bash command to use bash (zsh will be mostly the same; it’s derivative, but to ensure exactly the same as mine use bash)

  • use gitbash on Windows

To change directory

cd path/to/go/to

To make a directory (folder) for things in this course (or in my case for inclass time)

mkdir sysinclass

Then we have to cd into that new folder:

cd sysinclass/

To view where we are, we print working directory

pwd

View files

ls

It’s empty for now, but we will change that soon.

3.4. Using Git and GitHub locally

3.4.1. Authenticating with GitHub

There are many ways to authenticate securely with GitHub and other git clients. We’re going to use easier ones for today, but we’ll come back to the third, which is a bit more secure and is a more general type of authentication.

  1. GitHub CLI: enter the following and follow the prompts.

    gh auth login
    
  2. personal access token. This is a special one time password that you can use like a password, but it is limited in scope and will expire (as long as you choose settings well)

  3. ssh keys

3.4.2. Cloning a repository

Cloning a repository makes a local copy of a remote git repository.

We can clone in two different ways, with git only or with the GitHub CLI tools.

with the GitHub CLI:

gh repo clone introcompsys/github-in-class-brownsarahm

with git only:

git clone https://github.com/introcompsys/github-in-class-brownsarahm.git

Important

the git only version can be used with git repositories that are hosted anywhere, for example on BitBucket
or GitLab

Either way we will see something like this:

Cloning into 'github-in-class-brownsarahm'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 15 (delta 2), reused 5 (delta 1), pack-reused 0
Receiving objects: 100% (15/15), done.
Resolving deltas: 100% (2/2), done.

Now we can check what happened using ls

github-in-class-brownsarahm

When we clone a repository, it creates a new directory and downloads all of the contents and the repository information, including where it came from so that we can send our new changes back there.

3.4.3. Adding new files to a Repository Locally

We first go into that folder.

cd github-in-class-brownsarahm/

We can see what is there.

ls
README.md
touch about.md
ls

and then we see the list of files

README.md	about.md
git status

which gives us the following output

On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	about.md

nothing added to commit but untracked files present (use "git add" to track)
ls -a
.		.git		README.md
..		.github		about.md
git add .

again, we can check what git knows about

git status

and take note of the key differences from before.

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   about.md

git commit -m 'create empty about'
[main b81cf15] create empty about
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 about.md

3.4.4. Text editing on the terminal

nano about.md

then we can edit the file, adding some content and then write out (to save) and then exit nano

3.4.5. Commiting Changes to a file

git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   about.md

no changes added to commit (use "git add" and/or "git commit -a")
git add about.md
git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   about.md
git commit -m 'complete about closes #2
> '

[main 17320fc] complete about closes #2
 1 file changed, 4 insertions(+)
git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

3.4.6. Sending Changes to GitHub

git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 535 bytes | 535.00 KiB/s, done.
Total 6 (delta 1), reused 1 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/introcompsys/github-in-class-brownsarahm.git
   f707186..17320fc  main -> main

Notice on GitHub that the issue is now closed. and the commit is referenced and shows the changes.

3.5. A third way to close an issue

See the classmate issue:

owner:
- [ ] give a class mate access to the repo
- [ ] assign this issue to them

classmate:
- [ ] add `classmate.md` with your name and expected graduation on  a bracn `classmate`
- [ ] open a PR that will close this issue
  1. Do the owner list in your repo (the on that ends with your user name)

  2. Do the classmate actions in another person’s repo

  3. In your own repo, on the PR made by your class mate, tag @sp21instructors in a comment and then merge the PR.

3.5.1. Controlling Access

When you are a repository owner or organization level admin, you can change who has access to a repository on the settings tab.

Since the course repositories are in an organization, you get to choose the role for each collaborator or team. The GitHub Docs define the roles and permissions, so you can always refer t that to choose the right one.

Note

I seeded these notes by using the Export text option from the mac terminal app. Other terminals have similar options and you can always get only the list of commands you have run with history

3.6. Prepare for next class

  1. Complete the classmate issue in your in-class repository.

  2. read the notes PR, add or comment on a tip, resource, a bit of history in a sidebar or additional end of class question

  3. try using git in your IDE of choice, log any challenges you have on the practice repo (github-in-class-username), and tag @sp22instructors on GitHub. You can use either repo we have made in class, or one for an assignment in another course.

  4. using your terminal, download your KWL repo and update your 'learned' column on a new branch

  5. answer the questions below in a new markdown file, gitoffline.md in your KWL on your new branch and push the changes to GitHub

  6. Create a PR from your new branch to main do not merge this until instructed

  7. add your programming challenge(s) you have had as issues to our private repo or to the course website repo if you like. Put one 'challenge/question' per issue so that we can close them as addressed. See last class notes for prompt.

  8. Create or comment on a discussion thread in the private repo about the part of CS/ type of programming you like best/what you want to do post graduation.

Questions:

## Reflection
1. Describe the staging area (what happens after git add) in your own words. Can you think of an analogy for it? Is there anything similar in a hobby you have?
2. what step is the hardest for you to remember?
3. Compare and contrast using git on the terminal and through your IDE. when would each be better/worse?
4. Describe the commit that closed the `classmate` issue, who does it attribute the fix to?

3.7. More Practice

  1. Find the "Try it yourself" boxes in these notes, try them and add notes/ responses under a ## More Practice heading in your gitoffline.md file of your KWL repo.

  2. Download the course site repo via terminal.

  3. Explore the difference between git add and git commit try committing and pushing without adding, then add and push without committing. Describe what happens in each case in your gitoffline.md

3.8. Questions After class

3.8.1. Can we push the file before commit? And if so, what will happen?

3.8.2. what is the difference between add and commit? They seem to do the same thing to me.

3.8.3. Is there a point to doing multiple commits before a push.

3.8.4. Can I just use GitHub Desktop if I have it already?