Kinda colorful, though we hate to admit it

Join a laid-back, close-knit community of mixed interests Get a free account!

  1. Programming with Neko-Chan: Git


    #1054522016-07-25 23:48:57 *Neko-Chan said:

    Git

    Before we start programming we'll need a place for our code to live. Also we will need to learn how we are going to program in relation with version control. There is a ton of information out on the net for git. I'll explain some really basic basics but its really easy to learn more. Check the references at the bottom of the thread.

    What is version control?

    Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

    If you are a graphic or web designer and want to keep every version of an image or layout (which you would most certainly want to), a Version Control System (VCS) is a very wise thing to use. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead.

    Taken from the git book

    Go install git and make a cloud account

    • Download git Go do it right now. I'll wait.
    • Make a bitbucket or github account with an email you will develop with (or both)
      • github Most common, only public code if you aren't paying
      • bitbucket Second most common, if you want private code just use this

    Make an SSH key

    Check and make sure you have a program called Git Bash. Open it up, it will look like a terminal. Type the following, You cannot use copy and paste traditionally in git bash, right click to do so:

    ssh-keygen -t rsa -C "your_email@example.com"
    

    The email you use here should be the email you will plan to use with git. For now just hit enter on the file name. If you want a password, go ahead and set it otherwise just hit enter.

    It will tell you that an id_rsa and id_rsa.pub files were created with a hash fingerprint (unless you named them differently).

    Open up the id_rsa.pub (pub for public) in your text editor. Copy everything there.

    Login to github or bitbucket (whichever you made an account with) go into their settings and find where to add an SSH key. Name it something you'll remember (typically the key file name or just default) and save the key in the box.

    Reason we are doing this is so whenever you push something to the cloud you won't be prompted to type your username and password (unless you set one on your ssh key).

    Bash

    I recommend you familiarize yourself with bash. At least know how to change directories, list directories and files, make directories, and open files in your text editor (not listed).

    Respectfully:

    cd directory/
    ls
    mkdir directory/
    

    Configure Git

    Next we will need to set some things within git, within Git Bash type the following lines then hit enter after each

    git config --global user.name "John Doe"
    git config --global user.email your_email@example.com
    

    Make sure you type your name and email associated with git/bitbucket/ssh key there. (If you want to use multiple emails you will need to setup a config file local the repo as well as a config file in ssh. Google to find out more)

    Now we will setup your text editor:

    git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -nosession"
    

    Google how to setup your text editor with git if you aren't using notepad++. Here is atom and sublime if you followed along.

    Aliases

    I recommend you also add these aliases to git. They will save some time when working in the command line. All these do is substitute the alias with the command.

    git config --global alias.co=checkout
    git config --global alias.br=branch
    git config --global alias.ci=commit
    git config --global alias.st=status
    git config --global alias.unstage=reset HEAD --
    git config --global alias.last=log -1 HEAD
    

    So now instead of writing "git checkout" you can just write "git co"

    You can check your git configuration with the following command

    git config --list
    

    Great now we can use git!


    Git Basics

    Terminology:

    • Repo - Short for repository, a directory on your operating system that is version controlled
    • Staged - Files that are marked to be added to a commit
    • Commits - Group of staged files with a message briefly explaining the changes.
    • Branch - A timeline of commits

    Read the book for more information on these terms and more... just read the book or watch a video.

    If you have any questions about a command you can search the documentation or type:

    git help command
    

    Within my guides I will make references to more or less the same 6 commands. These will be the ones you use the most.

    Init

    Git init sets up a git repo

    git init
    

    Do this in a directory with existing files to version control or in a new project directory

    Add

    Git add stages changes for a commit

    git add .
    git add specific/file/path/here.txt
    

    git add . will add every file within the repo to a commit. You might want to be selective on which files you add if you made a lot of changes.

    Commit / Ci (alias)

    git commit will create a commit of the staged files.

    git ci -m "your commit message here"
    

    This is creating a checkpoint within your changes within version control. All changes revolve around commits.

    Remote

    Creates an ssh hook for your repo to push changes to the cloud.

    git remote add origin git@github.com:Username/reponame.git
    

    Origin is just a standard name, change it as you see fit (like cloud, or github etc.) If you are adding a repo via this method before using it make sure the repo already exists on the cloud.

    Push

    Pushes and merges commits on the cloud

    git push origin master
    

    Origin is the remote we wish to push to, master is the branch we are pushing. This will sync your commits with the cloud.

    Pull

    Pulls commits from the cloud and merges them with your local repo

    git pull origin master
    

    Opposite of push. If you are pulling however, you can have a merge conflict. It's a bit lengthy to explain here so check the documentation if you run into this. Should only happen when working with multiple people.

    Essentially you have changes that conflict with each other and need to resolve it in a new commit.

    Creating a new repo and pushing it to the cloud

    Assuming you are in a directory that is brand new with initial files for a project do the following

    cd /directory/your/code/is/in
    git init
    git remote add origin git@github.com:Username/reponame.git
    git add .
    git ci -m "Initial commit"
    git push origin master -u
    

    Every time you want to save changes to version control you will type the following:

    git add .
    git ci -m "Description of changes made"
    git push origin master
    

    I recommend you only push once you are done programming for awhile.


    Those are the essential commands you will need to use git. I didn't explain branching, merging, GUI, and remote repositories but the concepts are simple and well documented on the internet. Git is not the only version control software out there however it is the most widely used and its concepts are similar to the others.

    Version control is a bit advanced and unfortunately a lot of people I know graduating don't have a firm grasp on it. It is very important to learn version control early on I feel however so you have a place to store your code and grow your portfolio.

    Within future Projects I will explaining what to do alongside version control. This is also helpful in case you get stuck you can link your project from the cloud so we can see what you wrote.


    References