Git and Bash beginner notes - ovokpus/MLOps-Learn GitHub Wiki

Clone this repo:

git clone https://github.com/cloudacademy/example-git-repo.git

Confirm the repository cloned by entering the following commands:

cd example-git-repo
ls
git status

The git status command will show you any differences there are in files between the last commit (or snapshot) and now. Since you only recently checked out the repository and haven't made any changes, there are no differences to show. If there were changes to any files they would be displayed here. How they're displayed and handled, however, depends on whether the file is tracked.

Files in Git can be two types: tracked or untracked. Tracked files are those that were present in the last snapshot, or commit, made to the repository. Tracked files can be unmodified, modified or staged. If a tracked file is unmodified, then it hasn't been edited since the last commit. If it's been edited, it'll be a modified file. Modified files won't be included in a commit until they become staged files, which happens with a git command you will use later in the Lab Step. Staged files are modified files that have been added to the staging area. A staging area is a group of files that will be included in the next commit. Files in Git can be modified but not staged, meaning you have a lot of flexibility in what you want to include in any commit.

Untracked files are all other files in the directory. They're files that weren't present in the last commit or have been designated as ignored files, which you will learn about later in this Lab Step.

From that, you can deduce that all files in your repository are tracked, unmodified files. Since you just cloned the repository and haven't added any new files, there aren't any untracked filed. Similarly, since you haven't edited any filed, no files are modified or staged. Next, you'll take a file through the pipeline from untracked to staged and committed.

Run this command:

touch about.html

This will create a file in your repository called about.html

Re-run the git status command:

git status

Notice the untracked files section. The new file, about.html, appears here. This is because the file has been created, but not added to Git. Git won't include this file until it's been added to the staging area and then committed.

Run these commands:

git add about.html
git status

The git add command adds a file to the staging area. To reiterate, any files in the staging area will be included in the next commit made, meaning the next time you make a commit it will include (and therefore start tracking) the about.html file. Before you can make a commit, however, you'll need to tell Git who you are, so it knows to whom the file changes should be attributed.

Run the following commands (Replace the user name and email values with your github user name and email address):

git config user.name "Cloud Academy Student"
git config user.email "[email protected]"

Commit your changes with the folliwing commands:

git commit -m "Add an about file"

The -m flag allows to you pass a commit message when making the commit.

Running git status once more shows that as expected, there are no longer untracked, modified or staged files:

Also, notice the phrase Your branch is ahead of 'origin/master by 1 commit". This means that the "origin" remote (in this case, the Github repository you cloned this codebase from) doesn't know about this commit yet. You can let the "origin" remote know about this by using a git push command, which is beyond the scope of this Lab but is covered in another Git Lab.

You just created an untracked file and added it to Git. Next, you will work with a tracked file.

Edit and view a tracked file with this command:

echo "This is my about page" > about.html
git status

Notice that now, instead of an Untracked files section, you have a Changes not staged for commit section, with one modified file in it. Because about.html was present in the last commit, it's a tracked file now. Any changes to the contents of the file will switch the file from unmodified to modified.

Stage the about.html file and make a new commit with the newest changes:

git add about.html
git commit -m "Add a message to the about file"

View all commits made so far:

git log

The git log command displays all commits made, with the newest commits on top. As expected, the two commits you made are the first and second commits in the output.