Lecturer: Michael Lydeamore
Department of Econometrics and Business Statistics
Aim
file1.qmd
-> file2.qmd
-> … -> file25.qmd
file20.qmd
Definition: Version Control System
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. Git manual
Version control systems are a category of software tools that help store and manage changes to source code (projects) over time. They can:
It is a very useful (actually essential!) tool for collaborating and for sharing open source resources.
Local version control systems
Centralized version control systems (CVS) are now the standard for version control
Distributed version control systems
We are going to use a distributed version control called Git
Git was created by Linus Torvalds in 2005 for development of the Linux kernel and since then many other kernel developers have been contributing to its development. The maintainer since 2005 is Junio Hamano.
As with most other distributed version-control systems, and unlike most client–server systems, every Git directory on every computer is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server.
More info here
Let’s think of the connections between the different versions of an R project as a tree (Git tree).
Why?
Also known as the Shell, command line interface (cli) or terminal is an interface for typing commands to interact directly with a computer’s operating system.
Typically when you open your terminal, it will welcome you with a prompt that looks like this:
patricia@computerid-macbook:~$
or with the new Catalina Mac OX
patricia@computerid ~ %
On Windows it will contain the same elements but look like this:
patricia@computerid-pc MINGW64 ~$
We will start writing commands after ~$
or ~%
depending on the terminal version that you are using
The commands that we are going to use are the same regardless the terminal version you have.
pwd
: print working directory or present working directory
michael@computerid ~ % pwd
/Users/michael/Documents/ETC5513
pwd
command:/Users/michael/Documents/ETC5513
/
represents the root directoryUsers
is the Users directorymichael
refers to my directory or folder within the users directoryls
lists the files inside the current directory
michael@computerid Documents~ % ls Documents
Courses Research Teaching file.pdf example.txt
Documents
is an argument to the ls
command.ls
gives you a list of all the elements in a directoryls -a
list of all the files including hidden onesEach Linux command (pwd
,ls
…) have lots of options (flags) that can be added.
A reference list of unix commands with options might be found here
cd
: Change directory
pwd
).cd
command syntax is very simple, we just need to specify the directory that we want to navigate topwd
command to confirm your current location/
is assumed to be absolute.cd
in practice!Documents
. I want to get to Documents/Research/COVID
cd Research
means that we move into Research
cd COVID
means that we move into COVID
.
means the current directory COVID
cd ..
means (parent directory) that we move back into Documents
~
symbol is a shorthand for the user’s home directory and we can use it to form paths:
Downloads
directory (/Users/John/Downloads
) typing cd ~
will bring you to your Home directory /Users/John
!COVID
, or really Documents/Research/COVID
..
is shorthand for the parent of the current working directorycd ..
means that we move into Research
(1 directory up). That is from COVID
back to Research
cd ../../
means that we move up two directories: from COVID
to Documents
mkdir Project1 Project2
means “make two new directories (folders) called Project1 and Project2”.mv
move files or folders: takes two arguments, the first being files or folders to move and the second being the path to move to.cp
this command is used to copy files or group of files or directories. When copy files we need to use cp -r
to copy all the directory contents.rm
remove files and foldersrm
requires the -r
(recursive) flagtouch example.qmd
Excellent summary about the commands that we will be using can be found here.
Please read and practice 1-5 sections once you have installed your command line interface/terminal/Git Bash Shell
Once you have found the terminal:
Open your Terminal and check if you have Git installed:
git version
git version 2.20.1 (Apple Git-117)
If Git is not installed in your Mac:
brew install git
git version
in your terminal.git version 2.20.1
If Git is not installed:
sudo apt-get install git
Also remember that in Moodle you have the Forum for discussion where you can talk to each other, share tricks and resources. Please make use of it!
To interact between our projects and Git, we are going to use the shell/command line interface
We have learned that Git is a distributed version control system.
Each version corresponds to one of the dots on the git tree.
The states in a Git repository are: the working directory, the staging area (index) and the git directory:
working directory
is the current snapshot that you are working on.staging area
(index) is where modified files are marked in their current version ready to be stored in the database (i.e. the index of changes).git directory
is the database where the history is storedIn your file system you will see the folder and the files of your project
More info: Begining Git and Github
Git has three main states that your files can reside in: modified, staged, and committed:
This leads us to the three main sections of a Git project: the working tree, the staging area, and the Git directory.
These actions occur in your local repository and allow you to create your Git tree!
Here you can sign up for a free GitHub account. Please use your Monash email address to create it before the tutorial this week.
If you already have a GitHub account and want to keep them all together, you can add another email to your current account.
Our goal is to have both our local and remote repositories synchronized
Let’s look at all these in more detail!
Important:
README.md
: It is important to have a README.md
file for every repository. GitHub will use this file as the “presentation” of the repository and should briefly describe what the repo is about.First open your command line interface/Terminal/Git Bash Shell:
First of all we need to get your Git configured in Rstudio (the same follows for your own computer):
Open your command line interface/Terminal/Git Bash Shell and type:
git config --global user.email "your.email@example.com"
git config --global user.name "Your_Firstname Lastname"
Make sure you use the same email address for this and for setting up your GitHub account.
To check that everything is set up correctly, type the following in the CLI:
git config --global user.email
and git config --global user.email
Alternatively you could type the following in your R console inside Rstudio:
When you create a repository on GitHub, it exists as a remote repository.
Users can clone your repository to create a local copy on their own computer and sync between the two locations.
More detailed info here
In the tutorial you will learn how to add an SSH key into your workflow:
From our shell or command line: 1. Navigate to the computer location where we want to download the github repo 2. git clone git@github.com:okayama1/Git_demo.git
3. This will create a folder in your computer with the github repository files and folders
Example:
git add
git commit
) capturing a snapshotgit push
and update filesIn a git repository tracked files are those which are part of the git repository
However, we can also have untracked files for which their history is not tracked
Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.
Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area.
We will learn more about this in the coming weeks.
But if you want some early reading, have a look here
git clone "remote repo address"
is a Git command line utility which is used to target an existing repository and create a clone, or copy of the target repository in your local computer.git add filename
is a Git command that adds a change in the working directory to the staging area.git commit -m "Message"
: The Git commit command captures a snapshot of the project’s currently staged changes. (m = message for commit. The git commit is used to create a snapshot of the staged changes along a timeline of a Git projects history.)git push origin master (or main)
: The git push command is used to upload local repository content to a remote repository, in this case to the master (or main) branch.Clone pulls from the GitHub repo
Working in your computer and updating the remote repo in GitHub
You typically clone a repo only once. After that each time you work on your repo you will use the following Git commands:
git pull origin "branch name"
is used to fetch and download content from a remote repository (after the repo has been cloned) and immediately update the local repository to match that content.git status
displays the state of the working directory and the staging areagit add file_name
adds changes in the working directory to the staging areagit commit -m "Message"
is used to create a snapshot of the staged changes along a timeline of a Git project historygit push origin "branch name"
command is used to upload the local repository content to a remote repository in GitHubExcellent summary about the commands that will be using can be found here
We can write our commit messages like this:
git commit -m "Message"
Alternatively, we can also use an external editor. This is useful when we want to create extended commits and if we forget to use git commit -m
and we type git commit
Install VSCode here.
Then set VSCode as follows:
We need to set VS code as the editor for Git by typing the following in the command line interface/Terminal/Git Bash Shell:
git config --global core.editor "code --wait"
You can check if this has worked by committing without using -m
by just typing in your cli git commit
.
Then the VS Code editor will open and you can write your commit directly there.
If you are a Windows user and this does not work for you, you might need to set up your VSCode path manually.
First of all, learn to manage your own projects on GitHub: get familiar with committing, pushing and pulling.
RStudio keeps git constantly scanning the project directory to find any files that have changed or which are new.
The Diff window shows what has changed between the last committed version of a file and its current state.
Super easy: - After staging the files you want to commit… - Write a brief message (first line short, then as much after that as you want) and hit the commit button.
Allow us to understand past commits.
In this unit you must learn how to use Git via the command line interface/Terminal/Git Bash shell as that is the universal way of using git and it will be very useful for using with any programming language.
Assignment 1 will be released on Wednesday of this week (Week 3)
It will be due at 11:55pm on Thursday, March 30th (Week 5).
Summary
ETC5513 Week 3