Version Control with Git and GitHub
Lets look at the course website
Why use version control? Enables collaboration Merge changes to the same file View how code changes over time Visualize history of a code file Revert to specific version Recent change breaking things? Undo! See who is contributing Find all changes by username Backup Master copy is kept elsewhere (hopefully) Great for solo projects too!
Basics of Version Control https://snipcademy.com/code/img/tutorials/git/introduction/cvs.svg
Basics of Version Control Each user has a working copy Changes must get committed to the server Might require a merge
Types of Version Control Older VCS were “centralized” CVS SVN Distributed version control! Git Mercurial with GitHub for hosting and services
Distributed Version Control
GitHub-User Perspective You GitHub Working Dir Local Repos Remote Repos
First: Lets set up Git git config --global user.name “Austin Henley” git config --global user.email azh@utk.edu git clone https://github.com/utk-cs/test.git git config credential.helper store git pull Warning: stores pw in plaintext locally Alternative: setup Git to use SSH
Optional: If you don’t like VI If VS Code is installed and in your PATH Test by doing: code –help git config --global core.editor “code --wait” git config --global -e
Try it!
Working with Git working directory staging area gets repo from GitHub local repository clone remote repository
Working with Git working directory add stages files you changed staging area local repository remote repository
Working with Git working directory staging area commit saves changes local repository remote repository
Working with Git working directory sends all your commits to GitHub staging area local repository push remote repository
Working with Git working directory staging area gets latest version from GitHub local repository pull remote repository
Handy commands Get the repo for the first time git clone https://foobar See the status of your local repo git status Stage a file you modified git add filename Stage all modified files git add -A Commit staged changes git commit -m ”Foobar” Push your commits to GitHub git push
Try it!
Get previous versions See commits to a file git log filename Get previous version of repo git checkout commitid . Get previous version of file git checkout commitid -- filename Go back specific number of commits (e.g., 5) Git checkout master~5 -- filename Return to latest version git checkout master
I made a mistake! How do I… Unstage a file? git reset HEAD filename Uncommit? git reset --soft HEAD^ Undo your changes and go back to a fresh repo? git reset --hard HEAD^ Careful!!!
How to organize a commit? Commits should be logical groupings of code changes E.g., all the changes to fix a bug Try to make commits small and frequent Not fun to look through 2000 lines of changes across 11 files
A Few Resources A tutorial A visual explanation of Git https://git-scm.com/docs/gittutorial A visual explanation of Git http://marklodato.github.io/visual-git-guide/index-en.html Git cheat sheet for commands https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
Lets walk through GitHub