5. When do I get an advantage from git and bash?#

so far we have used git and bash to accomplish familiar goals, and git and bash feel like just extra work for familiar goals.

Today, we will start to see why git and bash are essential skills: they give you efficiency gains and time traveling super powers (within your work, only, sorry)

5.1. Important references#

Use these for checking facts and resources.

5.2. Setup#

First, we’ll go back to our github inclass folder

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

and make sure we are on main:

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

nothing to commit, working tree clean

Then we will use fetch to see if we are really up to date

git fetch

We will also check for open PRs in the browser:

Note this is optional, and only works with the gh cli is installed

gh repo view --web
Opening github.com/introcompsys/github-in-class-brownsarahm-1 in your browser.

and merge thm there.

To get to your main branch.

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

then fetch again because we now know there are changes on GitHub

git fetch
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), 628 bytes | 628.00 KiB/s, done.
From https://github.com/introcompsys/github-in-class-brownsarahm-1
   0169e39..4b89dff  main       -> origin/main
ls
README.md	about.md

Notice that it updated the .git, but not our working directory.

cat about.md 

So we can check again

git status
On branch main
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

now we see it knows we are behind and tells us how to update

git pull
Updating 0169e39..4b89dff
Fast-forward
 about.md | 1 +
 1 file changed, 1 insertion(+)

5.3. Branches review#

We can get a list of the branches we have locally

git branch
  2-create-an-about-file
  fill-in-about
* main

or see them with their upstream information

git branch -vv
  2-create-an-about-file 57de0cd [origin/2-create-an-about-file] create empty about
  fill-in-about          c28b4ad [origin/fill-in-about] add my name
* main                   4b89dff [origin/main] Merge pull request #5 from introcompsys/fill-in-about

5.3.1. Different ways to create a branch and switch#

This doesn’t work for a branch that does not exist

git checkout my_branch_checkedout
error: pathspec 'my_branch_checkedout' did not match any file(s) known to git

This creates and switches to.

git checkout -b my_branch_checkedoutb
Switched to a new branch 'my_branch_checkedoutb'

we’ll go back to main before the next step

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

this is not a real command

git branch create my_branch_created
fatal: not a valid object name: 'my_branch_created'

This is a 2 step way to create and switch.

git branch my_branch; git checkout my_branch
Switched to branch 'my_branch'

5.4. Organizing a project (workign with files)#

A common question is about how to organize projects. While our main focus in this class session is the bash commands to do it, the task that we are going to do is to organize a hypothetical python project

We’ll go back to main first

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

then create a new branch from main.

git checkout -b organization
Switched to a new branch 'organization'
touch abstract_base_class.py helper_functions.py important_classes.py alternative_classes.py README.md LICENSE.md CONTRIBUTING.md setup.py tests_abc.py test_help.py test_imp.py test_alt.py overview.md API.md _config.yml

git status
On branch organization
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	API.md
	CONTRIBUTING.md
	LICENSE.md
	_config.yml
	abstract_base_class.py
	alternative_classes.py
	helper_functions.py
	important_classes.py
	overview.md
	setup.py
	test_alt.py
	test_help.py
	test_imp.py
	tests_abc.py

nothing added to commit but untracked files present (use "git add" to track)

5.5. Files, Redirects, git restore#

cat README.md 
# GitHub Practice

Name: Sarah Brown

Echo allows us to send a message to std out. which is a special file.

echo "age=35"
age=35

We can add contents to files with echo and >>

echo "age=35" >> README.md 

Then we check the contents of the file and we see that the new content is there.

cat README.md 
# GitHub Practice

Name: Sarah Brown
age=35

We can redirect other commands too:

git status >> curgit

we see this created a new file

ls
API.md			abstract_base_class.py	setup.py
CONTRIBUTING.md		alternative_classes.py	test_alt.py
LICENSE.md		curgit			test_help.py
README.md		helper_functions.py	test_imp.py
_config.yml		important_classes.py	tests_abc.py
about.md		overview.md

and we can look at its contents too

cat curgit 
On branch organization
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:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	API.md
	CONTRIBUTING.md
	LICENSE.md
	_config.yml
	abstract_base_class.py
	alternative_classes.py
	curgit
	helper_functions.py
	important_classes.py
	overview.md
	setup.py
	test_alt.py
	test_help.py
	test_imp.py
	tests_abc.py

no changes added to commit (use "git add" and/or "git commit -a")
rm curgit 

we’ll delete this test file.

Now we have made some changes we want, so let’s commit our changes.

git commit -m 'start organizing'
On branch organization
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:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	API.md
	CONTRIBUTING.md
	LICENSE.md
	_config.yml
	abstract_base_class.py
	alternative_classes.py
	helper_functions.py
	important_classes.py
	overview.md
	setup.py
	test_alt.py
	test_help.py
	test_imp.py
	tests_abc.py

no changes added to commit (use "git add" and/or "git commit -a")

Without adding first, git cannot make a commit.

So we will add first then commit.

git add .
git commit -m 'start organizing'
[organization ef45e77] start organizing
 15 files changed, 1 insertion(+)
 create mode 100644 API.md
 create mode 100644 CONTRIBUTING.md
 create mode 100644 LICENSE.md
 create mode 100644 _config.yml
 create mode 100644 abstract_base_class.py
 create mode 100644 alternative_classes.py
 create mode 100644 helper_functions.py
 create mode 100644 important_classes.py
 create mode 100644 overview.md
 create mode 100644 setup.py
 create mode 100644 test_alt.py
 create mode 100644 test_help.py
 create mode 100644 test_imp.py
 create mode 100644 tests_abc.py
git status
On branch organization
nothing to commit, working tree clean

Now, let’s go back to thinking about redirects. We saw that with two >> we appended to the file. With just one what happens?

echo "age=35" > README.md 

We check the file now

cat README.md 
age=35

It wrote over. This would be bad, we lost content, but this is what git is for!

It is very very easy to undo work since our last commit. This is good for time when you have something you have an idea and you do not know if it is going to work, so you mak a commit before you try it. Then you can try it out. If it doesn’t work you can undo and go back to the place where you made the commit.

To do this, we will first check in with git

git status
On branch organization
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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

Notice that it tells us what to do (use "git restore <file>..." to discard changes in working directory). The version of README.md that we broke is in the working directory but not commited to git, so git refers to them as “changes” in the workign directory.

git restore README.md 

this command has no output, so we can use git status to check first

git status
On branch organization
nothing to commit, working tree clean

and it looks like it did before the > line. and we can check the file too

cat README.md 
# GitHub Practice

Name: Sarah Brown
age=35

Back how we wanted it!

Now we will add some text to the readme

 echo "|file | contents |
> > | ------| ------- |
> > | abstract_base_class.py | core abstract classes for the project |
> > | helper_functions.py | utitly funtions that are called by many classes |
> > | important_classes.py | classes that inherit from the abc |
> > | alternative_classes.py | classes that inherit from the abc |
> > | LICENSE.md | the info on how the code can be reused|
> > | CONTRIBUTING.md | instructions for how people can contribute to the project|
> > | setup.py | file with function with instructions for pip |
> > | tests_abc.py | tests for constructors and methods in abstract_base_class.py|
> > | tests_helpers.py | tests for constructors and methods in helper_functions.py|
> > | tests_imp.py | tests for constructors and methods in important_classes.py|
> > | tests_alt.py | tests for constructors and methods in alternative_classes.py|
> > | API.md | jupyterbook file to generate api documentation |
> > | _config.yml | jupyterbook config for documentation |
> > | _toc.yml | jupyter book toc file for documentation |
> > | philosophy.md | overview of how the code is organized for docs |
> > | example.md | myst notebook example of using the code |
> > | scratch.ipynb | jupyter notebook from dev |" >> README.md

this explains each file a little bit more than the name of it does. We see there are sort of 5 groups of files:

  • about the project/repository

  • code that defines a python module

  • test code

  • documentation

  • extra files that “we know” we can delete.

Note

using the open quote " then you stay inside that until you close it. when you press enter the command does not run until after you close the quotes

cat README.md 
# GitHub Practice

Name: Sarah Brown
age=35
|file | contents |
> | ------| ------- |
> | abstract_base_class.py | core abstract classes for the project |
> | helper_functions.py | utitly funtions that are called by many classes |
> | important_classes.py | classes that inherit from the abc |
> | alternative_classes.py | classes that inherit from the abc |
> | LICENSE.md | the info on how the code can be reused|
> | CONTRIBUTING.md | instructions for how people can contribute to the project|
> | setup.py | file with function with instructions for pip |
> | tests_abc.py | tests for constructors and methods in abstract_base_class.py|
> | tests_helpers.py | tests for constructors and methods in helper_functions.py|
> | tests_imp.py | tests for constructors and methods in important_classes.py|
> | tests_alt.py | tests for constructors and methods in alternative_classes.py|
> | API.md | jupyterbook file to generate api documentation |
> | _config.yml | jupyterbook config for documentation |
> | _toc.yml | jupyter book toc file for documentation |
> | philosophy.md | overview of how the code is organized for docs |
> | example.md | myst notebook example of using the code |
> | scratch.ipynb | jupyter notebook from dev |
ls
API.md			abstract_base_class.py	test_alt.py
CONTRIBUTING.md		alternative_classes.py	test_help.py
LICENSE.md		helper_functions.py	test_imp.py
README.md		important_classes.py	tests_abc.py
_config.yml		overview.md
about.md		setup.py

5.6. Getting organized#

First, we’ll make a directory

mkdir docs

next we will move a file there

mv overview.md docs/

what this does is change the path of the file from .../github-inclass-brownsarahm-1/overview.md to .../github-inclass-brownsarahm-1/docs/overview.md

This doesn’t return anything, but we can see the effect with ls

ls
API.md			abstract_base_class.py	test_alt.py
CONTRIBUTING.md		alternative_classes.py	test_help.py
LICENSE.md		docs			test_imp.py
README.md		helper_functions.py	tests_abc.py
_config.yml		important_classes.py
about.md		setup.py

We can also use ls with a relative or absolute path of a directory to list tht location instead of our current working directory.

ls docs/
overview.md
touch _toc.yml

5.6.1. Moving multiple files with patterns#

let’s look at the list of files again.

ls
API.md			about.md		setup.py
CONTRIBUTING.md		abstract_base_class.py	test_alt.py
LICENSE.md		alternative_classes.py	test_help.py
README.md		docs			test_imp.py
_config.yml		helper_functions.py	tests_abc.py
_toc.yml		important_classes.py

We can use the * wildcard operator to move all files that match the pattern. We’ll start with the two yml (yaml) files that are both for the documentation.

mv *.yml docs/

Again, we confirm it worked by seeing that they are no longer in the working directory.

ls
API.md			abstract_base_class.py	setup.py
CONTRIBUTING.md		alternative_classes.py	test_alt.py
LICENSE.md		docs			test_help.py
README.md		helper_functions.py	test_imp.py
about.md		important_classes.py	tests_abc.py

and that they are in docs

ls docs/
_config.yml	_toc.yml	overview.md

We see that most of the test files start with test_ but one starts with tests_. We could use the pattern test*.py to move them all without conflicting with the directory tests/ but we also want consistent names.

We can use mv to change the name as well. This is because “moving” a file and is really about changing its path, not actually copying it from one location to another and the file name is a part of the path.

mv tests_abc.py test_abc.py 

This changes the path from .../tests_abc.py to .../test_abc.py to. It is doing the same thing as when we use it to achieve a move, but changing a differnt part of the path.

ls
API.md			abstract_base_class.py	setup.py
CONTRIBUTING.md		alternative_classes.py	test_abc.py
LICENSE.md		docs			test_alt.py
README.md		helper_functions.py	test_help.py
about.md		important_classes.py	test_imp.py

Now we make a new folder:

mkdir tests

and move all of the test files there:

mv test_* tests/

ls
API.md			about.md		helper_functions.py
CONTRIBUTING.md		abstract_base_class.py	important_classes.py
LICENSE.md		alternative_classes.py	setup.py
README.md		docs			tests

5.7. Recap#

Why do I need a terminal

  1. replication/automation

  2. it’s always there and doesn’t change

  3. it’s faster one you know it (also see above)

So, is the shell the feature that interacts with the operating system and then the terminal is the gui that interacts with the shell?

Important

if your push gets rejected, read the hints, it probably has the answer. We will come back to that error though

5.8. Review today’s class#

  1. Read today’s notes

  2. Update your KWL chart with the new items and any learned items.

  3. add branches.md to your KWL repo and describe how branches work, in your own words. Include one question you have about branches or one scenario you think they could help you with.

5.9. Prepare for Next Class#

  1. Read through the grading section and all of your feedback as it arrives.

  2. Bring git questions or scenarios you want to be able to solve to class on Thursday

  3. Update your .github/workflows/experiencereflection.yml file as follows: replace the two lines team-reviewers: | and instructors with reviewers: <ta-gh-name> where ta-gh-name is whichever TA is in your group. You can see your group on the organiation teams page named like “Spring 2023 Group X”. Make a PR and ask that TA for a review.

  4. Answer the following in a comment on your prepare issue. Tag @brownsarahm on the issue

# Plan for success

__Target Grade:__ (A,B,..)

## Plan to get there: 
- 24 experience badges 
- (other badges you plan)

<!-- If you plan any build badges,uncomment the line below and list some ideas, topics from the course website your are curios about or questions you have. Also think about what timeline you could realistically achieve them on.  -->
<!-- ## Builds -->

<!-- If you plan any explore badges, create a schedule and propose one topic for your first explore badge -->

5.10. More Practice#

  1. Read today’s notes

  2. Update your KWL chart with the new items and any learned items.

  3. Learn about GitHub forks (you can also use other resources)

  4. add branches-forks.md to your KWL repo and describe how branches work, what a fork is and how they relate to one another. If you use other resources, include them in your file.

5.11. Experience Report Evidence#

Save your history with:

history > activity-2023-02-07.md

then append your git status, and the contents of your github-in-class and github-in-class/docs with -------------- to help visually separat the parts.

echo "--------------" >> activity-2023-02-07.md
git status >> activity-2023-02-07.md
echo "--------------" >> activity-2023-02-07.md
ls >> activity-2023-02-07.md
echo "--------------" >> activity-2023-02-07.md
ls docs/ >> activity-2023-02-07.md

then edit that file (on terminal, any text editor, or an IDE) to make sure it only includes things from this activity.

5.12. Questions After Today’s Class#

5.12.1. how would we move every file we have into a new folder#

mv old_folder/* new_folder/

5.12.2. Can we create a pull request through bash?#

Strictly speaking bash does not do any git operations or any GitHub operation, or any other version control operations for other version control systems. bash is a particular scripting language that we are using on our terminal. We could use a different one, and git can still be used.

Pull requests are not a git feature, so they are not done by the git program at all. They are done differnt ways for different projects and hosting plan. For example, the git source code is mirrored (copied) on GitHub, but that is not where they actually do the work of maintaining it. They accept “pull requests” via e-mail.

5.12.3. Can we merge using the terminal?#

Yes, we will see git merge soon.

5.12.4. I would like to know more about regular expressions as this is something I have seen but don’t really know anything about.#

We will use a few examples of them, but we will not go too in depth with them. We’ll focus on the places they can be used and basic regex for the most common cases. However, this is an excellent topic for an explore or build badge.

5.12.5. What other symbols can be used to move files like *?#

The wildcard * can be used not only for moving files, but in lots of places. It is a special character in regular expressions and glob matching. We will see some different ones, but this is also a good topic for an explore badge.

5.12.6. Will we use pipes in the terminal?#

Yes, we will see pipes soon.

5.12.7. When you said that it only changes file path, do you mean its storage placement in the hard drive stays the same?#

Yes, exactly.

5.12.8. Will we be learning out to open and use a .py file?#

Indirectly. We will do a tiny bit of editing and running code in python and C, but not too much. We will learn more about what files are, how they work in general, and the role of interpreters and compilers so that you will be able to use the terminal for any language you encounter.

5.12.9. To make sure, mv can rename and move files?#

Yes, it can do achieve both of those outcomes, because in the file system they are the same thing. “moving a file” does not have to actually copy and rewrite the file in a different part of the hard drive, but instead it changes the path of the file. We typically think about the path as having two parts: the location and the name, but technicall there is a single path to the whole file. mv can change any part of the path.

5.12.10. How can we delete branches?#

There is a delete option on git branch

5.12.11. What exactly are we doing with our teams that we were assigned?#

Instructions will be given as you need to respond to them, for now just reply to discussions on the team page.

5.12.12. Should I start using terminal as a replacement of finder to move files/folders?#

If you are comfortable with it. It will help you get more comfortable faster.

5.12.13. what is a .yml file?#

it is YAML which is typically used to store settings and configurations. GitHub uses it for the workflows, and a lot of other tools I interact with use it for configurations. It stores key-value pairs.

5.12.14. Will all the files we used in the terminal today will be on the github website?#

After we push them, yes.

5.12.15. Is there an official list of all git commands?#

the official git documentation includes a list of all commands. GitHub also publishes a cheatsheet in many languages. This visual cheatsheet is also good.