Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
GitGitVersion control with Git and GitHub
1
AdministriviaAdministriviaBathroomsWaterFire exitsMeeting point
2.2
AdministriviaAdministrivia
2.3
OverviewOverviewThese slides are online: Tutorial format (with some lecturing thrown in)Tutorial will be in phases (time permitting)1. Hands-on with GitHub and Atom2. Questions3. Discussion of GitHub workflows4. Hands-on with Git on the command line
http://cat-train.github.com/
2.4
OverviewOverviewGit is a powerful and potentially complex toolTwo goals for the day1. Cover core concepts and terminology2. Familiarise with a useful workflow
2.5
GitGitGit is a version control system:
software that manages different versions of files
3.2
GitGitWhy is it useful?
1. Keep track of changes to files2. Review and revert back to old versions3. Synchronise files between different locations4. Test changes without losing the original copy
3.3
GitGitWhy Git and not some other software?
1. Performance2. Flexibility3. Popularity
3.4
GitHubGitHubGitHub is a hosting site for Git repositories
(with lots of extra features)
4.2
GitHubGitHubWhy is GitHub useful?
1. Free hosting for open source projects2. Interface for browsing and editing code3. Workflows for collaborating with others4. API for doing other fancy things
4.3
GitHubGitHubWhy GitHub and not some other software?
1. Popularity
4.4
AtomAtomAtom is a text editor
with some useful Git and GitHub-related features
5.2
Let's get startedLet's get startedUsing Git's primary workflow
6.1
OverviewOverview1. Create a project (on GitHub)2. Clone the project (onto your computer)3. Change some files4. Commit the changes5. Push the changes (to GitHub)
6.2
1. Create a project1. Create a projectInitialise a new "repository" from within GitHub
7.1
Create a projectCreate a projectGo to
If you have an account, log in
If you don't, create one
You may need to verify your email address
Click New repositoryOr navigate to
https://github.com/
https://github.com/new
7.2
Create a projectCreate a projectEnter repository name cs4hsCheck Initialize this repository with a READMEClick Create repository
7.3
Remote cs4hs repositoryRemote cs4hs repositoryExists on GitHub's servers
It is a "remote" repository
Publically accessibleAnyone can see it
Only you can modify it
7.4
2. "Clone" the project2. "Clone" the projectCopy the project so you can edit it
8.1
"Clone""Clone"Given the location of a repositoryMakes a copy of the projectStores it on your computerLinks the two copies
8.2
Clone the cs4hs repositoryClone the cs4hs repositoryOpen Atom
Close any "welcome" tabs
Change the theme (if desired)
8.3
Clone the cs4hs repositoryClone the cs4hs repositoryPress Control-Shift-P to enter a command
Type GitHub: Clone and press Enter
Choose file locationsClone from: your GitHub repository URL
To directory: wherever (but remember where!)
8.4
Local cs4hs repositoryLocal cs4hs repositoryExists on your computer
It is a "local" repository
Files and folders behave as usualLinked to the version on GitHub
8.5
3. Change some files3. Change some filesNow you can make changes to your local copy
9.1
Edit the READMEEdit the READMEEdit file "README.md"
Save with Control-S (or from the File menu)
# cs4hs
## Test project
This is a project used to experiment with Git and GitHub.
9.2
Add a Python programAdd a Python programEnter Control-Shift-P and Add FileEnter the path for the new file: hello.py
Remember to save with Control-S
print("Dear world,")
print("")
print("Hello!")
print("")
print("Sincerely, Evan")
9.3
Sidenote: the ".git" folderSidenote: the ".git" folderThe ".git" folder contains Git's data filesThey can be edited but not saved in your repository
9.4
Local changesLocal changesYou now have:1. Some edits to a file2. An entirely new fileThese changes only exist locallyThey have not been "saved" to your repository yet!
9.5
3.1 Tell Git who you are3.1 Tell Git who you areIf you saw a red error message like this:
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'atomuser@WIN-VU7
10.1
Tell Git who you areTell Git who you areEdit file ".git/config"Add to the end of the file:
[user]
name = Your Name
email = [email protected]
10.2
4. "Commit" the changes4. "Commit" the changesSave your edits to a new version of the project
11.1
"Commit""Commit"Saving changes is a two-step process:1. Add them to the "staging area"
Tells Git to include the file in the next commit
2. "Commit" the changesTells Git to save a new version of the project
A "commit" is a versionIncludes a message and attribution information
11.2
Stage the edited fileStage the edited fileOpen the "Git" paneYour changes are currently "unstaged"Right-click and Stage "README.md"
11.3
Commit the edited fileCommit the edited fileEnter a Commit message: something descriptiveClick Commit to master
11.4
Stage and commit the new fileStage and commit the new fileUse the same process, this time for "hello.py"
11.5
Local commit historyLocal commit historyYou now have a series of changesThis is your project's "commit history"It tells you who changed what, how, and whenYou can "revert" to previous versions
11.6
5. "Push" the changes5. "Push" the changesNow you can publish these new commits on GitHub
12.1
Log in to GitHubLog in to GitHubEdit file ".git/config"Add your username to the cloned URL
[remote "origin"]
url = https://<username>@github.com/<username>/cs4hs.git
12.2
Push your commitsPush your commitsOpen the "Git" paneClick Push 2
Enter your password
Check Remember
Visit GitHub to view your published changes
12.3
ReviewReview1. Created a project (on GitHub)2. Cloned the project (onto your computer)3. Changed some files4. Committed the changes5. Pushed the changes (to GitHub)
13
Next StepsNext StepsReviewing and modifying commits
14
Reviewing changesReviewing changesYour project now has a "history" you can review
15.1
Your project's historyYour project's historyA series of commitsEvery commit has:1. A message2. An author3. A date4. A set of changes5. A "parent" commit6. A unique ID
This is also known as its "hash" or "SHA1"15.2
Fix-upsFix-upsWhat to do when you've made a mistake?
16.1
Undo a commitUndo a commitOpen the "Git" paneClick Undo on the most recent commit
The commit is deleted
Its changes are moved back into the staging area
Its message is preserved
16.2
Fix a commitFix a commitEdit "hello.py" and add two lines at the end
Save the fileThe new changes are now unstaged
print("Dear world,")
print("")
print("Hello!")
print("")
print("Sincerely, Evan")
print("")
print("PS. I'm learning Git")
16.3
Fix a commitFix a commitStage the new changesCommit the result
You have just replaced the old commit with a new one
16.4
Push the commitPush the commitHover over the button you used to push
What does it say now?
16.5
Push the commitPush the commitTo push these commits you must "force push"Right-Click and choose Force Push
This overwrites data on the remote repository!
16.6
BranchesBranchesIsolating and sharing specific sets of changes
17.1
BranchesBranchesEach "branch" indicates a separate versionMultiple branches in a single repositoryLet you work without interfering with othersEventually combined or "merged"The default branch is called "master"
17.2
OverviewOverview1. Create a new branch2. Commit to the branch3. Push the branch (to GitHub)4. Create a pull request (on GitHub)5. Accept the pull request (on GitHub)
17.3
1. Create a new branch1. Create a new branchStarts a new set of changes
18.1
Create a branchCreate a branchOpen the "Git" paneClick master
Click New Branch
Type feature
Click New Branch (again)
You have a new "feature" branch
18.2
2. Commit to the branch2. Commit to the branchNew commits are added to the active branch
19.1
Create a new commitCreate a new commitEdit the file "README.md"
Save and commit the change as usual
# cs4hs
## Test project
This is a project used to experiment with Git and GitHub.
This is some information about the project.
19.2
Isolated changesIsolated changesClick feature to open the branch picker
Click master to switch branches
README.md has changed backThe edit has been made to the feature branch
The master branch remains unchanged
19.3
3. Push the branch3. Push the branchSend the new branch to GitHub
20.1
Push the branchPush the branchOpen the "Git" paneSwitch back to featureClick PublishVisit your project on GitHub
The feature branch has been pushed
20.2
4. Create a pull request4. Create a pull requestRequest your branch to be "merged"
21.1
Request a mergeRequest a mergeClick Compare & pull requestEnter some informationClick Create pull request
21.2
5. Accept the pull request5. Accept the pull requestMerge the "feature" branch into "master"
22.1
Merge the branchMerge the branchMake sure you're happy with the changes!Click Merge pull request
Add any information you'd like to include
Click Confirm merge
Click Delete branchRemoves it from the remote repository
22.2
Review the new commitsReview the new commitsThe two branches have been combinedThere are two new commits:1. Your own2. A "merge commit"The combination happened on GitHub
GitHub is the remote repository
Your local repository is now out of date
22.3
Update your local repoUpdate your local repoBack in Atom, open the "Git" paneOpen the branch picker
Switch back to master
Click FetchClick Pull 2
22.4
Update your local repoUpdate your local repoThe two new commits are pulled from GitHubYou are back up to date with "origin"
22.5
ReviewReview1. Created a new branch2. Committed to the branch3. Pushed the branch (to GitHub)4. Created a pull request (on GitHub)5. Accepted the pull request (on GitHub)
23
Merge ConflictsMerge ConflictsWhen two branches can't be combined safely
24.1
OverviewOverview1. Create two incompatible commits
One locallyOne on GitHub
2. Pull the remote change (from GitHub)3. Resolve the resulting conflict
24.2
1. Create two commits1. Create two commitsChanging the same line but with different content
25.1
Edit a file locallyEdit a file locallyOpen "README.md" and change the last line
Make it read "important information"Commit the result
# cs4hs
## Test project
This is a project used to experiment with Git and GitHub.
This is some important information about the project.
25.2
Edit a file remotelyEdit a file remotelyVisit your repository on GitHubClick the pencil icon on README.md
Make it read "unimportant information"Enter a commit messageClick Commit changes
# cs4hs
## Test project
This is a project used to experiment with Git and GitHub.
This is some unimportant information about the project.
25.3
2. Pull the remote change2. Pull the remote changeTry (and fail) to combine the two branches
26.1
Fetch changesFetch changesRight-click the Push 1 buttonClick Fetch
This retrieves the new commits
Click Pull 1This attempts to merge the changes
You should hit Merge conflicts
26.2
3. Resolve the conflict3. Resolve the conflictIndicate how the two changes should be combined
27.1
Pick a sidePick a sideThe two different versions are colouredEdit the highlighted region to be "correct"
Remember to save the file when finished
# cs4hs
## Test project
This is a project used to experiment with Git and GitHub.
This is some potentially important information about the project.
27.2
Commit the resultCommit the resultStage the fileClick Commit to masterThe conflict is resolved locally
27.3
Share the resultShare the resultWe still need to push the resolutionClick Push 2Now everything is back in sync
27.4
ReviewReview1. Created two incompatible commits2. Pulled to combine the two
Encountered a merge conflict3. Resolved the conflict
28
Questions?Questions?
29
GitHub WorkflowsGitHub WorkflowsA few helpful tips for working with GitLab
30
ForkingForkingContributing to a project that isn't yours
31.1
ForksForksA "fork" is a copy of a repo on GitHub
Copies from one user account to another
Think of it like a "remote clone"
31.2
ForksForksThe result is a repo you control
You clone your forked copy
You push commits
Pull request comes from your fork
31.3
Let's try itLet's try itNavigate to my cs4hs repository
Click forkhttps://github.com/cat-train1/cs4hs
31.4
Clone your forkClone your forkIn Atom use Control-Shift-P
Choose GitHub: Clone
Enter the fork's URL including your usernameClick Clone
31.5
Make some changesMake some changesUse the standard workflow:1. Edit2. Stage3. Commit4. PushVisit your project on GitHub
31.6
Open a pull requestOpen a pull requestClick New pull requestClick Create pull request
This request will be in my repo
31.7
Git CultureGit CultureSome keywords to help navigate the landscape
32.1
Git KeywordsGit KeywordsSquash: Combining many commits into oneRebase: Alternative to mergingFeature: Branch dedicated to specific featureHotfix: Branch dedicated to a single bugCI: Testing new commits automaticallyOthers?
32.2
Command LineCommand LineUsing the Git program from the terminal
33.1
Open a TerminalOpen a TerminalThis depends on your Operating System
Windows: Git Bash
macOS: Terminal
Linux: Terminal
33.2
Using the TerminalUsing the TerminalQuick command line how-to
33.3
The Git CommandThe Git CommandThe git command (lower case) runs GitEnter git --version to see whether it works
33.4
Configuring GitConfiguring Gitgit config core.editor "atom --wait"
git config user.name "Evan Hanson"
git config user.email
33.5
Cloning a projectCloning a projectgit clone <url>
cd cs4hs
git status
33.6
Making commitsMaking commitsatom README.md
git status
git add README.md
git commit -m "message goes here"
33.7
Reviewing historyReviewing historygit log
git show
git show <commit>
33.8
Fixing commitsFixing commitsgit reset <commit>
git reset --hard <commit>
git add
git commit
33.9
BranchingBranchinggit branch
git branch <name>
git checkout <name>
33.10
Questions?Questions?
34
Thanks!Thanks!Evan Hanson
Slides will be emailed outDon't hesitate to contact me!
35
36