11  Git basic workflow

11.1 Overview

In this exercise, we will practice basic Linux command lines, including ls, cd, mkdir, touchand basic git commands including git status, git add, git commit, and git log.

Once we are able to move around in the shell, create files and directories, then we are ready for the basic git workflow, which is:

  1. edit a file
  2. save those edits
  3. git add
  4. git commit -m "commit message"

edit → save → add → commit

11.2 Working with files and the commandline in Rstudio

Goal of this step: get familiar with basic Linux command lines: ls, cd, mkdirand touch.

Log in to the R studio. The interface is shown like below.

Type ls in the terminal. It will list the files and directories in a specified directory or the current working directory if no directory is specified.

Type mkdir air_plane_exercise in the terminal. It will create a new directory with a specified name in the file system.

Type ls in the terminal to check the files and directories in current working directory.

Type cd air_plane_exercise in the terminal to change the current working directory to air_plane_exercise folder.

Type touch instructions.txt in the terminal to create a new text file named “instructions” inside the air_plane_exercise folder. You can type ls to check the updated files and folders.

Now go to the files explorer, double click the “air_plane_exercise” folder and then enter the “instructions.txt”. Enter something in the text editor. Save the file.

11.3 Creating a git repo,

Goal of this step: learn git init.

Type git init in the terminal. The git init command is typically used to start a new repository or to convert an existing project into a Git repository.

11.4 Git status, add, commit

Goal of this step: learn git status, git add, and git commit

Make some changes in the “instructions.txt” and save it.

Type git status in the terminal to show the state of the working directory and the staged changes. You will see untracked files.

Type git add in the terminal to stage changes for a commit. It allows you to select which changes in the working directory will be included in the next commit.

Type git status in the terminal, you will see “changes to be committed”

Type git commit -m "paper plane2"to save changes to the local repository. It takes all of the changes staged with git add and stores them as a new commit with a message describing the changes.

Type git status to check status again. It should show “nothing to commit, working tree clean”.

Make some changes:

  • edit the “instructions” text file, save it, git add it, git commit it
  • create another text file named “license”, save it, git add it, git commit it

11.5 Using git log to understand what is in the repo

Goal of this step: learn git log

Type git log in the terminal to display the commit history. It will show a list of all commits in the current branch, along with information about the author, date, and commit message for each one.

The latest message will show at the top and the oldest will show at the bottom.

You can also use git log --oneline --abbrev-commit --all --graph to show git log in short.

In class we will mostly add --color to this as well:

git log --oneline --abbrev-commit --all --graph --decorate --color

You can learn more about reading the output of that command here: https://stackoverflow.com/questions/22313343/git-graph-what-do-the-lines-and-asteriks-denote

The * characters show a single commit, the lines help us understand the branches.

Here are the review of Linux command lines and Git command lines

11.6 Linux Command Lines

  1. cd → change directory

    cd () is to change the current working directory to a specified directory.

  2. ls → list files

    ls (list directory contents) is to list the files and directories in a specified directory or the current working directory if no directory is specified.

  3. pwd → present working directory

    pwd shows the full path to the current directory. It can help understand how the files look. tree .. is another useful option (this goes up a directory using the two dots: .. and tree shows the files in a hierarchical diagram.)

  4. mkdir → make new directory

    mkdir (make directory) is to create a new directory with a specified name in the file system.

11.7 Git

  1. Git add

    git add is a command used in Git to stage changes for a commit. It allows you to select which changes in the working directory will be included in the next commit.

  2. Git commit -m “message”

    git commit saves changes to the local repository. It takes all of the changes staged with git add and stores them as a new commit with a message describing the changes.

  3. Git status

    git status shows the state of the working directory and the staged changes, and it indicates which branch you are currently on.

  4. Git log

    git log is to display the commit history of a repository. It shows a list of all commits in the current branch, along with information about the author, date, and commit message for each one.