# Intro to Git and GitHub Note: - By show of hands. How many people have used Git and or GitHub before? - This workshop is designed for people who are new to Git and GitHub and is created in the context of Code for Greensboro's workflow
# About This Workshop ## Goal - Using Git to track changes in a project - Using Git to undo changes in a project - Using GitHub to collaborate with others
# What is Git? ### According to XKCD:
## According to git-scm.com: > Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. ## But what does that mean and what problem does Git solve? ### Have you ever worked on project that starts out like this? ```bash project └── my_project_2018-09-25.txt ``` Note: Have you ever worked on a paper or a project where maybe after you finished your first draft and got some feedback and before you start working on the next draft you name the file something like `my_project_2018-09-25.txt` Then you start on the next draft and you repeat the process and name it like `my_project_2018-10-18.txt` And so on and so forth and eventually you end up with a directory that looks like this: ### And after a few iterations your project looks like this? ```bash project ├── my_project_2018-09-25.txt ├── my_project_2018-10-18.txt ├── my_project_final.txt ├── my_project_final_final.txt └── my_project_final_final_THIS_ONE.txt ``` Note: Now imagine you and a team were working on this project and you were saving files like this and each person was making their own changes and sharing the files back and forth. ## Git provides a way to track incremental changes and share those changes with others And, when using Git correctly it gives you the confidence to break stuff Note: In our example what would happen if two people made changes to the same file at the same time? They would have to manually figure out what to keep and what to get rid of. Git can do this automatically and when it can't it provides a convient interface for doing it yourself.
# What is GitHub? and how is it different from Git? ## According to GitHub's website: > GitHub is a development platform inspired by the way you work. From open source to business, you can host and review code, manage projects, and build software alongside 31 million developers. # What does that even mean? Basically GitHub is a cloud based Git server. It provides a place for developers to `push` and store their Git projects and it makes it easier for multiple people to work on the same project. ## Confused? Let's get started! Note: This is all still a little confusing and highlevel so lets get started on a project and hopefully that will clarify things.
## Step 0 - Fork the Repo - Go to this url: https://github.com/codeforgso/github-workshop-site - Click the `Fork` button in the upper right corner ### "What did I just do?" #### You forked a repo! #### Forking: Copying another person's project to your GitHub account. Note: Forking is Git terminology for copying another person's project. It provides a place for you to make your own changes and if you want you can later give your changes back to the original repo. When contributing to projects in a team (and especially in open source) this is the first step
## Step 1 - Clone your newly forked repo Mac/Linux - Open terminal Windows - Open Git Bash Type the following ```sh git clone https://github.com/YOUR_GITHUB_USERNAME/github-workshop-site.git # You can copy the URL from GitHub page cd github-workshop-site ls -a # List all files in the directory ``` ### "What did I just do?" #### You cloned a repo! Cloning is when you download a copy of a Git project to your computer
## "Wait! this is confusing! How is cloning different from forking?!" - Cloning: Downloading a project - Forking: Creating your own copy of a project on GitHub Note: Cloning is different because you are downloading the project to your machine. With forking you are creating your own copy of the project in a virtual environment. If there is ever a project you just want to use and not contribute to, you'd often skip forking it and would just clone it.
## Open the index.html file in your browser To find where your project is type `pwd` in your terminal
## Step 2 - Branches ## So I downloaded the project. Now what? Note: Alright, so you have the project on your machine and it's time for you to make some changes to it Let's assume your team wants you to _change something on the website_. Using Git, how do we go about this? there are many ways to do this with Git, but we are making the assumption that you're are working on a team. And for that assumption, this is a common workflow you would see. ### Let's make a change! ```bash git branch my-changes # Creates a branch called my-changes git branch # Shows you a list of all your local branches. Press `q` to exit git checkout my-changes # Switches from your current branch to the my-changes branch ``` ### "What did I Just do?" You created a branch! Note: A branch is just code that _branches_ from the another development branch. A lot people use branching for something called "feature branching". This means you create a new branch for each logical group of changes. For example your working on a website and your boss asks you to add the navbar. You might then create a branch called `add-navbar`. This would allow you to create and push as many changes as you want without affecting the main development branch.
# Branches and Forks and Repos. Oh My! Note: We've covered several terms so far and they all sound kind of similar. So lets take a moment to recap and clarify things ### Terms > - Repo (repository) -> This is your project - Fork -> Creating a repo by copying another repo - Branches -> Groups of logical code changes (Or, groups of commits) ## An analogy A **repo** is like a tree **Forking** is making copy of another person's tree **Branches** are (kind of) like real life branches Note: Maybe an analogy would make things clearer: A repo is a tree. When you fork someone's repo, you make an identical copy of their tree and plant it in your front yard. The branches on the tree represent your Git branches. These branches stem from the trunk (typically called the `master` branch) and in Git - unlike a real tree - these branches will rejoin the `master` or another branch ## Is that any clearer?
## Let's make change - Let's make a change - Open the `index.html` file in your favorite editor. Note: Using VS Code type `code .` Using Windows Notepad.exe type `notepad.exe` Around line 61 change the project URL to your GitHub url: ```bash git add index.html git status ``` ## You should see something like this: ```bash On branch my-changes Changes to be committed: (use "git reset HEAD
..." to unstage) modified: index.html ``` When you run `git add` you stage your change. this means you are ready to commit the file to the repo. Run the following: ```bash git commit -m "Add my repo url" ``` ### What did that do? You just made your first commit! Commits are how you save your changes to a project. Type `git log` to show a list of commits in the current branch. Press `q` to exit.
## Let's revert this commit Note: So you made your first commit and that's great. But let's pretend your boss or project manager comes to you and says they no longer want your change. How do you go back to a previous commit? ```bash git revert HEAD --no-edit # `no-edit` tells git that you want the commit message to stay the same # If you are in nano type Ctrl+X then type y git log ``` Note: You should see something that says "Revert" then your commit message This will undo your most recent change. If you need to revert to something several commits back checkout the atlassian resource at the end of the presetation
## Let's make one more commit, and this time we are going to leave it ### Checkout master ```bash git checkout master ``` ### Run ```bash # Checkout a new branch git checkout -b add-github-username ``` Note: `checkout -b` is just a shorter way of creating a new branch and checking it out Go to line 129 or so and add your Github username and a link to your profile with the listed template Example: ```html
SchaeStewart
``` Run ```bash git add index.html git commit -m "Add GitHub username" ``` ### Time to upload your change ```bash git checkout master git merge add-github-username git push origin master ``` Note: What did this do? You moved back to your master branch and merged you changes with it. Then you pushed your changes to GitHub.
## Make A Pull Request - Go back to your GitHub repository page - Click the `Pull requests` tab - Click the green `New pull request` button - Write a title and body - Submit your Pull Requests ### Now what? Note: I now have to approve your pull request
## Additional Resources - `man git` - `git --help` - Attlassian - atlassian.com/git/tutorials - Git SCM - git-scm.com/docs/gittutorial