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. Setup#
First, we’ll go back to our github inclass folder
cd Documents/inclass/systems/github-inclass-fa23-brownsarahm/
and we will make sure we are in sync with GitHub. We know we can fix it, but it is best to not break it.
git status
On branch fun_fact
Your branch is up to date with 'origin/fun_fact'.
nothing to commit, working tree clean
Since our working tree is clean, now we can get updates from GitHub
git pull
Already up to date.
Now let’s go back to the main branch.
git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
It tells us we are there.
Let’s look at what commits are on the main branch.
git log
commit 5c8aaa9f2a129d551b8cb2cb294676f63c4af410 (HEAD -> main, origin/main, origin/HEAD)
Merge: caeacb5 65e9e39
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:47:08 2023 -0400
Merge pull request #5 from introcompsys/add-name
closes #2
commit 65e9e39935be8400ef12cc9003592f12244b50da (origin/add-name)
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:46:00 2023 -0400
closes #2
commit caeacb503cf4776f075b848f0faff535671f2887
Merge: 6a12db0 693a2b5
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:38:54 2023 -0400
Merge pull request #4 from introcompsys/1-create-an-about-file
create and complete about file closes #1
commit 693a2b5b9ad4c27eb3b50571b3c93dde353320a1 (origin/1-create-an-about-file, 1-create-an-about-file)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:36:03 2023 -0400
We remember that we had added fun facts and none of our commits about those are here, so the fun_fact branch is still ahead of the main branch.
It’s something like this:
5.2. Merging a branch offline#
We can merge branches locally, without making a pull request in GitHub.
git merge fun_fact
Updating 5c8aaa9..756c487
Fast-forward
about.md | 4 ++++
1 file changed, 4 insertions(+)
It summarizes the changes.
Now we can use git log
to see where our branches all point.
git log
commit 756c4879c0447db20980f73a26bc2ba072e08a6d (HEAD -> main, origin/fun_fact, fun_fact)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 13:26:20 2023 -0400
second fun fact
commit 768dec80c5e0734476d476ae83376c9c786b6450
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 13:21:31 2023 -0400
Update about.md
commit 6d4dbd33860fceb9c87bd3c4509deff8cecb3f45
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 13:06:54 2023 -0400
add fun fact
commit 5c8aaa9f2a129d551b8cb2cb294676f63c4af410 (origin/main, origin/HEAD)
Merge: caeacb5 65e9e39
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:47:08 2023 -0400
Merge pull request #5 from introcompsys/add-name
closes #2
commit 65e9e39935be8400ef12cc9003592f12244b50da (origin/add-name)
Visually, it is like this
git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
It is ahead by 3 commits now because there were 3 commits on the fun_fact branch.
5.3. 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
next we are going to pretend we worked on the project and made a bunch of files
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 _toc.yml
New bash lessons:
touch
can accept a list of files to createa list in bash is space separated with no brackets
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 helper_functions.py test_imp.py
_config.yml important_classes.py tests_abc.py
_toc.yml overview.md
git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
API.md
CONTRIBUTING.md
LICENSE.md
_config.yml
_toc.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.4. Echo and redirects#
cat
concatenates the contents of a file to stdout, which is a special file that our terminal reads
cat README.md
# GitHub Practice
Name: Sarah Brown
[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-7f7980b617ed060a017424585567c406b6ee15c891e84e1186181d67ecf80aa0.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=11872426)
echo
allows us to send a message to stdout.
ehco "age=35"
-bash: ehco: command not found
For example, we can echo our age
echo "age=35"
age=35
We can also redirect the contents of a command from stdout to a file in bash
like file operations while programming there is a similar concept to this mode.
There are two types of redirects, like there are two ways to write to a file, more generally:
overwrite (
>
)append (
>>
)
echo "age=35" >> README.md
this command has no output, but we can use cat
to view the file to see its impact.
cat README.md
# GitHub Practice
Name: Sarah Brown
[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-7f7980b617ed060a017424585567c406b6ee15c891e84e1186181d67ecf80aa0.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=11872426)
age=35
Now we see the text at the bottom of the file.
5.5. commit and add has limitations#
Now we have made some changes we want, so let’s commit our changes.
Last class we saw we can add and commit at the same time
git commit -a -m 'start organizing'
[main bc28179] start organizing
1 file changed, 1 insertion(+)
This is way fewer things than we have done recently (remember we also made all of those files with touch)
lets check git status
again
git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
API.md
CONTRIBUTING.md
LICENSE.md
_config.yml
_toc.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)
The -a
option on a commit does not add untracked files
We have to explicitly add those files.
git add .
Now we can commit the content.
git commit -m 'start organizng for real'
[main d76bc52] start organizng for real
15 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 API.md
create mode 100644 CONTRIBUTING.md
create mode 100644 LICENSE.md
create mode 100644 _config.yml
create mode 100644 _toc.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 main
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
5.6. Redirect file modes#
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. One >
writes to the file in overwrite mode.
Note
mode is a good vocab term that could be added to the site glossary for a community badge
5.7. Recovering from mistakes with git restore#
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 times when you have something you have an idea and you do not know if it is going to work, so you make 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 main
Your branch is ahead of 'origin/main' by 5 commits.
(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: 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
Now lets check with git.
git status
On branch main
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Our working tree is clean now.
and it looks like it did before the >
line. and we can check the file too
cat README.md
# GitHub Practice
Name: Sarah Brown
[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-7f7980b617ed060a017424585567c406b6ee15c891e84e1186181d67ecf80aa0.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=11872426)
age=35
Back how we wanted it!
5.8. What are these files?#
We can use a redirect to add a bunch of text to the README file.
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.
We can see what it does with cat
cat README.md
# GitHub Practice
Name: Sarah Brown
[![Open in Codespaces](https://classroom.github.com/assets/launch-codespace-7f7980b617ed060a017424585567c406b6ee15c891e84e1186181d67ecf80aa0.svg)](https://classroom.github.com/open-in-codespaces?assignment_repo_id=11872426)
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 |
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
We can see how the "
allows us to go on multiple lines this way:
echo "sldkjfds
>
>
>
>
>
>
>
>
>
> "
and it will output all of the blank lines
sldkjfds
5.9. Getting organized#
Recall, we have just created a bunch of files.
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 helper_functions.py test_imp.py
_config.yml important_classes.py tests_abc.py
_toc.yml overview.md
First, we’ll make a directory with mkdir
mkdir docs
we can use ls
to see the files that exist
ls
API.md about.md overview.md
CONTRIBUTING.md abstract_base_class.py setup.py
LICENSE.md alternative_classes.py test_alt.py
README.md docs test_help.py
_config.yml helper_functions.py test_imp.py
_toc.yml important_classes.py tests_abc.py
5.10. Moving Files#
next we will move a file there with mv
We can see how to use this by getting the error when we do not pass any arguments.
mv
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
we get an error message that tells us how to use the command, it takes two arguments: source and target.
5.10.1. Help in GitBash#
mv --help
mv: illegal option -- -
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
This only give a basic version on mac, but is more on Git Bash
5.10.2. Getting help on *nix systems#
man
shows the manfile for help for a specific command
man mv
It opens in a program, then we need to use q
to exit.
5.10.3. Back to moving.#
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 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 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
5.10.4. 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 have lots with similar names.
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
Next we will work on the test files
mkdir tests
5.11. Renaming Files#
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
Here I pressed tab multiple times trying to get tab complete, but since it was not unique, it showed me the two options.
tests/ tests_abc.py
Then I added _
and pressed tab again to get it full file name.
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 move a file from one folder to another folder, but changing a differnt part of the path.
ls
API.md alternative_classes.py test_alt.py
CONTRIBUTING.md docs test_help.py
LICENSE.md helper_functions.py test_imp.py
README.md important_classes.py tests
about.md setup.py
abstract_base_class.py test_abc.py
Then we can use a similar pattern to move the files.
mv test_*.py tests/
Now we can confirm that it is how we expect.
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
ls tests/
test_abc.py test_alt.py test_help.py test_imp.py
5.12. Commiting Changes#
we check where we are
git status
On branch main
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
deleted: _config.yml
deleted: _toc.yml
deleted: overview.md
deleted: test_alt.py
deleted: test_help.py
deleted: test_imp.py
deleted: tests_abc.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
docs/
tests/
no changes added to commit (use "git add" and/or "git commit -a")
git add docs/
git status
On branch main
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: docs/_config.yml
new file: docs/_toc.yml
new file: docs/overview.md
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
deleted: _config.yml
deleted: _toc.yml
deleted: overview.md
deleted: test_alt.py
deleted: test_help.py
deleted: test_imp.py
deleted: tests_abc.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
tests/
git add .
git commit -m 'begin organizing
>
>
> '
[main 042a42e] begin organizing
8 files changed, 19 insertions(+)
rename _config.yml => docs/_config.yml (100%)
rename _toc.yml => docs/_toc.yml (100%)
rename overview.md => docs/overview.md (100%)
rename test_alt.py => tests/test_abc.py (100%)
rename test_help.py => tests/test_alt.py (100%)
rename test_imp.py => tests/test_help.py (100%)
rename tests_abc.py => tests/test_imp.py (100%)
ANd finally push
git push
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (11/11), 1.72 KiB | 1.72 MiB/s, done.
Total 11 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/introcompsys/github-inclass-fa23-brownsarahm.git
5c8aaa9..042a42e main -> main
5.13. experience badge with a file#
Run the action to create the branch for today’s experience badge
Open a PR to propose changes from your prepare work brach to the branch for today’s experience badge
merge that PR
See that in your experience badge, the prepare work is now visible
fill in your experience badge
request a review from the TA that is in your group
5.14. Prepare for Next Class#
Review your software.md file from last prepare
Review the notes from 2023-09-21
Bring git questions or scenarios you want to be able to solve to class on Thursday (in your mind or comment here if that helps you remember)
Update your
.github/workflows/experienceinclass.yml
file as to add another paramter to the last step (Create Pull request
)reviewers: <ta-gh-name>
where<ta-gh-name>
is the github user name of the TA is in your group. You can see your group on the organiation teams page named like “Fall 2023 Group X”. Do this on a branch for this issue.
5.15. Review today’s class#
badge steps marked lab are steps that you will be encouraged to use lab time to work on.
Update your KWL chart with the new items and any learned items.
Clone the course website. Append the commands used and the contents of your
fall2023/.git/config
to a terminal_review.md (hint: history outputs recent commands and redirects can work with any command, not only echo). Edit the README.md, commit, and try to push the changes. What happens and what GitHub concept that we have not used yet might fix it? see yourvocab-
repo for a list of key github concepts. (answer in theterminal_review.md
)lab Organize the provided messy folder in a Codepsace (details will be provided in lab time). Commit and push the changes. Answer the questions below in your kwl repo in a file called
terminal_organization.md
clone your
messy_repo
locally and append thehistory.md
file to yourterminal_organization.md
Find your team’s repository. It will have a name like
fa23-team#
where#
is a number 1-4. Join the discussion on that repo about naming your team. Link to your comment directly in your PR for this badge (use the 3 dots menu to get the comment specific URL).
# Terminal File moving reflection
1. How was this activity overall? Did this get easier toward the end?
2. When do you think that using the terminal will be better than using your GUI file explorer?
3. What questions/challenges/ reflections do you have after this exercise?
## Commands used
5.16. More Practice#
badge steps marked lab are steps that you will be encouraged to use lab time to work on.
Update your KWL chart with any learned items.
Get set up so that you can pull from the introcompsyss/fall2023 repo and push to your own fork of the class website by cloning the main repo, then forking it and adding your fork as an additional remote. Append the commands used and the contents of your
fall2023/.git/config
to a terminalpractice.md (hint:history
outputs recent commands and redirects can work with any command, not only echo). Based on what you know so far about forks and branches, what advantage does this setup provide? (answer in theterminal_practice.md
)lab Organize the provided messy folder (details will be provided in lab time). Commit and push the changes. Clone that repo locally.
For extra practice, re/organize a folder on your computer ( good candidate may be desktop or downloads folder), using only a terminal to make new directories, move files, check what’s inside them, etc. Answer reflection questions in a new file, terminal_organization_adv.md in your kwl repo. Tip: Start with a file explorer open, but then try to close it, and use only command line tools to explore and make your choices. If you get stuck, look up additional commands to do acomplish your goals.
Find your team’s repository. It will have a name like
fa23-team#
where#
is a number 1-4. Join the discussion on that repo about naming your team. Link to your comment directly in your PR for this badge (use the 3 dots menu to get the comment specific URL).
# Terminal File moving reflection
1. How was this activity overall Did this get easier toward the end?
2. How was it different working on your own computer compared to the Codespace from?
3. Did you have to look up how to do anything we had not done in class?
4. When do you think that using the terminal will be better than using your GUI file explorer?
5. What questions/challenges/ reflections do you have after this?
6. Append all of the commands you used in lab below. (not from your local computer's history, from the codespace history)
5.17. Experience Report Evidence#
redirect the history to a file
history >> makeup_2023-09-21.md
then move the file created to your KWL repo on your experience report branch.
5.18. Questions After Today’s Class#
5.18.1. Why does git not see new directories added to the repo while they’re empty?#
git only tracks files not directories. It also makes note of the the path to each file.
5.18.2. After a new directory has new files in it git status only shows the direcrory name but not the files within it. Why is that?#
It is saying that there is a file there that is untracked, but leaves it at the directory level to give you the choice to add/ignore the whole directory at once
5.18.3. If possible, is it common to echo a line of code into a programming file?#
Yes!
5.18.4. What other experience will we have with open source projects in this class?#
We will use mostly open source tools and you can do an explore or build to learn more.
5.18.5. How can the “*” be used consistently to move a bunch of file? because we used it with the text infront of the similarity and behind it. i’m just wondering how it’s used for multiple files, because it was used both before the similarity and after the similarity.#
It fills in for any number of characters.
5.18.6. I have some badges from 2 weeks ago that are awaiting a second review after changes had been fixed.#
Important
Do not merge an unapproved badge
Re-request a review
5.18.7. can you close pull requests on the terminal?#
Not with git because pull requests are not a git feature, but it is a feature of github. The gh
CLI can do this.
5.18.8. If you do mv * will it move all files?#
In the current working directory.
5.18.9. Are there any other ways of using mv that haven’t been covered yet?#
No those are basically the two purposes.
5.18.10. how often should we be practicing with the terminal for git?#
Ideally, you work on badges on at least several of the days we do not have class so that you are working with it close to every day.
You could also start trying to use them for your other classes.
5.18.11. Would it be beneficial to organize files with github rather than bash?#
GitHub cannot organize files and doing do in broswer would be slow and difficult. We will see that GitHub code spaces give us a virtual machine that we can work with.