Command Line Git - risdesignet/wd-winter2019 GitHub Wiki

This is a list of terms and commands you need to know to use your terminal for interfacing with programs on your computer, git version control, and github.

Key Terms

Shell - The shell is a command line interface for running programs on your computer.

Terminal - The program we use to interact with the shell is called a terminal program. If you are on Mac or Linux, you already have a good one of these. If you are on Windows you can download a shell and terminal program that works as well as the one on Linux. Windows does have a command shell, however it runs on the old MSDOS program rather than a Unix heritage.

Command Prompt - The prompt your terminal displays when waiting for your command. Usually looks like: Your-User-Name-Computer:~ Working_Directory$

Directory - Another name for folder.

Working Directory - The folder your terminal is currently pointing to.

PATH - The series of steps you took to get to a particular directory or file. A PATH lists the directories you stepped into to get to a file or folder, and separates each step with a forward slash. Ex. Users/BobbyJoe/Documents/Code/WinterSession2018/index.html

GIT Version Control - A Version Control System is just software that helps you control (or manage) different versions of something (typically source code). Git is a local (meaning stored on your computer) form of version control that we run via our computer's terminal. The main point of a version control system is to help you maintain a detailed history of the project as well as the ability to wok on different versions of it. Having a detailed history of a project is important because it lets you see the progress of the project over time. If needed, you can also jump back to any point in the project to recover the data or files. It’s like clicking the undo button in a word document, except git version control is substantially more powerful. Git also gives you the ability to:

  • label a change
  • give a detailed explanation of why a change was made
  • move between different versions of the same document
  • undo change A, make edit B, then get back change A without affecting edit B

GitHub GitHub is an online (remote) repository for storing files and commits for a project. It's like the google drive, where you can save files online, create and edit documents, work on a document simultaneously with people working on other computers, and give access to other people to read or modify certain files or folders. Git does the same thing, but with code, and it saves the history of revisions to files and folders.

Commit - Git thinks of its data like a set of snapshots of a mini filesystem. Every time you commit(save the state of your project in Git), it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. You can think of it as a save point in a game - it saves your project's files and any information about them. Everything you do in Git is to help you make commits, so a commit is the fundamental unit in Git.

Repository/Repo - A repository is a directory which contains your project work, as well as a few files (hidden by default on Mac OS X) which are used to communicate with Git. Repositories can exist either locally on your computer or as a remote copy on another computer. A repository is made up of commits.

Working Directory (When Referring to GIT) - The Working Directory is the files that you see in your computer’s file system. When you open your project files up on a code editor, you’re working with files in the Working Directory. This is in contrast to the files that have been saved (in commits!) in the repository. When working with Git, the Working Directory is also different from the command line’s concept of the current working directory which is the directory that your shell is “looking at” right now.

Staging Area/Staging Index/Index - A file in the Git directory that stores information about what will go into your next commit. You can think of the staging area as a prep table where Git will take the next commit. Files on the Staging Index are poised to be added to the repository.

Checkout - A checkout is when content in the repository has been copied to the Working Directory.

Branch - A branch is when a new line of development is created that diverges from the main line of development. This alternative line of development can continue without altering the main line. You can think of a branch as where you make a save point in a video game and then decide to try out a risky move in the game. If the risky move doesn't pan out, then you can just go back to the save point. The key thing that makes branches incredibly powerful is that you can make save points on one branch, and then switch to a different branch and make save points there, too.

SHA - A SHA is basically an ID number for each commit. Here’s what a commit’s SHA might look like: e2adf8ae3e2e4ed40add75cc44cf9d0a869afeb6\. It is a 40-character string composed of characters (0-9 and a-f) and calculated based on the contents of a file or directory structure in Git. “SHA” is shorthand for “Secure Hash Algorithm”.

BASH Commands for Interfacing with Your Computer Via the Terminal

These are the commands you will use inside your terminal to navigate to your project folders, create directories and files, move them, edit them, rename them, or remove them. Type these commands into your terminal to perform the action. Do not include the <> marks in your commands, they are there to signify a variable (referred to as argument) where you will need to include specific information.

pwd - Print Working Directory. Prints the PATH to the Working Directory (the directory your terminal is currently pointing to).

pwd

ls - List. Lists the names of all of the visible files and folder in the working directory

ls

ls -a is a modifier to the ls command that will list all of the visible and hidden files and directories in the working directory. cd - Change Directory. Allows you to move into a different directory. Include the PATH to the directory you would like to move into.

cd <folderName>

mv - Move. Allows you to move files or folders into a different directory. Include the PATH to the files and directories you would like to move, and the PATH to the directory you would like to move them into.

mv <nameOfFileYouWantToMove> <whereYouWantToMoveIt>

mv can also be used to rename a file or folder by having the first variable be the current name of the file and the second variable be the name you want to change it to. mv <currentFileName> <newFileName>

There are special names for folders that can be used with terminal commands like cd or mv. .. stands for parent directory, . stands for working directory, / stands for root folder, ~ stands for home directory.

mkdir - Make Directory. This command creates a new folder in the working directory.

mkdir <nameOfDirectory>

touch - Create a new file. This command creates a new file in the working directory.

touch <nameOfFile>

rm - Remove. This command can be used to delete a file or folder.

rm <nameOfFile>

To delete an entire folder you will need to use the command rm -r <folderName>

GIT Version Control

Git version control allows you to save versions of your coding projects on your local computer. Just like with a document you're working on in illustrator or Microsoft Office, you should save your work often. With Git version control, not only will you save your work, but you will keep a record of the changes you have made.

Set Up

To set up git version control for a project follow this process:

  1. Navigate to/Create Project Directory - In your terminal, navigate to the root directory (the folder containing all of your project files and folder) of your project. If you're starting a new project and have not created a folder yet for your project, you can create one with the command mkdir <folderName>. OR you can create a root project folder AND initialize a git repo (step 2) by typing the command git init <folderName>. This tells the computer to create a folder with the name you gave it and initialize a git repository inside of it. If you set up your root project folder and initialized a git repository by using git init <folderName> then you can skip step 2.
  2. Initialize Git Repository - Initialize a git repository by entering the command
git init

into your terminal. This will set up a git repository inside of your root project directory. It is important to set up the git repository only inside of the root directory otherwise it wont keep track of all of your files. The git repository is hidden inside of your finder widow, so you will need to use the command ls -a to be able to see it. The git repository will appear as a .git directory. Do not change any of the files inside of the .git directory.

Committing Versions of your Code to Your Git Repository

A git repository is now set up in your root project folder. Any changes you make to files or directories inside of your project folder will be tracked by git. You can now begin working on your project. As you work on your project you will need to save significant changes to your work to git, like creating a header, adding images, changing a font or background color. Any working changes that alter the look or functionality of your project. In git, this is called making commits to the repository — you are saving snapshots of your project to the git repository that you can later refer back to. Anytime you are ready to make a commit to your repository follow this process:

  1. Save Files - Hit save on any project files to which you made changes.
  2. Check Git Status - First you want to see what changes if any have been made. You can ask git what changes inside of your root project directory have been made by typing the command:
git status

Git will give you a response. If git doesn't see any changes made to the folder since the last commit that was made, you will receive the response:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

Git is telling you that your working directory (in the git sense) is in line with the last commit you made. If there are some changes that haven't been been committed yet, you'll get a response that lists all of the modified files and directories that need to be staged for a commit:

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified:   <fileName>
no changes added to commit (use "git add" and/or "git commit -a")

In the response, git is telling you which files need to be staged for a commit and the command you need to run to do it. 3. Stage Modified Files for Commit - In order to create a commit, you need to add the files you want to commit to the "staging area". This tells git which modified files you want git to track as part of the commit. You can do this with the command:

git add <nameOfFile1> <nameOfFile2>

You can use the response from git status as a reference for the names of each modified file that needs to be staged for a commit. If there are a number of files that need to be staged for commit, instead of typing out each name, you can inlude them all with the command git add -A. This tells git to stage all of the modified files in the working directory. You can then run git status again to be check that all of the files you wanted were moved from the working directory to the staging area. 4. Commit Modified Files to Repo - Now that you have the modified files staged for commit, all that's left is to commit them to the repository and leave a note about what changes were made. You can do this with the command:

git commit -m "Message"

The commit message should be written in parenthesis, and best practice is to have the commit message be written in present tense such as "Add Hero Image to Homepage" or "Change background color to black". Once you commit the changed files, git will respond with a message that tells you the SHA for the commit, the message, and how many files changed, along with the number of insertions and deletions in code that were made between this commit and the last one. 5. Push to Remote Repository - If you have a remote github repository established, you can now push the commit you made to your local repository to the remote repository with the command:

git push origin master

This will only work if you already connected your remote repository. If you have not, go through the instructions in the next section to set up a remote repository and connect it. 6. Make More Changes and Repeat - That's all there is to it! You just saved a version of your code to your repository. You can now make more changes, and repeat the steps of adding and committing those changes to the repository.

Remote Version Control with GitHub

The git version control process commits versions of your code to a git repository on your computer. If you want to make your code and git repository available to other computers, you will need to establish a connection to a remote repository on hosted on the web, and sync your local repository to the remote repository. The remote repository we are using in class is appropriately name github. The following steps will show you how to set up a connection to your github account and push your local commits to a remote repository using your command terminal.

Set Up

  1. Create New GitHub Repository - Got to github.com and sign into your account. In the nav bar in the top right corner you will see a plus sign with a dropdown menu arrow next to it. Click on the dropdown and select the "New repository" option. This will begin the process for creating a remote repository.
  2. Fill In Information - Selecting "New repository" will bring you to a page where you can set up your github repo. All you need to do on this page is name your repository. Check to make sure your repository is set to public, and that "Initialize this repository with a README" is NOT selected. You also do not need to add a .gitignore file or a license, so both of those options can be left on the default setting of "None". Click the button "Create repository," and there you have it! You've now created a remote repository on github. All you have left to do is establish a connection between your local repository and the remote repository you just created.
  3. Connect Local and Remote Repositories - Once you select "Create repository" github will bring you to page of instructions on how to push your local repository up to the remote repository you just created. You will see a url that github generated as an address for your remote repository. Copy that url for use in your terminal. Go to your terminal and navigate into your root project folder containing your local git repository. Once in the root folder type the command:
git remote add origin <urlOfRemoteGitRepository>

This adds a remote git repository to the url you provided with the name "origin". You could actually name your remote repository anything you wanted by swapping out the word origin with the name your prefer, but it is common practice to just use the name "origin". 4. Push Local Repository to Remote Repository - Now that you've established a connection to your remote repository, you can push all of your code and commit history to that remote repo with the command:

git push -u origin master

This line of code pushes your code and commit history in your local repository to origin. The modifier -u tells git to remember to push to this remote repository in the future. You only need to run the commands in steps 3 and 4 once when setting up the connection between your remote and local repositories. After that you can push your code and commit history with the just one command git push origin master.

⚠️ **GitHub.com Fallback** ⚠️