###### tags: `Git` `Tutorials` # Git Just what is this thing called git? How do we use it? Is it hard to use? ## What is Git? Git (not to be confused with github) is a version control system. Ever worked on a document and forgot to hit save and suddenly the computer looses power? Ok so git won't fix that, however, while you are working on that document you magically highlight text and delete it, bet that's happened before right? The ever so magical undo button to the rescue. Until you hit save and close the document and realize that you deleted something you really did need later on. Once you close that document that undo button gets emptied so to speak. This is where git comes in. Everytime you create a git save point you are basically taking a snapshot of your work at that moment in time so in case later on you delete something that was there you can get it back. :::danger Save your code often, but commit your code EVERY major change. Going to delete some code? Commit it first and again after. You'll thank me later ::: This isn't all that git is good for though. Version control isn't simply making lots of hard save points so you can recover missing code, but it's a way to have a team work on the same code and because they were smart and each had a branch they were working on they can then merge their collective work together. Oh and John Doe over there who saw a function that didn't look important so he removed it? Yea you can get that back. ## How do we use Git? Well now this conversation can go 2 ways. It is either easy or confusing. All depends on well the developer in question I guess. He is a quick down and dirty look at the typical git flow process ### Git Flow (Just the basics): #### The Best and Easy way 1. Create a repository on github 2. Grab the clone link 3. Open up your IDE (VS Code) terminal or just your regular terminal 4. Use the following commands to pull the repository conents down to your computer ``` cd <folder you want the repo to live> git clone <enter clone link here> ``` 5. Ok now we have the code on our local device. Lets make some edits. Add, remove, adjust, what ever you need to do to the repo. Don't forget to hit save 6. Once done you will use the following commands ``` git add . // this adds all the changed or added files to the staging area git commit -m "enter relevent comment here" // This creates that all so magical save point git push // As long as you are working in the main branch this will take all the changes you just commited and push them up to in this case github ``` Yup it is basically that simple. Now once you start adding branches and team members then things will get a little more complicated. However at the root this is the basic flow. #### Alternate way but can cause issues plus more steps Now say you already have code on your computer you don't really want to just start a new repo and re-create everything after the above flow right? Well you can follow the same steps and simply move the files into the cloned folder (but for some applications this is NOT ideal) or you can follow steps 1 above then follow the following steps 2. On your local device in the terminal enter the following 2. commands (just make sure you are in the current project folder) ``` git init // this guy will create all the files needed (hidden files by the way) that are needed for git git add . git commit -m "initial commit" ``` 3. Now on github you should see a screen (as long as you started an empty repo) that has 3 sets of commands. The middle one should be add code from a locally created repo (should be 3 lines of code. ) 4. Copy those 3 lines and enter them into your terminal. The 1st 2 lines will auto run so after you paste those lines just hit enter. You should be good to go. I know your thinking it can't be that easy can it? It usually isn't but at the root of it yes it is. ## Is it hard to use? Generally speaking no it's not hard to use. Does it take time to get used to the commands and the flow. 100%. Are you going to run into issues? I'd say there is a 95% chance that at some point yes you will run into an issue. Most of the time the issues that arrise are dealing with merge conflicts. Much like errors in your code this isn't something that can be taught so much as you need to run into it, take notes and breathe. There is always someone out there that can figure it out.