4. Git Offline#


4.1. Absolute Path vs Relative Path#

Navigate to your inclass repo on your terminal

pwd
/c/Users/ayman

Which command will help me navigate between folders?

  • [ ] ls

  • [x] cd

  • [ ] pwd

  • [ ] mkdir

Note

Remember: cd stands for change directory


What type of path do I use with the command cd?

  • [ ] absolute path only

  • [ ] relative path only

  • [x] Either one will work


Using absolute path means you know how to navigate to your desired folder starting from the root /


The / is the starting point to where all your files and folders can be found


Using you relative path, means that you know how to reach your desired folder relative to where you are right now

How do I get to Tyler 052?

Absolute path: USA/Rhode Island/Kingston/Flag rd/Green house rd/Red building/052 Relative path: leave Ranger 302 (…)/ leave Ranger Hall (…)/ Through quad/ pass by engineering/ Red building/ 052


The / between folder names means “from here, go to” We can separate the cd command that contains / into multiple commands


cd Documents/ 
cd systems/

pwd
/c/Users/ayman/Documents/systems

Let’s try another way

cd ../../
pwd
/c/Users/ayman

cd Documents/systems/
pwd
/c/Users/ayman/Documents/systems

We got to the same destination


Watch your steps carefully for this one (Don’t hit Enter)

cd gh-

Hit Tab

cd gh-inclass-AymanBx

Now you can hit Enter


Note

The terminal determined that the existing folders/files that begin with “gh-” are limited to only one and helped you complete it with the press of tab


getting to GitHub from your local system


Now on the other side of things, navigate to your inclass repo on GitHub

the step below requires that you have the gh CLI.

gh repo view --web

What files/folders can you see on there?

.github/workflows

How many branches do you have?

main
1-create-an-about-file

Select the 1-create-an-about-file

Same files so far (No changes made yet)


Back to your terminal


Let’s go back to comparing files between online and local repos

ls -a

Note

We used -a as an option for showing us hidden files/folders (anything that starts with a .)

.          .git
..         .github

We’ll notice a few things here

We have three extra files/folders

We know what .. refers to by now. It’s a pointer to the parent direcctory

The . is a pointer to where I am right now (the current directory)


Let’s test out this theory

pwd
/c/Users/ayman/Documents/systems/gh-inclass-AymanBx
cd .
pwd
/c/Users/ayman/Documents/systems/gh-inclass-AymanBx

Our place didn’t change. It checks out!


4.2. .git#

The third difference was the .git folder


.git folder is created by the git tool that we use on our terminal to hold special information about the project we’re working on


Everytime we start a command with the word git we’re calling that tool and then telling it what we want it to do


So far we saw **git** status and **git** fetch


status & fetch were the commands that we asked git to execute

.git is a black box ( until furhter notice ;-) )
It holds tracking information about what changes were made locally and online

We use git fetch to update the .git box with changes that happed on the GitHub repo


Let’s try it

git fetch

If you got nothing then your .git box is already up to date with the online repo (project)


Check the current status of your project

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

git status is your friend, we will use it all the time to keep track of our movements


Notice two key lines here


Your branch is up to date with 'origin/1-create-an-about-file'. is telling you that your local project matches the online one at the moment (as far as it knows)


More importantly, nothing to commit, working tree clean is telling you that your local repo (everything out side of the .git folder) matches the tracking information of git (everything inside the .git folder)


Much more on that later


Lastly, one more small thing we noticed


Github showed us .github/workflows whereas ls -a only showed my .github

As discussed, the / tells us that this there is a file/folder within a folder

Github combined them in one because there are no other files/folders on the .github folder

How do we prove it?

ls .github
workflows/

Listing the contents of .github showed us that it contains the folder “workflows” with it


Note

Conclusion: The ls command can take an argument that is a path and it will list the contents of the folder passed in that path for us


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

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)

Notice: Working tree is not clean anymore.
There is one “untracked file”

This means the working directory (everything outside .git) had changes but those changes were not added to the .git box to be tracked


nano about.md 

What year are you in? When do you expect to graduate?


Ctrl + s (Only windows users) Ctrl + x Enter (Only mac users would need this)


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.


We put some content in the file, any content then saved and exit.

On the nano editor the ^ stands for control.

and we can look at the contents of it.

Now we will check again with git.


cat concatenates the contents of a file to standard out, where all of the content that is shown on the terminal is.

Note

Standard out is a special file in your device that your programs/commands executed will put their output into. The terminal prints the content of standard out (aka: std out), hence we get the output printed on the terminal

cat about.md 

and we can see the contents


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)

4.4. git add#

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.

We will use . as our “file” to stage everything in the current working directory.

git add .

And again, we will check in with git

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

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.


Notice: git tells you that if you changed your mind on that file and you don’t want it staged to be commited to the next “checkpoint” anymore, you can simply take it out of the staging area using the command git restore --staged <file_name>

Try this yourself

Try making a change, adding it, then restoring it. Use git status to see what happens at each point

4.5. git commit#

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.

Note

A commit is a “checkpoint” in your project that you want to save, to be able to go back at any point in time. A commit saves the status of all the files in the project that had been modified since the last check point and staged for the new commit

git commit -m "create about - closes #1"

We used a closing keyword so that it will close the issue.


Warning

When you make your first commit you will need to do some config steps to set your email and user name.

[1-create-an-about-file c7375fa] create about - closes #1
 1 file changed, 3 insertions(+)
 create mode 100644 about.md

one more check

git status

On branch 1-create-a-readme
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

Note

Now your working tree is clean again, but your local branch (as reflected inside the black box .git) now looks different from what’s on the online GitHub repo (Sometimes referred to as the “Upstream”)

4.6. git push#

Git suggests that we use the push command to update the online repo with the local one.


And push to send to github.com

git push

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 341 bytes | 341.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/compsys-progtools/gh-inclass-AymanBx
   98ca2d6..9e8a

Now check out your online repo. Make sure your on the “1-create-an-about-file” branch

about.md should exist now on there!

4.7. Summary#

flowchart TD shell --> |is used through| terminal zsh --> |is an example of a | shell bash --> |is an example of a | shell powershell --> |is an example of a | shell shell --> |interface to | os[Operating system] git --> |is a program used through | terminal

Another way to think abut things (and adds some additional examples to help you differentiate between categories and examples of categories)

flowchart LR subgraph programs subgraph shell bash zsh powershell end subgraph vcs[version control systems] git svn mercurial end end subgraph gh[git hosts] GitHub BitBucket GitLab end

Today’s bash commands:

command

explanation

pwd

print working directory

cd <path>

change directory to path

mkdir <name>

make a directory called name

ls

list, show the files

touch

create an empty file

We also learned some git commands

command

explanation

status

describe what relationship between the working directory and git

clone <url>

make a new folder locally and download the repo into it from url, set up a remote to url

add  <file>

add file to staging area

commit -m 'message'

commit using the message in quotes

push

send to the remote

4.8. Prepare for next class#

  • [ ] Make sure you’re inclass repo has two branches on github main & 1-create-an-about-file

  • [ ] Make sure you’re inclass repo has two branches locally as well using the command git branch on your terminal when you’re inside the inclass folder

  • [ ] Make sure you can see the about.md file on github when you select the 1-create-an-about-file branch and not the main branch

4.9. Badges#

  • [ ] Review the notes from 02-04

  • [ ] Ask questions about what we did in the Experience report for 02-04

  • [ ] Review the notes from 02-04

  • [ ] Ask questions about what we did in the Experience report for 02-04

  • [ ] In your own words, explain the process of making changes to a repo localy and then pushing them to your online repo in a file called local-change.md

4.10. Questions after class#