3. How do I use git offline?#
3.1. Open a Terminal#
Use the same one we used in the last class
and move to the folder we made to use as a working directory for in class
cd Documents/inclass/systems/
Note
we did some review, see the last notes instead of recapping that here
..
is a special file that points to a specific relative path, of one level up.
we can use cd
and pwd
to illustrate what “one level up” means
pwd
/Users/brownsarahm/Documents/inclass/systems
cd ..
pwd
/Users/brownsarahm/Documents/inclass
we move to make the current working directory the one, one step prior in the hierarchy with cd ..
cd systems/
3.2. A toy repo for in class#
Warning
I removed the link from the public notes, but you can get it in prismia
this repo will be for in class work, you will not get feedback inside of it, unless you ask, but you will answer questions in your kwl repo about what we do in this repo sometimes
only work in this repo during class time or making up class, unless specifically instructed to (will happen once in a few weeks)
3.3. Connecting with GitHub#
We have two choices to Download a repository:
clone to maintain a link using git
download zip to not have to use git, but have no link
we want option 1 beacuse we are learning git
3.3.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.
ssh keys (we will do this later)
gh
CLI / gitscm in GitBash through browser
3.3.2. Windows (gitbash)#
git clone
and paste your URL from GitHubthen follow the prompts, choosing to authenticate in Browser.
3.3.3. MacOS X#
GitHub CLI: enter
gh auth login
and follow the prompts.then
git clone
and paste your URL from github
3.3.4. If nothing else works#
Create a 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).
Then proceed to the clone step. You may need to configure an identity later with git config
3.3.5. Cloning a repo#
git clone https://github.com/introcompsys/github-inclass-fa23-brownsarahm.git
then we get several messages back from git and GitHub (the remote)
Cloning into 'github-inclass-fa23-brownsarahm'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 8 (delta 0), reused 4 (delta 0), pack-reused 0
Receiving objects: 100% (8/8), done.
Confirm it worked with:
ls
github-inclass-fa23-brownsarahm
We see the new folder that matches our repo name
3.4. What is in a repo?#
We can enter that folder
cd github-inclass-fa23-brownsarahm/
When we compare the local directory to GitHub
ls
README.md
Notice that the .github/workflows
that we see on GitHub is missing,
that is because it is hidden. All file names that start with .
are hidden.
We can actually see the rest with the -a
for all option or flag. Options are how we can pass non required parameters to command line programs.
ls -a
. .git README.md
.. .github
We also see some special “files”, .
the current location and ..
up one directory
3.5. How do I know what git knows?#
git status
is your friend.
Let’s see how it works:
git satus
git: 'satus' is not a git command. See 'git --help'.
The most similar command is
status
git give you hits, when you try to use a command that doesn’t exist. It is friendly, not scary!
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
this command compares your working directory (what you can see with ls -a
and all subfolders except the .git
directorty) to the current state of your .git
directory.
this tells us:
the branch we are on (
On branch main
)that we have incorporated all changes downloaded from GitHub (
up to date with 'origin/main'
)that our working directory matches what it was after the repo’s last commit (
nothing to commit, working tree clean
)
3.6. Making a branch with GitHub.#
Note
Run the one action in the github in class repo one time to get the issues
First on an issue, create a branch using the link in the development section of the right side panel. See the github docs for how to do that.
Then it gives you two steps to do. We are going to do them one at a time so we can see better what they each do.
git fetch orign
fatal: 'orign' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Since the argument after git fetch
is the variable anme of a remote, when I splled origin
wrong, it says that it can’t find it.
First we will update the .git
directory without changing the working directory using git fetch. We have to tell git fetch where to get the data from, we do that using a name of a remote.
git fetch origin
From https://github.com/introcompsys/github-inclass-fa23-brownsarahm
* [new branch] 1-create-an-about-file -> origin/1-create-an-about-file
then we check status again
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Looks like nothing so far.
Next, we switch to that branch.
git checkout 1-create-an-about-file
branch '1-create-an-about-file' set up to track 'origin/1-create-an-about-file'.
Switched to a new branch '1-create-an-about-file'
and verify what happened
git status
On branch 1-create-an-about-file
Your branch is up to date with 'origin/1-create-an-about-file'.
nothing to commit, working tree clean
3.7. Creating a file on the terminal#
The touch
command creates an empty file.
touch about.md
We can use ls
to see our working directory now.
ls
README.md about.md
git status
On branch 1-create-an-about-file
Your branch is up to date with 'origin/1-create-an-about-file'.
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)
Now we see something new. Git tells us that there is a file in the working directory that it has not been told to track the changes in and it knows nothing.
It also tells us what we can do next. Under “Untracked files” it gives us advice for how to handle those files specifically. If we had made more than one type of change, there would be multiple subheadings each with their own suggestions.
The very last line is advice of what do to overall.
We’re going to do a bit more work first though, by adding conent to the file.
We are going to use the nano text editor to edit the file
nano about.md
On the nano editor the ^
stands for control.
and we can look at the contents of it.
cat about.md
Sarah Brown
2027
Now we will check again
git status
On branch 1-create-an-about-file
Your branch is up to date with 'origin/1-create-an-about-file'.
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)
In this case both say to git add
to track or to include in what will be committed. Under untracked files is it says git add <file>...
, in our case this would look like git add about.md
. However, remember we learned that the .
that is always in every directory is a special “file” that points to the current directory, so we can use that to add all files. Since we have only one, the two are equivalent, and the .
is a common shortcut, because most of the time we want to add everything we have recently worked on in a single commit.
git add
puts a file in the “staging area” we can use the staging area to group files together and put changes to multiple files in a single commit. This is something we cannot do on GitHub in the browser, in order to save changes at all, we have to commit. Offline, we can save changes to our computer without commiting at all, and we can group many changes into a single commit.
git add .
We will use status to see what has changed.
git status
On branch 1-create-an-about-file
Your branch is up to date with 'origin/1-create-an-about-file'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: about.md
Now that one file is marked as a new file and it is in the group “to be committed”. Git also tells us how to undo the thing we just did.
Try this yourself
Try making a change, adding it, then restoring it. Use git status to see what happens at each point
Next, we will commit the file. We use git commit
for this. the -m
option allows us to put our commit message dirctly on the line when we commit. Notice that unlike commiting on GitHub, we do not choose our branch with the git commit
command. We have to be “on” that branch before the git commit
.
git commit -m 'create and complete about file closes #1'
[1-create-an-about-file 693a2b5] create and complete about file closes #1
1 file changed, 2 insertions(+)
create mode 100644 about.md
Warning
At this point you might get an error or warning about your identity. Follow what git says to either set or update your identity using `git config
ls
README.md about.md
Remember, the messages that git gives you are designed to try to help you. The developers of git know it’s a complex and powerful tool and that it’s hard to remember every little bit.
We again check in with git:
git status
On branch 1-create-an-about-file
Your branch is ahead of 'origin/1-create-an-about-file' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Now it tells us we have changes that GitHub does not know about.
We can send them to github with git push
git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/introcompsys/github-inclass-fa23-brownsarahm.git
6a12db0..693a2b5 1-create-an-about-file -> 1-create-an-about-file
This tells us the steps git took to send:
counts up what is there
compresses them
sends them to GitHub
moves the
2-create-an-about-file
branch on GitHub from commit3f54148
to commit57de0cd
links the local
2-create-an-about-file
branch to the GitHub2-create-an-about-file
branch
3.8. Review today’s class#
Any steps in a badge marked lab are steps that we are going to focus in on during lab time. Remember the goal of lab is to help you complete the work, not add additional work. The lab checkout will include some other tasks and then we will encourage you to work on this badge while we are there to help. Lab checkouts are checked only for completion though, not correctness, so steps of activities that we want you to really think about and revise if incorrect will be in a practice or review badge.
Read the notes. If you have any questions, post an issue on the course website repo.
Using your terminal, download your KWL repo. Include the command used in your badge PR.
Try using setting up git using your favorite IDE or GitHub Desktop. Make a file gitoffline.md and include some notes of how it went. Was it hard? easy? what did you figure out or get stuck on? Is the terminology consistent or does it use different terms?
lab 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 a file called gitcommit.md. Compare what happens based on what you can see on GitHub and what you can see with git status.
3.9. More Practice#
Any steps in a badge marked lab are steps that we are going to focus in on during lab time. Remember the goal of lab is to help you complete the work, not add additional work. The lab checkout will include some other tasks and then we will encourage you to work on this badge while we are there to help. Lab checkouts are checked only for completion though, not correctness, so steps of activities that we want you to really think about and revise if incorrect will be in a practice or review badge.
Read the notes. If you have any questions, post an issue on the course website repo.
Using your terminal, download your KWL repo. Include the command used in your badge PR.
Try using setting up git using your favorite IDE or GitHub Desktop. Make a file gitoffline.md and include some notes of how it went. Was it hard? easy? what did you figure out or get stuck on? Is the terminology consistent or does it use different terms?
lab 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 a file called gitcommit_tips.md. Compare what happens based on what you can see on GitHub and what you can see with git status. Write a scenario with examples of how a person might make mistakes with git add and commit and what to look for to get unstuck.
3.10. Prepare for Next Class#
Reply below with any questions you have about using terminals so that you can bring it up in class
Be prepared to compare and contrast bash, shell, terminal, and git.
(optional) If you like to read about things before you do them, read about merge conflicts. If you prefer to see them first, read this after.
3.11. Experience Report Evidence#
link to your github inclass repo
3.12. Questions After Today’s Class#
3.12.1. Are the things we learned in the terminal available to revisit later on?#
Yes, in these notest!
3.12.2. what’s the difference between git checkout and git pull, i used pull in 305 a lot#
git pull
updates the same branch you are on. It calls git fetch
and then does some extra stuff. git checkout
switches branches
3.12.3. What situations would you use the terminal over something like VScode for?#
really quick edits where the time spent to open and configure VSCode does not pay off
3.12.4. Will we learn how to fix mistakes in the terminal? As in commands we did not mean to run#
Yes, for the things that can be fixed, we will also see what commands cannot be undone and what safeties are in built into CLI tools.
3.12.5. Im curious about the benefits to viewing files in the terminal as opposed to quick viewing them in finder, or other things that seem to be easier outside of terminal#
Viewing a file in finder when we know it is really small and only text requires context switching which makes it more work. Using a GUI can be an easier way to start, it is less to remember, but once you know how things work you cannot make them any faster, you have to move your mouse so far, wait for things to load, etc. The terminal lets you gain speed once you learn.
You can also automate and script things.
Finally, sometimes a GUI is a waste of resources that you do not have to spare.