1 03-VersionControl


Previous: 01-02-LinuxBash.html

1.1 Introduction: three problems

1.1.1 1. How to keep past versions of your stuff?

For example, in a day’s work you may produce:

~> ls
draft.py
final.py
final_real.py
final_real_real.py
actually_done.py
actually_done_v1.py
actually_done_v2.py
actually_done_v2.1.py
actually_done_v2.1-2019-12-10.py
...

Ok, I guess I should use Git…

Sub-problem:
You had a working version of your code at the beginning of the day,
but at the end of the days work, it’s broken.

03-VersionControl/vc-terrible.jpg

1.1.2 2. How to collaborate by making copies of a document or code, and then re-integrate those changes.

For example:
* How to write code between 1000’s of people while everyone wants to work at once.
* How to re-write or draft a document (e.g., a constitutional amendment) at once with lots of people.

1.1.3 3. How to back up your code?

In case of fire:
git commit, git push, leave the building!

1.2 Version control, the git that keeps on giving

Version control is the Git…
Git is one byte short of a four-letter word.

1.3 Git

https://git-scm.com/
https://en.wikipedia.org/wiki/Git
03-VersionControl/git.png
* Git is software. It exists locally on your machine and other developer’s machines.
* Github, Gitlab, and BitBucket are websites (servers), that interface with end-users’ git software.
* They host their own versions of Git-compatible server software that hosts Git repositories and talks to local Git processes.
* Quite ironically, unlike GitHub (now a Microsoft product), Gitlab’s server-side software is actually https://en.wikipedia.org/wiki/Open_source, so anyone can host their own Gitlab website/server.
* Gitlab itself also has a rich positive development community.
* MST IT hosts two installations of Gitlab server-side software, on two different servers residing on campus (cool!!):
* https://git.mst.edu (permanent code, like lab code or personal projects)
* https://git-classes.mst.edu (class code, which gets deleted ever now-and-then)

03-VersionControl/gitcomic.png
* This is actually a good approach for now, if you break your “repo” but still have your code…
* Later, you will want to learn branching and conflict handling better.

1.3.1 Demo 1

#!/bin/bash

# Make a repository at https://git-classes.mst.edu
# Show the gitlab view of it

git clone
vim README.md
vim hello_world.py
# write, save, quit
git add .
git commit -m "my first repo!"
git push -u origin master

# show web interface

# edit a file locally
git push #?
git pull #?

# edit something in web, then, what happens?

git pull

1.3.2 Demo 2

Check out some real repositories
* https://github.com/explore
* For example:
* https://github.com/nasa
* https://github.com/LLNL
* https://gitlab.com/explore
* For example:
* https://gitlab.com/cryptsetup/cryptsetup
* https://gitlab.com/inkscape/inkscape

1.3.3 Extra background

1.3.4 Tracking changes

1.3.4.1 Git version control?

1.3.4.2 How does Git work?

1.3.5 Distributed

Distributed version control
03-VersionControl/distributed.png

1.3.6 Snapshots

Snapshots (commits) include all files
03-VersionControl/snapshots.png

1.3.7 Storage landscape

Three places where edits exist
03-VersionControl/areas.png

1.4 Gitting Started…

Pre-use configuration: these are just for meta-data, not login, etc.
* $ git config --global user.name "<YOUR NAME>"
* $ git config --global user.email <EMAIL>
* $ git config --global core.editor vim
* or your choice of text editor

1.4.1 Basic local use:

THESE SHOULD BE YOUR CONSTANT GO-TO:
* $ git status Shows the status of the repository.
* $ git diff Shows the diff of anything you have done from your last snapshot
* $ git diff fileofinterest.py
* $ git log --all --graph Shows a nice history

1.5 commit

$ echo hey >>README.md
$ git add README.md
$ git commit -m "a message"
$ echo hey >>README.md
$ git commit -am "b message"
$ echo hey >>README.md
$ git commit -am "c message"
$ git log -p --all --graph

++++++++++++++++++++++++++++
Cahoot-02c.1

1.6 branch

May the forks be with you!

$ git branch new-branch
$ git checkout new-branch
$ git checkout -b new-branch
$ git log -p --all --graph
$ echo hey >>README.md
$ git commit -am "d message"
$ git log -p --all --graph
$ git checkout master
$ git log -p --all --graph
$ git checkout -b another
$ git log -p --all --graph
$ echo hey >>README.md
$ git commit -am "e message"
$ git log -p --all --graph

1.7 diff for branches

$ git diff branch1..branch2

1.8 merge

Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.
$ git merge new-branch
$ git log -p --all --graph
$ git checkout master
$ git merge another
$ git log -p --all --graph

1.8.1 Merge conflicts (oh Fork! …)

CONFLICT (content): Merge conflict in the-file.txt
Automatic merge failed; fix conflicts and then commit the result.

In the-file.txt:

`<<<<<<< HEAD `
`The current branch's contents `
`=======`
`Stuff from the branch you're merging `
`>>>>>>> new-branch `

$ git add the-file.txt
$ git commit -m "message"

++++++++++++++++++++++++++++
Cahoot-02c.2

1.9 Exploration

1.9.1 Looking at stuff

$ git status shows summary data

$ git log Show a log of commits
--graph Neat ASCII graph
--all Shows all branches
-p Show what changed in each commit

$ git show firstfourofhashofcommit

$ git diff Show un-added, un-committed changes for all files
$ git diff firstfourofhashofcommit
$ git diff --cached shows diff with added but not committed changes
$ git diff branch1..branch2

1.10 Git happens

Now, how to clean up a mess?

1.10.1 Revert single file in latest commit

$ git checkout file.py

1.10.2 reverting changes

$ git revert help

1.10.2.1 Undoing stuff since a commit

To delete all local changes in the branch that have not been added to the staging area, and leave un-staged files/folders, type:
$ git checkout .

To undo the most recently added, but not committed, changes to files/folders:
$ git reset .

1.11 Remote repositories

1.11.1 Working with remotes

$ git clone <REPO_URL>
* makes a copy of a repository.

git push
$ git push
* Pushes changes from your current branch to the remote branch it tracks. (You may need to run $ git config --global push.default simple.)

For example, to push your local commits to the master branch of the origin remote:
$ git push origin master

git pull
$ git pull
* Pulls changes from the remote branch and merges them into your current branch

$ git remote -v
* To view your remote repositories

$ git remote add <REMOTE_NAME> <REPO_URL>
* adds a remote to an existing repository.

For projects you work on:
a ‘git pull’ a day, keeps the conflicts away

If there may be remote changes,
then commit before pull!

$ git commit -am "always commit before pull
$ git pull

++++++++++++++++++++++++++++
Cahoot-02c.3

1.12 Working with others

1.12.1 Collaboration

1.12.2 Blaming your collaborators

When you need a scapegoat for that critical mistake in your code-base…
$ git blame help

1.12.3 Commit early, commit often!

A tip for version control, not for relationships…

++++++++++++++++++++++++++++
Cahoot-02c.4

1.13 Final Git Tips

1.14 Time to Git-er-done: Continuous testing and integration

03-VersionControl/continuousintegrationcycle.png
03-VersionControl/continuous-integration.png

Note:
Your https://git-classes.mst.edu unit tests are built into the git CI framework!
Check out how we do it:
* https://about.gitlab.com/ci-cd/
* https://docs.gitlab.com/ee/ci/
* https://about.gitlab.com/product/continuous-integration/

Remember, in learning to code, and trying new projects:
Fork it until you make it!