Training Material:
Instructors
- “Piled Higher and Deeper” by Jorge Cham, http://www.phdcomics.com
Microsoft Word’s Track Changes
Google Docs’ version history
LibreOffice’s Recording and Displaying Changes.
Version control systems start with a base version of the document and then save just the changes you made at each step of the way.
Better kind of backup.
Version control is like an unlimited undo.
Review history.
Restore older file versions.
Ability to undo mistakes.
Maintain several versions of the code at a time.
Version control also allows many people to work in parallel.
You can think of it as a tape: if you rewind the tape and start at the base document, then you can play back each change and end up with your latest version.
Once you think of changes as separate from the document itself, you can then think about “playing back” different sets of changes onto the base document and getting different versions of the document.
Unless there are conflicts, you can even play two sets of changes onto the same base document.
There are many different version control systems:
There are many different version control systems:
Git is not GitHub
version control system tool
hosting service for Git
repositories
https://gitforwindows.org/
You’ll know it has worked when you can open a Git Bash terminal (the window should have a title that starts with MINGW32) and get the Git version by running:
$ git --version
Here is how Jane sets up her new laptop:
$ git config --global user.name "Jane Smith"
$ git config --global user.email "jane.smith@university.ac.uk"
we’ll be setting up an account on Github so make sure you use the same email address.
You can check that they have been set correctly by running
$ git config user.name
$ git config user.email
$ git config --global color.ui "auto"
$ git config --global init.defaultBranch main
$ mkdir infammation
$ cd inflammation
$ git init
Using command line:
What happened?
$ ls -a
. .. .git
Git stores information about the project in this special sub-directory.
If we ever delete it, we will lose the project’s history.
$ git status
$ rm -rf .git
Be careful! Running this command in the wrong directory, will remove the entire git-history of a project you might wanted to keep.
Therefore, always check your current directory using the command pwd.
$ git status
fatal: Not a git repository (or any of the parent directories): .git
First, lets check the status
Git repositories can interfere with each other if they are “nested” in the directory of another: the outer repository will try to version-control the inner repository. Therefore, it’s best to create each new Git repository in a separate directory.
use pdw to ensure that you are in the correct directory
use git init to create a git repository
use git status to ensure that git is working
Create a file called project.txt
Type the text below into the project.txt file:
Some initial data analysis to identify how inflammation changes over time after surgery.
$ git status
The untracked files message means that there’s a file in the directory that Git isn’t keeping track of.
We can tell Git to track a file using git add
$ git add project.txt
$ git status
Git now knows that it’s supposed to keep track of project.txt, but it hasn’t recorded these changes as a commit yet.
To get it to do that, we need to run one more command: git commit -m "some message"
$ git commit -m "Start notes on the patient inflammation project"
$ git status
Now suppose Jane adds more information to the file. Update your text to look like:
Some initial data analysis to identify how inflammation changes over time after surgery.
Jane is a Data Scientist and Samit is a statistician. We'll need to determine
who is responsible for what in this project.
$ git status
$ git diff
Now commit your change!
$ git commit -m "Add note about project responsibilities"
Whoops: Git won’t commit because we didn’t use git add first. Let’s fix that:
$ git add project.txt
$ git commit -m "Add note about project responsibilities"
use add to put your changes in the staging area
use git diff to check the differences between your last commit and your file
use git commit -m to commit the staged changes
$ git log
We can refer to commits by their identifiers:
$ git diff <commitID1> <commitID2>: show you any difference betwenn the commit <commitID1> and <commitID2>
$ git diff HEAD HEAD~1
$ git show <commitID>: show the commit log message
$ git show cf53ab40ddc732676c7e7a65063629b5df1c96f5
As you might guess from its name, git checkout checks out (i.e., restores) an old version of a file.
To use this command we need to provide two information, the commit identifier and the file name, e.g.:
$ git checkout da43c77 project.txt
After restore an old version, you still need to add and commit the change. Now see the log! Note that even when restoring an old version, you do not overwrite/lose commits already made.
Let’s create a few dummy files in the inflammation directory:
$ mkdir results
$ touch a.dat b.dat c.dat results/a.out results/b.out
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.dat
b.dat
c.dat
results/
nothing added to commit but untracked files present (use "git add" to track)
Let’s create a .gitignore file, and inside this file:
*.dat
results/
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
$ git add .gitignore
$ git commit -m "Add the ignore file"
Let’s check our branches
$ git branch
Create a new branch named experimental
$ git branch experimental
We can switch to a different branch by using the git checkout command.
$ git checkout experimental
Create an "experimentalfile.txt"
add some text
Our experiment is performed as follows:
1. ....
2. ....
3. ....
Now add and commit this change
$ git add experimentalfile.txt
$ git commit -m "creating experiment description file"
First go back to the main branch
$ git checkout main
Now create a new "mainfile.txt" and add some text on it.
Navigate around your folders and files, change branches and do the same. What did you notice?
Add changes in the desired branch
$ git checkout main
$ git add mainfile.txt
$ git commit -m "creating the mainfile"
Again: navigate around your folders and files, change branches and do the same. What did you notice?
$ git checkout main
$ git merge experimental
Again: navigate around your folders and files, change branches and do the same. What did you notice?
Open the "project.txt" file in the main branch and delete the entire last sentence. Don’t forget to add/commit the change.
Switch to the experimental branch, open the file and instead of deleting the sentence, just make a small change to it. Again be sure to commit the changes
Finally, merge experimental into main:
Now we’re going to purposefully provoke a conflict.
$ git checkout main
$ git merge experimental
Open the "project.txt" file in the main branch and Fix the conflict.
add/commit!
Auto-merging project.txt
CONFLICT (content): Merge conflict in project.txt
Automatic merge failed; fix conflicts and then commit the result.
It is recommended to delete any secondary branches after merging them with main.
To delete the experimental branch:
$ git branch -D experimental
Deleted branch experimental (was 3099b91).