Before Class
PreReqs * Git: everyone type ‘git’ in command line. Make sure you get a response that isn’t a variant of ‘unrecognized command' * Github: register for Github account if haven’t already. do it at the break, or if you need a mental break
Switch to slides for Intro to Version Control
Use Slides https://docs.google.com/presentation/d/1_8zDM_eZMF-Nkjvz35_gBAifJaoB_of54Q9CsbulPSc/edit?usp=sharing
Objectives: get system set up for proper attribution of your work
cd ~
pwd
git config --global user.name “<username>"
git config --global user.email “<email address>"
git config --global core.editor "nano -w"
git config --global core.editor “vim"
git config --global core.autocrlf input
git config --global core.autocrlf true
--global
means will apply for every command entered afterward. The majority of the time, you’ll use this. You’ll want the same text editor, and generally the same other config settings. Different user accounts example (work vs home)git config –-list
git help
and git help <command>
is your friend.git status
every time something changes) it’s a very good habit to get into.Objectives: start tracking versions in a particular folder/directory (repository)
cd ~
mkdir learning-git
cd learning-git
git init
ls
ls -a
git status
git status
to check in and see what state your content is islearning-git
directory with that is now tracked by git?Objectives: practice workflow (modify-add-commit), explain where information is stored
nano basics.md
(.md is markdown file)core.editor
setting. but in our case, they are probably the samegit init
- creates a new git repository in the current folder ```CTRL-X
will exit and prompt to save. Hit Y
to save.ls
cat basics.md
git status
git add basics.md
git status
draw attention to "Changes to be committed": it's tracking, but the changes aren't recordedgit commit -m “document git repository initialize command"
git status
git log
nano basics.md
* `git status` - I can use this anytime to see the current status of the repository"
cat basics.md
git status
git diff
. talk through the output of the diffgit commit
(intentionally forgetting to add
first)git add basics.md
git commit -m "document git status command
git log
nano basics.md
* `git add <file>` - places the file in the git staging area"
git diff
(talk about what this shows)git add
, git status
, git diff
(git diff
, by default, doesn’t see a difference between what’s in the current directory and what is committed)git diff —staged
git commit -m “document git add command"
git status
git log
(look at history of commits)Objectives: last thing before we dive into wonderful world of Github. identify and use Git commit numbers, compare versions, restore old versions * git diff HEAD~1 basics.md
(remember head from commands yesterday? same thing here, head is the top of the repo) - note: we’re diffing against HEAD. when not specified, that’s what it runs against * git diff HEAD~2 basics.md
* what if we want to see the differences between the previous two commits? * git diff HEAD~1..HEAD~2
* we can also use the unique IDs for the commits to do the same thing. but honestly, unless you’re going back beyond a few versions, better of with HEAD
* If using a unique ID, you can just use the first 7 characters of the ID * let’s see how we can restore an old version, you know in case we accidentally run a script that wipes out our source file 😃 * > basics.md
* cat basics.md
* git status
* we now have an “oh sht” moment. how can we get out file back the way it was? git diff
(yep, there’s all the stuff we deleted) * git checkout HEAD basics.md
* cat basics.md
* YAY! We have our previously committed file back * we can go back farther as well for a file. HEAD~1, or we can use the commit ID. Had we committed that change accidentally, we could have used HEAD~1 to get us back to the previous version. * note: easy accident is to forget to specify a file when checking out a commit * git will tell you that you are in a detached HEAD
state. awesome huh. * DO NOT make any changes in this state. If it happens, do git checkout master
to get back. * Questions? - SLIDE: Recovering older versions of a file - SLIDE: Exploring History
Objectives: explain why remote repos, clone remote repos, push/pull
git-basics
repo_git remote add origin https://github.com/<username>/git-basics.git
origin
is an alias. it could technically be anything. but it is a convention widely used for referring to remote repos. Stick to it!git remote -v
(v stands for verbose output. but we can always look that up in git help remote)git push origin master
You will need to authenticate now with your GH username + password
git pull origin master
- Run it.
Objectives: collaborate pushing to common repo * ok, we’re going to work on a shared repo now. * pair up with your tablemate to collaborate with - Each person lead the other to their git-basics repo on Github - demo this with class TA - Fork the owner's repository into your own account (explain) - cd ~
- Make a safe directiory to work in mkdir collaborating-test
- cd collaborating-test
- clone your forked repo into a new directory - git clone <repo>
- cd git-basics
- do a git status
. - see how it says “On branch master”. By default all the changes we’ve been making are on the master branch. You can see this in Github too (show it) - git status
- now we’ll add entries for push, pull, and fork - nano git-basics.md
- add a bullet: * `git push <remote> <branch>` send changes in local repository to remote repository branch
- add a bullet: * `git pull <remote> <branch>` get changes from remote repository branch into local repository
- add a bullet: * Forking a repository - Create a complete copy of an existing Github repository to enable collaboration.
- git add git-basics.md
- git commit -m "documenting forking a repository and push/pull commands."
- git push origin master
- Now look on Github - See how git asks us about a PR? Let’s do that and add proper information about our changes, and ask if we can merge our changes in master * CHECK IN Do all collaborators have PRs? * Owner - review the changes. You can make comments if you’d like to see anything different, @mention, you can add emojis and request changes. * Merge the PR and delete the remote branch. (Why can we delete the remote branch?) * Owner - do a git pull origin master
* CHECK IN Do you all owners have the collaboraits changes? * SWITCH (IF THERE’S TIME) OR Do via a fork + PR * Questions? * SLIDE: Github at SIO * SLIDE: Using Git: Alternatives to the command line