Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Intermediate Topics in Git and GitHub - October 11 #195
In this session, we will build on the material presented at last week's Intro to Git and will discuss more advanced ways to use Git in collaboration with others. The material will be aimed at an audience that has some familiarity with basic Git commands, but by no means do you need to be experienced or an expert. If you attended last week's session or have used Git before or have looked over the material from last week, you should be well-prepared.
Installation instructions: You will need to install the appropriate programs. See the git section of the installation instructions page. .
Watch: This event will be streamed live. If you have questions during the live stream (or just want to chat with us), please ask in our Gitter lobby and we will forward your questions to the instructor! (Although we aim to live stream each event, there are sometimes technical difficulties so it's best to attend in person if you can.)
Directions: MADLab is located in Gerstein Science Information Centre, Room B112 at the south end of the first lower level. Once you go through the main entrance of Gerstein, take a right turn down a corridor (across from the admin desk or just past the reading room), then take the stairs down and follow the signs to MADLab, the door should be open 10-15 minutes before the lesson.
Here's lightly annotated list of commands that were run during the session:
# Recap of last week: ## Check version of git / install status git --version ## Check for config: user.name, user.email, core.editor git config --global --list ## Make a repository cd Desktop/ mkdir git-workshop cd git-workshop/ git init ## Add some files touch afile.txt bfile.txt cfile.txt git add afile.txt bfile.txt cfile.txt ## Check status git status ## Commit files git commit -m "Initialize repository" ## Check status git status ## Make an edit and commit it nano afile.txt git status git add afile.txt git commit -m "Update information" ## Check history git log git log --graph git log d705dbae45ab0..d04025c6777 # between two commits git log d705dbae45ab0..master # between a commit and a branch name git log --oneline --graph # Adding branches git branch new-branch # Add a new branch at current location in history. git log --graph ## Make another commit on master branch nano bfile.txt git branch nano bfile.txt git status git add bfile.txt git commit -m "Write about movies." git log --graph # new-branch remains where it was cat bfile.txt ## Switch to new-branch git checkout new-branch git status cat bfile.txt # Now this has the old contents git log --graph # Only shows history since new-branch (no extra master commit) git log --graph --all # Shows full history (including master branch) ## Make a new commit on new-branch nano cfile.txt git add cfile.txt git commit -m "Write about Pluto." ## Now we see the split in history: git log --graph git log --graph --all --oneline git status ## Go back to master branch git checkout master nano bfile.txt # File goes back to how it was on master git status ## Shortcut to make a new branch and switch to it: git checkout -b movie-branch git status ## Make a new commit on movie-branch git add bfile.txt git commit -m "It was actually a better movie." git log --graph --all --oneline # Merging branches ## Switch back to master (the target of the merge) git checkout master ## Merge new-branch *into* master git merge new-branch -m "Merge new-branch into master" git log --graph --all --oneline ## All files contain contents of both branches: cat afile.txt cat bfile.txt cat cfile.txt ## Now we can delete the merged new-branch git branch -d new-branch git branch git log --graph --all --oneline git branch -d master # Not possible because we are using this branch git branch -d movie-branch # Not possible because it's not merged git branch -D movie-branch # But we can force it to be deleted git log --graph --all --oneline # Handling conflicts git checkout -b conflict-branch d04025c # Make a new branch sometime in the past git log --graph --all --oneline ## Edit a file and change a line that will conflict with the other branch nano bfile.txt git add bfile.txt git commit -m "Make a conflict." git log --graph --all --oneline ## Try to merge conflict-branch into master: git checkout master git merge conflict-branch # Will error due to conflicts git status # Should show bfile.txt is conflicting nano bfile.txt # Here we fix the conflicts (between < = >) git add bfile.txt git status # Now no conflicts are listed git commit -m "Merge conflict-branch into master" # Commit this merge # Working with GitHub ## First, fork the https://github.com/UofTCoders/sandbox-2018 repo on GitHub. ## Then copy the clone url (denoted <CLONE-URL> below) cd ~/Desktop/ git clone <CLONE-URL> # Download a copy of the repository from our fork. cd sandbox-2018/ ls # Should show all files that you can see online git log --graph --all --oneline # A complicated history. ## Start a new branch git checkout -b my-bio git log --graph --all --oneline ## Add some information about ourselves: nano bio.txt git add bio.txt git commit -m "Add my name." git log --graph --all --oneline ## Push new branch my-bio to GitHub git push -u origin my-bio # -u enables tracking (shortcut for git push) git remote -v # Shows remote (nicknames for remote repositories) ## Here, we go to the GitHub website for the UofTCoders sanbox repository and ## open a Pull Request (PR). ## Let's open another PR and try to fix a conflict... ## Start a new branch git checkout -b another-bio nano bio.txt # Make some changes git add bio.txt git commit -m 'Add UofT Coders' git push -u origin another-bio # push to our fork ## At some point, upstream changed and our PR now has conflicts. ## We will try to fix them. git remote -v # Shows list of remote git remote add upstream firstname.lastname@example.org:UofTCoders/sandbox-2018.git # Add a remote pointing to UofTCoders (upstream is a conventional name) git remote -v # Shows both origin and upstream ## Let's see what's new from UofTCoders: git fetch upstream git log --graph --all --oneline ## Merge upstream's new changes and fix conflicts: git checkout another-bio git merge upstream/master # Merge with upstream git status # There are conflicts that need fixing! ## Resolve the conflicts nano bio.txt git add bio.txt git status git commit -m "Resolve conflicts" # Commit resolution to conflicts. ## Update our PR with the fixed conflicts. git push