Try   HackMD

2022 UC Carpentries Fall Workshop (Version Control w/ Git)

Workshop Details
Dates: September 6th - 13th, 2022
Time: 9am - 12pm

Workshop Agenda:
https://ucsdlib.github.io/2022-09-06-carpentries-uc/

Day 5: Version Control w/ Git

Software Installation:
Git should be installed on your device from the Day 4 session.

Session lesson-by-Lesson overview and cheatsheet links: https://swcarpentry.github.io/git-novice/reference.html

Workshop Day 5

First name and Last Name/Organization/Dept./Email

Name Dept. Email
IT jdoe1@ucsd.edu
Geno Sanchez UCLA Library genosanchez@library.ucla.edu
Economic indeankrehel@yahoo.com
Epidemiology and Biostatistics apisit.kaewsanit@ucsf.edu
Scripps nrosenberg@ucsd.edu
Pediatrics aleszczynska@ucsd.edu
Yulissa Perez Rojas Environ. yperezrojas@ucmerced.edu
Alexander Frey UCSD, Rady School of Management alexander.frey@rady.ucsd.edu
ETS jaychi@ucsb.edu
Cell and Developmental Biology aheidbrink@uscd.edu
CMM bah@ucsd.edu
Oishee Misra Economics omisra@ucsd.edu
Josue Duque QSB jduque2@ucmerced.edu
Melodi Frey UCSD CMM mtastemel@health.ucsd.edu
John Thompson UC Merced jthompson44@ucmerced.edu
Psychology wollermi000@gmail.com
Mario Cuaya Computer Science mcuay001@student.rccd.edu
Public Policy lzimmerman@g.ucla.edu
Dexin Zhou Mathematics dzhou@ucsd.edu
Douglas Zhang Chemistry and Biochemistry UCSD doz023@ucsd.edu
Mathematics jle173@ucr.edu
Jacobus Kats UCR ITS bart.kats@ucr.edu
Govind Sah UCSD Pathology gsah@health.ucsd.edu
Charles Faulhaber Spanish & Portuguese UCB cbfberkeley.edu
Stella Yuan Ecology and Evolutionary Biology UCLA scy8@ucla.edu
Julia Nat Sci
Josiah Piceno MBSE jpiceno3@ucmerced.edu
Shang Su Cell and Cancer Biology U Toledo shang.su@utoledo.edu
Leila Fattel Agronomy lfattel@iastate.edu
Jun Tan Economics UCSD j4tan@ucsd.edu
Kazuma Nagatsuka Robotics(Mechani) knagatsuka@ucsd.edu
Ha Vu UCSD Economics vha@ucsd.edu
Zhaoning (Johnny) Wang UCSD CMM zhw063@health.ucsd.edu
Elizabeth (Lisa) McAulay UCLA Library emcaulay@library.ucla.edu
Donald Zarate UCR Political Science and Psychology dzara016@ucr.edyy
Sam Erickson UC Merced Physics serickson3@ucmerced.edu
Daryl Han UC Irvine Student Center and Event Services ddhan@uci.edu
Andrew Chan UCSD IGPP andrewchan@ucsd.edu
Jessica Wu-Woods UCR Microbiology jwuw001@ucr.edu

NOTES:

A copy of the instructor live session notes will be made available to participants upon request at the end of the workshop.

setup:
Windows users download Git for Windows: https://gitforwindows.org/
Mac/Linux users already have Git installed. Just open the Terminal app

#MacOS
cd Desktop

#Windows
cd /c/Users/username/Desktop/ 

References

Carpentries Git reference guide: Key Points: https://swcarpentry.github.io/git-novice/reference.html

https://education.github.com/git-cheat-sheet-education.pdf

What is Version Control

Tracking files over time. Allowing you to save previous versions and be able to look at them.

Automated Version Control

Version control systems start with a base version of the document and then record changes you make each step of the way.

Git is traditionally done in the Command Line Interface (CLI).

There are a variety of software applications for working in Git. These may be helpful in free version for students of a program called Tower:

Version control is like an unlimited ‘undo’ and allows users to work in parallel.

Setup for Git

git config

git config --global user.name "Vlad Dracula"

git config --global user.email "vlad@tran.sylvan.ia"

Please use your own name and email address instead of Dracula’s. This user name and email will be associated with your subsequent Git activity, which means that any changes pushed to GitHub, BitBucket, GitLab or another Git host server after this lesson will include this information.

We will be using Github for this lesson. You can sign up for a Github account here: https://github.com/

Line Endings

MacOS

$ git config --global core.autocrlf input

Windows

$ git config --global core.autocrlf true

As with other keys, when you hit Enter or ↵ or on Macs, Return on your keyboard, your computer encodes this input as a character.

Read more here: https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_core_autocrlf

Find what version of git your computer is using

git -v
#or
git --version

git version 2.37.3 

#version might be different for you

Default editors

# nano
$ git config --global core.editor "nano -w"

Commands for other editors: https://git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config

ls -lh

#your gitconfig file should be in your home directory

nano .gitconfig
#where your git settings are saved

Setup your default branch

In 2020, most Git code hosting services transitioned to using the name 'main' to designate the default branch of the repository. However, the Git setup still uses 'master' as the default branch name, so you need to reset it. As an example, any new repository that is opened in GitHub and GitLab default to main. As a result, local repositories must be manually configured have the same main branch name as most cloud services.

git config --global init.defaultBranch main

Will display your settings in your Terminal window:

git config --list

Git Help and Manual

git config -h
git config --help

Press Q to exit Help screen

Creating a Repository

First, let’s create a new directory in the Desktop folder for our work and then change the current working directory to the newly created one:

cd ~/Desktop
mkdir planets
cd planets

Then we tell Git to make planets a repository – a place where Git can store versions of our files:

git init

It will create a git repository in the planets directory

Git uses this special subdirectory to store all the information about the project, including the tracked files and sub-directories located within the project’s directory. If we ever delete the .git subdirectory, we will lose the project’s history.

ls -a

.     ..     .git

Next, we will change the default branch to be called main. This might be the default branch depending on your settings and version of git.

git checkout -b main


Switched to a new branch 'main'

We can check that everything is set up correctly by asking Git to tell us the status of our project:

git status



On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Tracking Changes

First let’s make sure we’re still in the right directory. You should be in the planets directory.

cd ~/Desktop/planets

Create a text file:

nano mars.txt

type:
Cold and dry, but everything is my favorite color.
Press CTRL O to save
Hit Enter to confirm save
Press CTRL X to exit nano

ls

mars.txt



cat mars.txt

Cold and dry, but everything is my favorite color.
git status



On branch main

No commits yet

Untracked files:
   (use "git add <file>..." to include in what will be committed)

	mars.txt

nothing added to commit but untracked files present (use "git add" to track)

We can tell Git to track a file using git add:

git add mars.txt



git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   mars.txt

Record changes as a commit:

git commit -m "Start notes on Mars as a base"


#output:
[main (root-commit) f22b25e] Start notes on Mars as a base
 1 file changed, 1 insertion(+)
 create mode 100644 mars.txt

If we want to know what we’ve done recently, we can ask Git to show us the project’s history using git log:

git log


#output:
commit f22b25e3233b4645dabd0d81e651fe074bd8e73b
Author: Vlad Dracula <vlad@tran.sylvan.ia>
Date:   Thu Aug 22 09:51:46 2013 -0400

    Start notes on Mars as a base

git log lists all commits made to a repository in reverse chronological order. The listing for each commit includes the commit’s full identifier (which starts with the same characters as the short identifier printed by the git commit command earlier), the commit’s author, when it was created, and the log message Git was given when the commit was created.

Adding to your text file:

nano mars.txt

#output:
Cold and dry, but everything is my favorite color.
The two moons may be a problem for Wolfman.

Press CTRL O to save
Hit Enter to confirm save
Press CTRL X to exit nano

Check status of your mars.txt file:

git status

#output:
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   mars.txt

no changes added to commit (use "git add" and/or "git commit -a")

Show us the differences between the current state of the file and the most recently saved version:

git diff


#output:
diff --git a/mars.txt b/mars.txt
index df0654a..315bf3a 100644
--- a/mars.txt
+++ b/mars.txt
@@ -1 +1,2 @@
 Cold and dry, but everything is my favorite color
+The two moons may be a problem for Wolfman

Adding more lines to your text file:

nano mars.txt



#output:
Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman
But the Mummy will appreciate the lack of humidity

Press CTRL O to save
Hit Enter to confirm save
Press CTRL X to exit nano

Reverting to the previous change

git restore mars.txt

#removes the latest change


nano mars.txt


#output:
Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman

commiting our changes

git commit -m "Discuss concerns about Mars' climate for Mummy"


#output:
[main 005937f] Discuss concerns about Mars' climate for Mummy
 1 file changed, 1 insertion(+)
git status

#output
On branch main
nothing to commit, working directory clean

and look at the history of what we’ve done so far:

git log


#output:

commit 005937fbe2a98fb83f0ade869025dc2636b4dad5 (HEAD -> main)
Author: Vlad Dracula <vlad@tran.sylvan.ia>
Date:   Thu Aug 22 10:14:07 2013 -0400

    Discuss concerns about Mars' climate for Mummy

commit 34961b159c27df3b475cfe4415d94a6d1fcd064d
Author: Vlad Dracula <vlad@tran.sylvan.ia>
Date:   Thu Aug 22 10:07:21 2013 -0400

    Add concerns about effects of Mars' moons on Wolfman

commit f22b25e3233b4645dabd0d81e651fe074bd8e73b
Author: Vlad Dracula <vlad@tran.sylvan.ia>
Date:   Thu Aug 22 09:51:46 2013 -0400

    Start notes on Mars as a base

Directories

  1. Git does not track directories on their own, only files within them
mkdir spaceships
git status
git add spaceships
git status

#changes are not shown until you edit a file
  1. If you create a directory in your Git repository and populate it with files, you can add all files in the directory at once by:
git add <directory-with-files>


touch spaceships/apollo-11 spaceships/sputnik-1

git add spaceships

git status

#output will show both new files ready for commit

git commit -m "Add some initial thoughts on spaceships"

Remotes in GitHub

Github setup

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/checking-for-existing-ssh-keys

How to check your setup, type into terminal:
ssh -T git@github.com

You should get a message back from github that says "Hi Vlad Dracula" (with your actual name!)

Create a remote repo

log into GitHub: https://github.com/
then click on the icon in the top right corner to create a new repository called planets:

As soon as the repository is created, GitHub displays a page with a URL and some information on how to configure your local repository:

e.g.: https://github.com/username/planets.git (your link will be slightly different)

Copy that URL from the browser, go into the local planets repository, and run this command:

#make sure your working directory is planets
git remote add origin git@github.com:USERNAME/planets.git

#pushes your local changes to the GitHub repo (remote)
git push -u origin main


#output:
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (16/16), 1.45 KiB | 372.00 KiB/s, done.
Total 16 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/vlad/planets.git
 * [new branch]      main -> main

Pulling changes from remote

#when edits are made somewhere else like GitHub's code editor you can pull those changes to your local machine

git pull origin main

Connection issues:

  1. Go to settings and select SSH and GPG keys on left side panel
  2. create a new SSH key

Day 5 Questions:

Please enter any questions not answered during live session here:
1.

End Day 5