Git shouldn't be scary, but it can be when you are first starting to use it. Here's a quick GIT guide you can use as a reference or cheat sheet. I am always forgetting so this may be for my own benefit.

Configuring GIT

Configuring Git
git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
# Optional: 
git config format.pretty oneline
# Set your default git editor
git config --global core.editor vim
# Generate your SSH Key
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"
# Display and copy your new SSH key
cat ~/.ssh/id_rsa.pub
# Add it to your Github account by visiting:
https://github.com/settings/keys 
# Check that it worked by running
ssh -T git@github.com
# You should see this: Hi Name!, 
# You've successfully authenticated, but GitHub does not provide shell access.

# View your configuration settings
git config --list

Getting Help

Getting Help
git help
man git

Creating A New Repository

Creating A New Repository
# Initialize a local git repository with 
git init
# If git init does not work, make sure you have a .git folder. 
mkdir .git

Clone An Existing Repository

Clone An Existing Repository
# clone a repository with SSH
git clone git@github.com:username/application-name.git
# clone with https (better to use SSH)
git clone: https://github.com/username/application-name.git
# Add a remote origin to your existing application 
git remote add origin <remote_url>
# View the remote with
git remote -v

# View information about your remote
git remote show origin

# Rename your remote
git remote rename mirrorcommunications mirrorcomm

Committing & Inspecting Local Changes

Committing & Inspecting Local Changes
# Review file changes before staging them with
git diff
# See the files you have staged with
git diff --staged
# Add files to the staging area with
git add <filename>
# Add multiple files for staging with
git add <filename> <filename>
# For an interactive view of your file changes, run
git add -p <filename>
# Add all files to staging with
git add .
# Commit staged changes with
git commit -m 'commit message'

# Create tags (for software development) for your commits
git tag 1.0.0 1b2e1d63ff
# List your tags
git tag
# List all tags for a specific segment (all tags that end in 1.86)
git tag -l "v1.86*" 
# View a specific tag
git show v.1.0.0
# Delete a tag
git tag -d  v1.0.0
# Delete a tag from a remote server
git push origin --delete <tagname>

# Show your file changes that have not been committed yet with 
git status

Git Log Commands

Git Log Commands
# Show your last 5 commits with 
git log
# Show a specified number of commits with
git log -3
# Show all commits with
git log --all
# Filter commits by author, with 
git log --author=Brian
# Filter commits by committer with 
git log --committer=Brian
# View commits by date range
git log --before or --after <date>
# Examples: git log --after 2.days.ago | git log --after "2019-01-05" --before "2016-01-05"
# To view the changes for each commit found, run
git log -p 
git log -p 2 # last two commits
# To view a summary of the changes made in each commit run
git log --stat
# View one line of information per commit
git log --oneline
# View one line of information per commit and make it look pretty
git log --pretty=oneline
# Find more options with 
git help log

Creating & Merging New Branches

Creating New Branches
# Create a new branch with
git branch <new-branch-name>
# Move into the new branch with
git checkout <new-branch-name>
# Create a new branch and switch into it automatically
git checkout -b <new-branch-name>
# List all of your branches with
git branch
# Merge a branch into master
git checkout master
git merge <branch-name>
# Delete a branch
git branch -d <branch-name>

# View the last commit of all your branches
git branch -v
# View branches you have merged
git branch --merged
# View branches you haven't merged
git branch --no-merged

Rebasing

Rebasing
# NOTE: Never use rebase on public branches
# Rebasing allows you to incorporate changes from your master branch into a feature branch
git checkout some-feature
git rebase master 
# This moves the entire feature branch to begin at the top of the master branch

# Interactive Rebasing
# Allows you to review all the commits that are about to be moved
git checkout some-feature
git rebase -i master 

# Only move the last three commits example
git rebase -i HEAD~3

# After running the above command you can use:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into the previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

Push, Pull & Fetch

Push, Pull & Fetch
# Push your code to a remote repository with
git push origin master 
git push heroku master etc.
# Forcing a push with -f
git push -f <location>
# Specify a remote branch to push
git push <location> <branch-name>

# Pull changes from a remote or server with
git pull <location>
# Pull changes from a remote or server branch with
git checkout --track <location> <branch-name>
# Download and inspect changes from a remote server without committing them
git fetch <location>

Fixing Errors

Fixing Errors
# Revert back to a working commit with (Be Careful with this!)
git reset --hard <commit-string>
Other options include --soft and --mixed
# Unstage a file, but keep your changes
git reset HEAD <filename> 
# Reset two commits
git reset HEAD~2
# Discard changes in a file (Be Careful with this!)
git checkout --<filename>
# Drop your local changes and use the server instead
git fetch origin
git reset --hard origin/master
# Revert to the second last commit with
git revert HEAD^
# Revert a commit with a specified id
git revert c7861gf
# Modify your last commit message with
git commit --amend -m "New Commit Message"
# Add a file to a previous commit
git add somefile
git commit --amend --no-edit
# Remove a staged file for a commit (Maybe a file you don't want tracked!)
git rm --cached <filename>
echo <filename> >> .gitignore
# For merge conflicts, run
git status
# Discard local file modifications with: 
git checkout --<filename> or <folder> <folder2>

Aliases

You can also set up aliases for git commands. I prefer to do this in my ~/.bash_aliases file, but the approach below is also a great option.

Aliases
git config --global alias.ga git add .
git config --global alias.ci commit
git config --global alias.st status

Cherry Pick

The Cherry Pick command lets you add commits to the current working HEAD. Basically, you are picking a commit from another branch and applying it to a new branch or master.

Cherry Pick
# First find the commit you want to cherry pick
git log --oneline
# Now, copy the commit string
# Switch into the branch where you want to put the cherry-picked commit
git checkout new-feature
# Now add it with
git cherry-pick <commit-string>

Working with Forks

If you want to experiment with an existing repository, you can fork it and create changes without affecting the original project. To do this, view an existing repository on Github and hit the Fork button.

Working with Forks
# Now git clone your Fork
git clone git@github.com:username/repo-name.git
cd repo-name
$ git remote -v
# This will list your current configured remote repository for your fork
# Example:
> origin  https://github.com/username/your_fork.git (fetch)
> origin  https://github.com/username/your_fork.git (push)

# Now add the original repository to sync with your fork
git remote add upstream git@github.com/original_owner/original_repository.git

# Verify it worked by running
$ git remote -v

# Fetch changes from the remote repository with
git fetch remote-name

# Pull and merge changes from a remote repository
git pull remote-name branch-name
# Grabs online updates and merges them with your local work

# If you want your changes to be added to the original repo
git push origin master
# Now visit your Fork on Github
# Next create a pull request