Skip to content

Instantly share code, notes, and snippets.

@davidagitlen
Last active March 26, 2019 06:58
Show Gist options
  • Save davidagitlen/fc91bd1aeff5746e08cdd82b084ec43e to your computer and use it in GitHub Desktop.
Save davidagitlen/fc91bd1aeff5746e08cdd82b084ec43e to your computer and use it in GitHub Desktop.

Beginner's Guide to Git

Here is a basic introduction to using Git!

What is Git?

Git is one of the most popular Version Control Systems (VCS) in use today. It is used to track changes made to files, such that previous versions of the same file can be recalled and a record of each change can be seen. This is useful not only for modulating how specific features are built out, but also for identifying points in time that problems are potentially introduced, and eventually for facilitating collaborative workflow for team projects with GitHub.

Basic Git Commands in Terminal

In order to use Git you will need to know some basic commands to enter in your terminal. Once you have a file created you will need to acquaint yourself with the following three commands:

  1. Git init
  2. Git add
  3. Git commit -m "message"

These will (1) tell Git that you want the file to be tracked, (2) put your file into the "staging area", and (3) add a "snapshot" of the file with whatever changes you have made. The first step only needs to be done one time to whatever you are working on, and you will know that it is successful because you will see (master) after the file path in the terminal, which should look something like this: Davids-MacBook-Pro:mod0resources (master). Changes made to a file being tracked are added to the staging area and then commited along with a message, the first of which will always be "Initial commit". Subsequent commit messages should begin with a capitalized verb in the imperative, and should be concise and descriptive of the change you are tracking. The second and third steps will be repeated as you continue to make alterations to the file and commit them to create a chronological record of changes.

Throughout this process you can check exactly where you are with two additional commands:

  • git status
    • this command will show you what Git is tracking and shows a record of any commits
  • git diff
    • this command checks the changes made since the last commit

Using Git Commands in Conjunction with GitHub

Once you are comfortable with the basics of initializing, adding, and commiting from the command line, you will need to know how to integrate using these commands with the online database of the "snapshots" you have been taking, which is GitHub.

The code you are working on will be organized into repositories, and each repository will contain records of the changes to each file in different branches, with a main branch referred to as the master and any number of branches coming from whichever point the user chooses. You can go into a repository and either clone it to create a copy of the files contained in it to a directory on your local machine, or fork it to create a personal branch, to which you can push the code you are working on. By creating a separate branch you give yourself a place to work things out without have to worry about the integrity of the master branch, and the ability to offer the code to be merged into it in an appropriate and compatible format. This is done by submitting a pull request, whereupon others working on the project will review what you have written, and provide whatever feedback is appropriate before potentially merging the code back into the master branch.

alt text

Pushing the changes you have made from the local copy of the files you are working on to a repository on GitHub is accomplished by using the command git push. This will display a block of code showing the various functions ending with a display of the target url and the branches being merged, like so:

Davids-MacBook-Pro:mod0resources (master) $ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 364 bytes | 364.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/davidagitlen/mod0resources.git
   2fd9e25..cf3a44b  master -> master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment