Sitemap
Jordan Martin

Jordan Martin

Quick Review: Git Usage Basics

3 min readMar 13, 2019

--

Over the past few months, I have been working mostly on data structure and algorithm problem or minor improvements to my own existing projects. I have found that it can be easy to get relaxed about maintaining a proper Git workflow, especially when you are the only person contributing to a project. It is all too easy to forget about correctly following the checking out of branches, adding commits, merging branches, etc. I wanted to create this blog post to help myself review a basic Git workflow procedure and serve as a reference guide for beginner programmers.

Local Repository Break Down

Press enter or click to view image in full size
Source: http://rogerdudler.github.io/git-guide/img/trees.png

Git maintains three different “trees” on your local repository. There is the Working Directory where the actual files are held. The Index acts as a staging area for new files, and finally the Head which points to the last commit

Getting Started

git init — use this command in of a local directory to create a new git repository

git clone username@hotst/path/to /repository— creates a working, local copy of an existing repository on a remote server

Making Changes

git add <filename> (or * for all files)— this the first step in Git workflow where you can propose changes are added to the Index

git commit -m ‘Commit message’— following the git add command, this commits the added files to the Head of your local repository, but does not yet add it to the remote repository

Get Jordan Martin’s stories in your inbox

Join Medium for free to get updates from this writer.

git push origin <branch> — sends your changes in the Head of your local working to the specified branch of your remote repository

Maintaining Branches

A great way to use branches is for the development of a specific feature so that you can isolate your new code. By default, the repository is set to the master branch. When you are finished developing a feature on a branch, you can merge it back to the master branch.

Press enter or click to view image in full size
source: http://rogerdudler.github.io/git-guide/img/branches.png

git checkout -b <branch> — creates a new branch with the specified name and switches to it

git checkout master — switches back to the master branch

git branch -d <branch> —deletes the specified branch,

Updating and Merging

git pull — execute in your working directory to updates your local repository to the latest commit by fetching and merging the remote changes

git merge <branch> — merges the specified branch into your current branch

git diff <source_branch> <target_branch> — show a preview of the merge to check for possible conflicts

Fixing Mistakes to Local Repository

git checkout -- <file_name> — replaces the changes in your working directory tree with the latest content in the Head

git fetch origin THEN git reset --hard origin/master — drops all local changes and commits then fetches the latest history from the remote server to use for your master branch

Source: http://rogerdudler.github.io/git-guide/

--

--