Git and GitHub for Beginners (written by a n00b)
The idea of learning how to code had crossed my mind for years, but has only come to fruition the start of 2020. As a beginner programmer, learning about Git and understanding how to use it alongside GitHub can be pretty intimidating. I’ve had a GitHub account since 2015, but I’ve only really just started to understand and use Git in the past 2 weeks. I’m going to break down what I’ve learned about it so far.
Git vs. GitHub: What’s the difference?
Git is a decentralized version control system. A version control system (VCS) allows you to track changes to files. An important thing to keep in mind is that Git, being a decentralized (or distributed) VCS, is tracking changes locally, meaning the changes are tracked on the machine being used (i.e. your laptop).
GitHub is a remote host for Git repositories. A repository is essentially all of the versions of files Git is tracking within a directory. GitHub is a way to store and access your tracked project directories and files online. GitHub has tons of other features that make it one of the most widely used technologies in the development community.
The main takeaway here is that Git and GitHub are NOT the same thing! They are 2 separate technologies. We can use Git with other remote host repositories like GitLab or Bitbucket.
The Basic Terminology
repository (or repo): collection of stored Git tracked files with versions and changes. These can be local and/or remote.
fork: a copy of a remote repository where you can make changes and collaborate without affecting the original repository.
clone: a copy of a remote repository that can be stored and updated locally.
branch: a version of a repo pointing to a set of changes. The default/main branch is often called the master
branch, and it is best practice not to make updates directly on the master
branch. Additional branches should be created to duplicate the code and test prior to updating the master
branch.
working directory: the active branch changes are currently being made to
commit: a snapshot of the revisions made. A single commit will include all the latest changes that have been staged.
staging area: where changed files are added and tracked prior to committing.
pull request (PR): a request indicating to collaborators on a repository that a set of changes/commits have been made and are ready for review to be included in another branch
merge: the act of integrating changes from one branch to another
Useful Git Commands
git init
Creates a repository within the directory it is initiated in and tracks changes for all files within that directory.
git clone [repo-link].git
Clones a remote repository to a local directory.
git checkout master
git checkout -b [branch-name]
Switches the current/active branch to the indicated branch i.e. master
.
The -b
flag creates a new branch i.e. [branch-name]
then switches to the newly created branch.
git branch
Displays a list of local branches.
git status
Displays current branch, whether the branch is synced with the remote repo, any files that have been changed within the working directory, and files that are staged/ready to be committed.
git add .
git add [file-name.html]
Adds changed files from the working directory to the staging area. Including .
will move all changed files to staging, but you can add files individually, i.e. [file-name.html]
, in order to stage and commit changed files in smaller chunks so that your commits can be more specific.
git commit -m "[Add message about changes.]"
Creates a commit for all changes that were staged. Commits require a message to be added to indicate what changes have been made to the files.
git push origin HEAD
Pushes local commits to the active corresponding branch in the remote repo. origin
represents the original repo the local repo is cloned from. head
represents the current working directory. Not including head
will assume the repo’s default branch which is typically master
, so it’s important to include this!
git pull origin master
Syncs current branch with the remote master branch. All local changes should be committed before the branch can be synced.
For a full list of commands, check out https://git-scm.com/docs/git#_git_commands