1 of 35

Working with Git Bash

JSC270 Lab 4

2 of 35

Acknowledgment

Thanks to Matthew Edwards, TA for JSC270 in 2021, for the original version of these slides!

1/37

3 of 35

Last Week: Git

Recall last week, we learned about version control

Git is a version control system used to track changes in sets of files (called repositories, or repos )

It was originally designed for computer programmers to collaborate during software development

Today, almost all data science teams (in both academia and industry) use it to track their work

The system includes a small (but very powerful) set of commands for editing and tracking files, including:

push

pull

add

commit

clone

branch

2/37

4 of 35

Last Week: GitHub

If the goal is collaboration, these file collections (aka repos) should be stored somewhere easily accessible

GitHub is an internet hosting service designed to store Git repositories. It allows for easy interaction with Git

It is not the only service that does this (e.g. GitLab, BitBucket, SorceForge), but it is by far the most popular and free it is free!

Git and GitHub can track multiple branches (or versions) of the same repository, so contributors can make changes in parallel without affecting the main/master branch

This is particularly useful if you’re experimenting (ie. you’re not sure if your changes will actually work)

3/37

5 of 35

A Basic Git Workflow

Here is a basic step-by-step process for managing repos:

1. You have a new repo you’d like to work with. Either you’ve created it yourself, or it is someone else’s public repository.

Recall from last week: Only you (the repo creator) and contributors you allow can see a private repo.

2. You Fork or Clone that repo to your local machine (e.g into your local gitrepos folder). More on this later.

3. You edit files in the local repo (or create new ones). If you’re experimenting, you would first create a new branch, so your changes do not affect main.

4/37

6 of 35

A Basic Git Workflow

4. You submit any changes you’ve made so they can be tracked by Git. This is a two-part process:

i) Use the add command to move changes to the staging area. This tells Git that you have changes to be included in your next commit.

ii) Use the commit command to save these changes to your local repo. This effectively takes a ’snapshotof your files.

Note:

Committed snapshots are safe - Git will never change them unless you tell it to.

Every commit you make is recorded and tracked, so you may revert back to past versions of your project at any time.

5/37

7 of 35

A Basic Git Workflow

5. (Optional) If you were experimenting on a different branch, you can merge your newer version back into the main branch.

6. You use the push command to push your commit(s) from your local repo to the remote repo (on GitHub). Again, more on this later.

6/37

8 of 35

A Basic Git Workflow

7. If other contributors are following a similar process, it’s likely they will push changes to the remote (GitHub) repo that you do not have locally. Use the pull command to to update your local repository to the most current version.

Note:

In this course, you will be the only ones editing your assignment files (although we need contributor access to read them), so steps 5 and 7 may not be necessary.

But in practice, there could be many contributors, and the remote repo might be ahead of (upstream from) your local version. So a good rule is to always pull before you make changes.

7/37

9 of 35

Question Time

Any questions about Git or GitHub (or about anything else so far)?

8/37

10 of 35

What is Bash?

So what does any of this have to do with Bash?

A command line interface (CLI) is an interpreter that processes text commands to allow interaction with your computer’s operating system.

The Born-again Shell, or Bash, invented in 1989, is one of the most popular CLIs available

Bash is designed to work seamlessly with Git, but also makes it very easy to navigate through the files on your computer

Every step in the workflow above can be done in Bash

9/37

11 of 35

Installing Bash (from last week!)

Mac Users: Good News! Apple computers use a version of Bash as the default CLI.

To open the shell, simply use Command + Spacebar to launch Spotlight, then search Terminal and double-click the search result

Alternatively you could click through Applications -> Utilities and find Terminal in there

10/37

12 of 35

Installing Bash (from last week!)

Windows Users: Unfortunately Bash is not the default, so we’ll have to install it separately

1. Go to the install page: https://gitforwindows.org/

2. Click the ’Downloadbutton

3. Once downloaded, run the .exe file

4. "Do you want to allow this app to make changes to your device" -> Yes

5. Use the default settings. Keep clicking Yes, and then eventually, click Finish

11/37

13 of 35

The Shell

For Windows, the application is called Git Bash.

You can open this by searching ’Git Bashin the Windows Start menu

12/37

14 of 35

Exercise 1

Windows users, go ahead and install Git Bash using the instructions above, if you haven’t already. Then open the Bash shell

Mac Users can just open the terminal. You can type “terminal” into the spotlight search to open up terminal.

13/37

15 of 35

Navigating Your File System

Your computer stores files in a tree structure

The top of the tree is called the root

Individual files are grouped together in folders, known as directories

Some directories can be made into git repos

*When you open a new bash shell, you are automatically at the root of your file tree

14/37

16 of 35

Navigating Your File System

The bash shell (Windows or Mac) will indicate that it is ready to receive your commands by prompting you with a $

To identify where your shell is looking in your file tree, you can use the pwd command (print working directory) to print your location. Try it yourself (type the command in, and press enter)

Use the ls command to ’listall the files in your current directory (this may include other directories)

15/37

17 of 35

Navigating Your File System

Suppose we want to move further down our file tree:

We just used ls to identify subdirectories (folders) within our current directory.

To move into one of these subfolders, type:

cd <dir name> (which directory you choose is up to you) This command stands for ’change directory

You have now moved into the subdirectory, and can view the contents of this new folder with the ls command (see for yourself!)

16/37

18 of 35

Some Other Useful Navigation Tricks

Question: What if we want to go up the file tree instead of down?

Answer: Bash uses the argument ../ to mean ’the parent directory of my current location

So by typing:

cd ../

I move up one level of the tree.

Note that cd also accepts longer file paths so by typing:

cd ../../../

I would move up three levels (assuming you are at least 3 levels down)

17/37

19 of 35

Some Other Useful Navigation Tricks

After a few commands in a row, you’ll probably be close to the bottom of the shell:

Use the clear command to remove the history shown in the shell (Bash still maintains your current directory, which you can confirm using pwd or ls)

When moving down several directories at once, you can use the TAB key to autocomplete directory names as you type (this assumes the name is unique):

cd Documents/gitrepos/JSC270.github.io/

18/37

20 of 35

Question Time

Any questions about file navigation?

20/37

21 of 35

Creating, Removing, and Renaming Directories

We can do more than look at our file tree: we can add new directories

We use the command mkdir <newdir name> to create a new directory (here, <newdir name> is something you choose)

The command rmdir <dir name> will remove an existing directory

You can use mv <oldname> <newname> to rename a directory. This command stands for ’move’, since you can also use it to move a directory

21/37

22 of 35

Forking or Cloning Existing Repos

When you clone a remote repository, you’re just making a copy of that repo on your local machine

Anyone can clone any public GitHub repo

However, if you’re not a contributor on the repo you cloned: 1. You cannot push local changes you make back to the remote (GitHub) repo

2. You cannot pull new changes made by the contributors to update your local version (you would have to re-clone that remote repo if you wanted to update)

The command to clone a repo is:

git clone http://github.com/username/repo-name where username and repo-name depend on the repo

23/37

23 of 35

Forking or Cloning Existing Repos

If you want to add to a GitHub repo but aren’t the creator/contributor, you first need to fork that repo.

Forking creates a copy of the repo you want on your account. Your copy is connected to the original repo, but changes you push to your forked copy do not affect the original

24/37

24 of 35

Forking or Cloning Existing Repos

You can fork a repo using the “forkbutton in the upper right corner on the GitHub repo page (see previous slide)

Then you would clone your forked copy to your local machine (as before)

To actually change the original repo, you would:

1. Make changes locally, and push them to your forked copy (usually on a separate branch)

2. Submit a pull request to the original repo owner, so they can merge the changes on your fork into the original remote repo

25/37

25 of 35

Exercise 3: Cloning Your Repo

Let’s make a new repo and clone it:

1. You can copy the link to your remote repository using the green button on GitHub

27/37

26 of 35

Exercise 3: Cloning Your Repo

2. Make sure you’re actually in your gitrepos directory (use the cd command if you’re in its parent directory)

3. Clone your remote repo to your local machine by typing:

git clone <repo weblink>

28/37

4. Change the directory to the cloned repo

27 of 35

Creating Files

In addition to creating directories, we can also create files:

Use the command

touch <filename>

to initialize a file (e.g. touch example.txt)

Then use

start <filename> or open <filename>

to open and edit that file

We can use a similar syntax to open an application without specifying a file name:

e.g. start firefox

To remove a file use:

rm <filename>

29/37

28 of 35

Question Time

Any Questions?

30/37

29 of 35

Exercise 4: Adding a Text File to Your Repo

Using the touch and start commands, create a file in your assignment 2 repo called prereq.txt

In this text file, please indicate all the different types of statistical distributions you’re familiar with (ie the ones you’ve worked with before)

You can separate them with commas, or put one distribution on each line (the exact formatting doesn’t matter)

The next page contains a list of distributions for you to reference

31/37

30 of 35

Exercise 4: Adding a Text File to Your Repo

Some types of distributions:

Uniform

Normal

Bernoulli

Binomial

Exponential

Geometric

Hypergeometric

Poisson

Chi-squared

Student-T

F Distribution

Gamma

Beta

Multinomial

Negative

Binomial

Cauchy

Laplace

Pareto

Rayleigh

Weibull

32/37

31 of 35

Tracking and Committing Changes

Once you’ve modified existing files or created new ones in your local git repo, we can use the process from last week to commit those changes:

1. Make sure you’re in your git repo (use the cd command if not)

2. Use git status to check for modifications to the local repo

3. Use git add <filename> or git add . to stage your revisions

4. Use git commit -m “Messageto commit the changes (take a snapshot)

5. Push the newest commit to the remote repo with git push

33/37

32 of 35

Exercise 5: Committing Your New File

Go ahead and follow the steps above to add, commit, and push prereq.txt to your remote repository for Lab 3

34/37

Example: Cloning ‘RareEventsMeta’ forked repo and adding and opening example.txt.

33 of 35

Exercise 5: Committing Your New File

You’ll notice Bash uses different colours to separate modifications that haven’t been staged (red) from those that are staged but not committed (green)

35/37

34 of 35

Exercise 5: Committing Your New File

You’ll know you’ve succeeded when you get a message similar to:

36/37

35 of 35

Conclusion

In terms of Bash’s capabilities, this is just the tip of the iceberg. You can:

Customize the look of the shell

Run programming files directly from the command line

Alias certain commands (ie customize the shell commands to your liking)

But, we’ve covered everything you’ll need for JSC 270. Questions?

37/37

An alternative to using Bash is the GitHub Desktop App