This repo contains sample files and an introduction on how to use Github for beginners.
- Setting up GitHub
- Creating a Repository
- Cloning/Adding to a Repository
- Branching and Merging
- Fork a Repository
- Basic/Other git commands
- Go to GitHub.com
- Enter your info to “Sign up for GitHub”
- Select a username and password (your username is your github id)
- ex: firstname-lastname
- your github username is public and you will be using it in the future for classes/interviews/etc. so be mindful of what you choose
- Use an email address that you use often - school or personal email (you can change this in the future/when you graduate)
Mac
- Type in
gitinto the command line- if it's not installed you will be prompted to install it
Windows
- Use this link to install git
- OR using Windows Subsystem Linux (WSL, which was probably set up in a previous class) do the following:
- run linux/ubuntu
- type
sudo apt install gitin the command line
- Log into github
- Click on the icon in the top right
- Click on "Your Repositories"
- Click on the green "New" button to create a new repository
- Create a repository name and give the repo a short description if you want
- Make it Public/Private depending on the project and personal preference (can change this later)
- You can add a ReadMe file (What this is) now, or create it later (What is a README, Basic writing and formatting syntax for README)
- Press the green "Create repository" button
- Navigate to the repository you want to add a collaborator to
- Click on "Settings" at the tob of the page
- Click on Collaborators (it may ask you to confirm your github password)
- Click the green "Add people" button
- Type in the github id of the person you want to collaborate with
- They can then view your repository and collaborate on it
- The invite will be sent to your collaborators, they have to accept within a week or it expires
- Go to the repository you want to clone
- Click on the green "<> Code" tab
- Copy the URL displayed
- Go to your terminal and navigate to a folder that you would like this project to sit in
- Type
git clone <URL LINK>into the command line
- Click on the "+" button and either create or upload a new file
- Once the files are uploaded/created you need to commit your changes with a commit message (usually describes what changes were made)
- When you want to update/edit a file click on the file, then click on the pen/pencil icon and make your edits, then commit your changes with a new commit message
- Find/create a file that you want to add to your repository and put/save it to the cloned repo folder on your computer
- In the terminal navigate to the folder of the repository where the new file is
- Follow these 3 steps in this order to add the file to the repo:
git add <filename>: adds files that are pushed to githubgit commit -m "<commit message>"git push: this pushes files to github
- To update an existing file in the repository on git repeat step 3 again (Ex: Each time you add more code to a file thats in the repo folder, using VS Code or another text editor, repeat step 3)
- If a collaborator adds to the repository and pushes their changes, in order to get the updated version on your computer use the command
git pull- if you're working on a repository with collaborators it's good habit to use
git pullbefore making changes and push whenever you make changes to avoid conflicts
- if you're working on a repository with collaborators it's good habit to use
If multiple people are working with the same project on the same repository they can't all make changes to the file at the same time on their individual accounts, this will cause a conflict. This is where branching and merging comes in handy.
Multiple people can create separate branches to work on their code and then they can merge their changes onto the main branch. Branches are made to be temporary. Changes made in a particular branch don't affect other branches until branches are merged.
Branching/merging is also useful when you want to create/try multiple different versions of the same code with some variations (useful for comparing outputs/finding what works best)
To branch and merge make sure you are navigated to the folder of your repository in your terminal
(Note: the main branch is always called "main", so don't name other branches "main")
Branching creates a copy of the code from the
mainbranch onto the new branch. Changes made directly in the branches don't affect the code of the main branch until you merge.
- create a branch:
git branch <branch name> - move to a branch
git checkout <branch name> - view available branches:
git branch- Git uses an asterisk to identify what branch you're in
- When you make changes on your branch push them using the steps above
Combines branches so that the 2 branches have the same content/code
- 1st use
git checkoutand switch to the branch you want to merge into,- Say you want to merge "branch1" into "main"
git checkout main(this means you want to copy the contents from branch1 and paste it into main) - Merge branch1 into main (you're on the main branch):
git merge branch1
- Say you want to merge "branch1" into "main"
- If you have multiple collaborators that each create their own branch everything needs to be merged to the main first then you're individual branch
- Say user1 creates a branch called "user1" and user2 creates a branch called "user2".
- If user1 makes a change and pushes it onto their branch (user1), they then need to merge "user1" into "main" and push the changes
- git checkout main - git merge user1 - git push - Then user2 needs to pull from "main" and merge those changes onto their branch (user2)
- git checkout main - git pull - git checkout user2 - git merge main - git push
- If user1 makes a change and pushes it onto their branch (user1), they then need to merge "user1" into "main" and push the changes
- Say user1 creates a branch called "user1" and user2 creates a branch called "user2".
A fork is a new repository that shares code and visibility settings with the original “upstream” repo.
Forks are used to propose changes to the main repository.
You can use forks to propose changes related to fixing a bug, you can:
- Fork the repository
- Make the fix
- Submit a pull request to the project owner
How to fork a repo (More on Forking)
- In the top-right corner of the repo's page, click "Fork"
- You can then change the name, owner, and description to your desire of this "forked" repo
- Click the green "Create a fork" button at the bottom
- After you fork a repo, in order for it to exist locally on your computer you can go through the steps of cloning it
Create a pull request to propose changes to a repo. These changes are proposed in a branch, which can then be chosen to merge with.
ON the Forked repo:
- On the forked repository click on "Pull requests" button at the top
- Click on the Green "Compare & pull request" button (then make sure the base and compare branches are correct)
- Write a title and description (optional) for your pull request
- Click on the green "Create pull request" button at the bottom
ON the Main repo:
- On the main repository click on the "Pull requests" button at the top
- It will show you the number of pull requests submitted, click on the one you want to merge with
- In the specific pull request you can see what was changed in "Files changed"
- Then to merge click on the green "Merge pull request" button
- Then click on the green "Confirm merge" button
- The main repo should now be updated with the contents/changed files of the forked repo
Here are some other basic/helpful git commands for terminal (includes commands from above too)
git init: creates local git repogit clone <URL>: clone repositorygit status: checks statusgit add <filename>: add file to staging (to be pushed)git add .: add all files in the current/sub folders
git commit -m "<commit message>": commit changesgit branch: list all branches ("*" denotes current branch)git branch <branch name>: creates a new branchgit branch -d <branch name>: deletes a branchgit checkout <branch name>: switch to a branchgit merge <branch name>: merge a branch into the active branch (the branch you're in)git merge <source branch> <target branch>: merge source branch into target branch
git push: push changes to repogit push origin <branch name>: push a branch to your remote repo
git pull: update local repogit pull origin <branch name>: pull changes from remote repo
GitHub Docs: To read and learn more about github