4. How can I work with branches offline?#

4.1. Review#

Recall, We can move around and examine the computer’s file structure using shell commands.

pwd
/Users/brownsarahm

We can ust tab to complete once we hav a unique set of characters. If what we have is not unique bash will do nothing when you press tab once, but if you press it multiple tiems it will show you the options:

cd Do
Documents/ Downloads/ 

Let’s go back to the github inclass repo.

cd Documents/inclass/systems/github-in-class-brownsarahm-1/
pwd
/Users/brownsarahm/Documents/inclass/systems/github-in-class-brownsarahm-1

Also recall our one file

ls
README.md

and how to see hidden files.

ls -a
.		..		.git		.github		README.md

4.2. How do I know what git knows?#

git status is your friend.

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.

4.3. Making a branch with GitHub.#

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.

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-in-class-brownsarahm-1
 * [new branch]      2-create-an-about-file -> origin/2-create-an-about-file

We can look at the repo to see what has changed.

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

nothing to commit, working tree clean

This says nothing, because remember git status tells us the relationship between our working directory and the .git repo.

Next, we switch to that branch.

git checkout 2-create-an-about-file
branch '2-create-an-about-file' set up to track 'origin/2-create-an-about-file'.
Switched to a new branch '2-create-an-about-file'

and verify what happened

git status
On branch 2-create-an-about-file
Your branch is up to date with 'origin/2-create-an-about-file'.

nothing to commit, working tree clean

4.4. 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.

:class: cell_input
ls
README.md	about.md

and check how git has changed too?

:class: cell_input
git status
:class: cell_output
On branch 2-create-an-about-file
Your branch is up to date with 'origin/2-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.

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 2-create-an-about-file
Your branch is up to date with 'origin/2-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 empty about"
[2-create-an-about-file 57de0cd] create empty about
 1 file changed, 0 insertions(+), 0 deletions(-)
 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

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 2-create-an-about-file
Your branch is ahead of 'origin/2-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.

ls
README.md	about.md

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), 313 bytes | 313.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/introcompsys/github-in-class-brownsarahm-1.git
   3f54148..57de0cd  2-create-an-about-file -> 2-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 commit 3f54148 to commit 57de0cd

  • links the local 2-create-an-about-file branch to the GitHub 2-create-an-about-file branch

Remember our directory has other things in it:

ls -a
.		.git		README.md
..		.github		about.md

4.5. What does push do?#

git push only sends the .git directory. To see that, we can make a file

touch temp

but not add or commit it

git push
Everything up-to-date

when we push git tells us there is nothing to send, because git doesn’t know about that file.

Even though our computer can see that file with ls

ls
README.md	about.md	temp

git only looks at the working directory and compares it to the .git directory’s snapshots of our work when we use git status and git add other commands work with the .git directory only

We can delete that file, since we do not need it.

rm temp 

4.6. Getting changes from GitHub#

In your browser, make a pull request and then merge it for the changes that we pushed. It will be automatically linked to the issue, since we created the branch from the issue. Linked issues get closed when the PR is merged.

Now, switch to main on your local copy

git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

It tells us that it is up to date, because it is up to date with what our computer knows about GitHub. If we used git fetch then git status it would tell us we were behind. Then we would have to apply the changes to our working directory after we fetched them. We can fetch and apply them at the same time with git pull.

git pull
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 639 bytes | 639.00 KiB/s, done.
From https://github.com/introcompsys/github-in-class-brownsarahm-1
   3f54148..0169e39  main       -> origin/main
Updating 3f54148..0169e39
Fast-forward
 about.md | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 about.md

Here we see 2 sets of messages. Some lines start with “remote” and other lines do not. The “remote” lines are what git on the GitHub server said in response to our request and the other lines are what git on your local computer said.

So, here, it counted up the content, and then sent it on GitHub’s side. On the local side, it unpacked (remember git compressed the content before we sent it). It describes the changes that were made on the GitHub side, the main branch was moved from one commit to another. So it then updates the local main branch accordingly (“Updating 3f54148…0169e39”) then it tells the type of update (“Fast-forward”) and describes a summary of the changes made to each file (created about.md empty).

We can see that this updates th working directory too:

ls
README.md	about.md

We’ve used git checkout to switch branchs before. To also create a branch at the same time, we use the -b option.

git checkout -b fill-in-about
Switched to a new branch 'fill-in-about'

and git tells us what it has done.

4.7. Editing a file on the terminal#

we used the nano text editor. nano is simpler than other text editors that tend to be more popular among experts, vim and emacs. Getting comfortable with nano will get you used to the ideas, without putting as much burden on your memory. This will set you up to learn those later, if you need a more powerful terminal text editor.

nano about.md

this opens the nano program on the terminal. it displays reminders of the commands at the bottom of th screen and allows you to type into the file right away.

Navigate with your keyboard arrows, add your name to the file. Then, write out (save), it will prompt the file name. Since we opened nano with a file name (about.md) specified, you will not need to type a new name, but to confirm it, by pressing enter/return.

Now, we check in with git again.

git status
On branch fill-in-about
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")

This is very similar to when we checked after creating the file before, but, notice a few things are different.

  • the first line tells us the branch but does not compare to origin. (this branch does not have a linked branch on GitHub)

  • thefile is listed as “not staged” instead of untracked

  • it gives us the choice to add it to then commit OR to restore it to undo the changes

git add .

we’ll add, since we want these changes

git status
On branch fill-in-about
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   about.md

and commit.

git commit -m "add my name"
[fill-in-about c28b4ad] add my name
 1 file changed, 1 insertion(+)

and push

git push
fatal: The current branch fill-in-about has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin fill-in-about

It cannot push, because it does not know where to push, like we noted above that it did not comapre to origin, that was beecause it does not have an “upstream branch” or a corresponding branch on a remote server.

However, git helps us out and tells us how to add one. So we do as advised.

git push --set-upstream origin fill-in-about
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 265.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: 
remote: Create a pull request for 'fill-in-about' on GitHub by visiting:
remote:      https://github.com/introcompsys/github-in-class-brownsarahm-1/pull/new/fill-in-about
remote: 
To https://github.com/introcompsys/github-in-class-brownsarahm-1.git
 * [new branch]      fill-in-about -> fill-in-about
branch 'fill-in-about' set up to track 'origin/fill-in-about'.

This time the returned message from the remote includes the link to the page to make a PR. Visit that and make and merge the PR.

Note though, that this does not change your local copy.

git status
On branch fill-in-about
Your branch is up to date with 'origin/fill-in-about'.

nothing to commit, working tree clean

Even the main branch does not change

git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

but it does not know that it is behind until we fetch or pull again.

4.8. Review today’s class#

  1. Review the notes

  2. Find your team’s page on GitHub. It is named like Spring2023-group-# join the discussion that I started on your page.

  3. Complete the classmate issue in your inclass repo from today. Find a partner from within your assigned team by posting on your team’s page. Link to your commits on your badge issue.

  4. Try using git using your favorite IDE or GitHub Desktop. You can either do the other tasks for this badge, work on a different badge, or add & commit some random files in your inclass repo. Answer the questions below in gitcompare.md.

Questions:

## Reflection

1. What tool's git integration did you use? 
1. Compare and contrast using git on the terminal and through the tool you used.  When would each be better/worse?  
1. Did using a more visual representation help you understand better? 
1. Describe the staging area (what happens after git add) in your own words. 
2. what step is the hardest for you to remember? what do you think might help you? 

4.9. Prepare for Next Class#

  1. Examine a large project you have done or by finding an open source project on GitHub. Answer the reflection questions in software.md in your kwl repo. (will be in notes)

  2. map out how you think about data moving through a small program and bring it with you to class (no need to submit)

## Software Reflection

1. link to public repo if applicable or title of your project
1. What types of files are there that are not code?
1. What different types of code files are in the project? Do they serve different goals?
1. Is it all in one language or are there multiple?
1. Try to figure out (remember) how the project works. What types of things, without running the code can you look at at a high level?

4.10. More Practice#

  1. Review the notes

  2. Find your team’s page on GitHub. It is named like Spring2023-group-# join the discussion that I started on your page.

  3. Download the course website repo via terminal. Append the commands used to a terminalwork.md

  4. 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 gitcommit.md. Compare what happens based on what you can see on GitHub and what you can see with git status.

  5. Complete the classmate issue in your inclass repo from today. Find a partner from within your assigned team by posting on your team’s page. Link to your commits on your badge issue.

  6. Try using git using your favorite IDE and GitHub Desktop. You can either do the other tasks for this badge, work on a different badge, or add & commit some random files in your inclass repo. Answer the questions below in gitcompare3ways.md.

Questions:

## Reflection

1. What IDE did you use?
1. Was the IDE or GitHub better for you? Why?
1. Compare and contrast using git on the terminal and through your IDE. When would each be better/worse?  
1. Did using a more visual representation help you understand better? 
1. Describe the staging area (what happens after git add) in your own words. Provide an analogy for it using a hobby  or interest of yours.  
2. What programming concepts is the staging area similar to? 
2. what step is the hardest for you to remember? what do you think might help you? 

4.11. Experience Report Evidence#

For today’s class evidence

4.12. Questions After Today’s Class#

4.12.1. How does the computer hold the information for my github account after I shut it off?#

It stores your account information in files on your system.

4.12.2. How does the terminal know that our upstream server is github?#

We cloned the repo from github. This can also be configured manually. We can even add more then one available remote in a single repo. Each branch will have a specific, single upstream, but two branches in the same repo can have different upstreams.

4.12.3. Could I have edited my experience reflection in terminal if I wanted?#

Yes!

4.12.4. What is the best way to get comfortible with the terminal?#

Lots of practice. Start with the recently posted badges, they’re designed to give you good practice.

4.12.5. What is a .md file?#

Markdown

4.12.6. Are there any disadvantages to using nano as opposed to something like VIM?#

nano only has simple features. For simple tasks, there’s no disadvantage. For complex tasks it would likely be more laborious.

4.12.7. Is there going to be somewhere we can see all of git hubs syntax for the terminal?#

I passed out a printed cheatsheet from GitHub with a good amount of the sytax. If you did not get one, let me know.

4.12.8. If I close the terminal is everything we did saved?#

Yes, everything we did today in the terminal was directly manipulating files. The log of what commands we ran is even likely saved, but not necessarily in an easy to find and use file.

4.12.9. Is it possible for me to perform everything that I need to with git through the terminal?#

git is a command line tool, by defition. The git program is only available on the terminal. Other programs like GitHub Desktop or VSCode can do git operations, but typically only a subset. There is also only a subset of git operations available on GitHub.com.

There are however, some GitHub things that there is no command line tool for.

4.12.10. How do I create different versions of my code on GitHub?#

branches!

4.12.11. Is origin the name used by git terminal to reference github’s servers?#

In our repos we have seen so far origin has always poointed to a specific repo on GitHub. origin is just the default name, so, there could be no origin and the repo still have the remote be a GitHub repo.

We will see soon that we can work with multiple remotes, for example with a GitHub fork. In that case, there would be two different remotes tht each point to a GitHub repo.

4.12.12. How to commit multiple files at once?#

add more than one file to the staging area before commiting.

4.12.13. Is there a way to work with other things (issues, actions, etc) in the terminal?#

For GitHub, yes!