cdobratz / Git-Command-Cheat-Sheet

This repository is to store a reference sheet of git and github commands.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

One More Git Cheatsheet

This document is study resources to prepare for the Github Foundation Certification.

Originally this repo started to house basic git commands. Recently I am adding my notes as I go through this course from Andrew Brown of ExamPro.

Table of Contents

Octocat!

Git Overview of Terms

  • Repository (repo): Represents the container holding the codebase

  • Commit: Represents a change of data in the local repo.

  • Tree: Represents the entire history of a repo.

  • Remote: A version of your project hosted elsewhere, used for exchanging commits.

  • Branches: Divergent paths of developement, allowing isolated changes.

  • Main (formally known as Master): common name for default branch

  • Clone: Creates a complete local copy of a repo, including its history.

  • Checkout: Switches between different branches or commits in your repo.

  • Pull: Downloads changes from remote repo and merges them into your branch

  • Push: Uploads local repo changes to a remote repo.

  • Fetch: Downloads data from remote repo without integrating it into your work

  • Reset: Undoes local changes, with options to unstage or revert commits.

  • Merge: Combines multiple commit histories into one.

  • Staging files: Prepares and organizes for a commit.

    • Add: Adds changes to file or files bringing them to the staging area for the next commit.
    • Commit: Saves changes as a snapshot in the local repo
  • To see the complete documentation follow Links to Git Docs

  • If your in the wild and get stuck use git help [command]

Basic Commands

main      # default level branch
origin    # default upstream
HEAD      # current branch
HEAD^     # parent of HEAD

Create

From existing repo

Three main ways to clone a repo

  • HTTPS
  • SSH
  • Github CLI

It is possible to clone entire repo or a single branch

git clone [URL] #bring repo from existing repo
git clone ~/old ~/new
git clone --single-branch
From existing files
git init`
git add my file #add file to repo from local to repo
git add . #add all files
git diff --staged #used after `git add` to review changes to code

Commit

Watches changes that have been marked explictly with add
git commit -m "Commit Message" # add changes of repo to main branch
git commit -a -m "Com Message" # -a: automatically stages all tracked modified files before commit
git commit --amend # Modifies most recent commit
git commit -m "Initial Commit" --allow empty # Creates empty commit to act as placeholder
git commit -m "Message" --author="name <email@example.com>" # commit with specific author
git push # send local files to remote
git push -u origin # the -u sets upstream branch

View

git status # check local files to main github branch
git diff [oldid newid]
git log [-p] [file|dir]
git blame file
git show id
git show id:file
git branch # see branches

Branches

Can be created through

  • Issues tab
  • w/ GitHub UI
  • w/ GitHub Desktop
  • In terminal (below)
git branch # Lists all local branches
git branch [branch-name] # creates new branch
git branch -m [old-name][new-name] # Rename a branch
git branch -d anybranch # with -d anybranch will be deleted
git branch -a # Lists both remote and local branches
git checkout [branch-name] # switchs to branch-name
git checkout -b newbranch # this creates and moves to new branch
git merge # branches together

Update

git pull # get latest changes from branch
git fetch [remote-name] # Fetch updates withour pulling
git apply patch.diff

Remote

# Lists all remote repositories along with their URLs
git remote -v
git remote add [name] [URL]                
git remote remove [name]     
git remote rename [old-name] [new-name]
# Pushes a branch and its commits to the specific remote
git push [remote-name] [branch]
  
# Pull updates from a remote branch        
git pull [remote-name] [branch]
This describes a new commit that undoes previous commits
git reset --hard # NOT UNDONE, reset to last commit
git revert branch
git commit -a --amend # replaces prev commit

Useful Tools

  • git log view log history
  • git archive create release tarball
  • git bisect binary search for defects
  • git cherry-pick take single commit from elsewhere
  • git fsck check tree
  • git gc compress metadata
  • git rebase forward-port local changes to remote branch
  • git remote add URL register new remote repo for tree
  • git stash Temporarily set aside changes

Git Workflows

Simple Github Workflow Example
  • Create new branch
  • Add new feature and code
  • add, commit, and push changes to the remote
  • Get changes reviewed by team member
  • Delete remote branch
  • Delete local branch
  • Pull new code on the remote master to local machine
Workflow with commands
git checkout -b my-new-branch
# ... make changes to files 
git add .   
git commit -m "my changes"   
git push -u origin my-new-branch 

Extra modifications

  • After pull request complete
  • git tag 1.0.1
  • git push --tags

The rest of the notes can be found at my other Repo

About

This repository is to store a reference sheet of git and github commands.

License:GNU General Public License v3.0