# Github Guide GitHub is a tool for tracking and storing changes for our code. It gives us a way to ensure that we always have a working copy of our project and allows us to push our updates online so that they won't be lost if the code on our computer gets mistakenly deleted. The general development flow is to: 1) make local code changes on your computer 2) commit your local changes (this is esssentially "saving" your progress locally) 3) push your new changes to GitHub (this saves your progress on the cloud) 4) refresh the repository by pulling in the latest code from GitHub (this ensures that the code you're working on is the most update to date code so conflicts don't occur when you try to push new changes on outdated code) We will be using GitHub throughout the course as we build out our apps. Below are some guidelines for: - setting up Android Studio with your GitHub account - pushing a new repository onto GitHub - pushing new changes when your code is updated - pulling changes You may chose to use the Github integration that's available in Android Studio, or utilize the command line. ## Working With Github in Android Studio ### Setting up Git #### Set up Git for Android Studios (For WINDOWS MACHINES ONLY) : If you have a Windows machine, this is the extra pre-step for setting up Git. If you're on a Mac, skip this step and refer to the next step! - In the next step, while going through the installation process for Git, there are a couple specific options that you MUST select - Step 5 : Adjusting your PATH environment: Select the "Use Git and optional Unix tools from the Windows Command Prompt" option as shown below: <br/><img src="https://i.stack.imgur.com/B1jgT.jpg" width="600" /> - For the rest of the installation steps, the default options are fine - Install Git for Windows from here: http://git-scm.com/download/win and be sure to select the specific options as outlined in the first bullet point above - Inside Android Studio, click `Android Studio` -> `Preferences` -> `Version Control` -> `Git` and make sure to update the `Path to Git executable` to `c:\Program Files\Git\bin\git.exe`. ![Imgur](https://i.imgur.com/Qbcqrct.png) - Close and restart Andriod Studio #### Set up Git for Android Studios (For MAC MACHINES ONLY) : If you are working on a Mac machine: - open a terminal window - use `command` + `space bar` to open the search prompt and type in "terminal" and enter - in the terminal window, enter `git --version` - if you have git installed, it will show you the version you have installed. - if you don't have git installed, it will prompt you to install it. Follow the instructions to install it. Once you have git installed, close and restart Android Studio ---------- ### Setting up Android Studio with your GitHub account - First we must integrate our GitHub account with Android Studio <img src="https://i.imgur.com/9bVyOtj.png" width="800" /> - Android Studio should now ask for your Github login (make sure your selected Auth type is Password, not Token) <img src="https://i.imgur.com/IAPsPxz.png" width="700" /> ### Pushing a new repository - Android Studio should now ask for the 'New Repository Name'. Give it an appropriate name such as "Flashcard" (or something more creative) and click 'Share' - You should now see a dialog in Android Studio saying it successfully pushed the repo. Click on the link and make sure all your code has been pushed. <br/><img src="https://i.imgur.com/aaDoNxe.png" width="700" /> That's it! Your code is now living on GitHub. ### Pushing new changes - Whenever you write new code and want to save your progress, you must 'Commit' your changes, write a commit message, and then select the option to 'Commit and Push'. Commit messages should be descriptive of what changes you are pushing. For example, a sample commit message could be "Add TextView that shows a Question" or "Add listener so that the answer shows when the question card is clicked" <img src="https://i.imgur.com/l0IieJq.png" width="800" /> <br></br> <br></br> <img src="https://i.imgur.com/KZEgtbK.png" width="800" /> ### Pulling Changes - Sometimes the code on your machine may not be the same as the code in the GitHub repository due to changes made and pushed from places other than your machine (ie : if you added a ReadMe through the browser or if your collaborating with another person on the same project and they pushed new changes) . Therfore it is important to always pull the latest code before you start to write new code. <img src="https://i.imgur.com/Bjl5wW3.png" width="800" /> ## Working With Github through Command Line #### Set up (For MAC MACHINES ONLY) : 1. Install Git by opening up a terminal window and entering the command `brew install git` 2. Define your Git user with the following commands: `git config --global user.name "YourUsername` `git config --global user.email "your_email@youremail.com"` #### Set up (For WINDOWS MACHINES ONLY): Go to http://git-scm.com/download/win and the download for Git will start automatically. #### Useful commands once Git is setup Once you have Git setup on your workstation, here are the most common commands that you can use to push and update your project code. - saving a change : `git -am 'a useful commit mesage'` (this 'stages' your changes with a commit message so that youre changes are ready to be pushed) - pushing your changes : `git push origin master` (this officially pushes your code changes so that you can see the changes if you browse to the git repository) - pulling any new changes : `git pull` (this updates your codebase and pulls in any new changes since the last time you updated your project) You can push new repositories by following the instructions here https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/. Alternatively, you can also create a new repository through the browser and then following the step-by-step instructions after the repo is successfully created. ![Imgur](https://i.imgur.com/HAk3n17.png)