Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Latest commit

 

History

History
153 lines (100 loc) · 2.62 KB

cheat_sheet.md

File metadata and controls

153 lines (100 loc) · 2.62 KB

Git Reference Sheet

This is a reference guide for the git commands that we've gone over in lecture and that you will need for the lab.

Working with git locally

  1. Create a repository in the current folder
git init
  1. Tell git to track a new file
git add <filename(s)>

# OR to simply add all the files in the current working directory:
git add .
  1. Commit changes in staging area
git commit -m "<message here>"

# ex.
git commit -m "adds initial java files for lab 8"
  1. Looking at old commits
git show <commit hash>

# ex.
git show 68a3056
  1. Looking at files in an old commit
git show <commit hash, branch, or tag>:<file_name>

# ex.
git show master:Example.java
git show 68a3056:Example2.java
  1. Undoing an old commit
git revert <commit hash>

# ex.
git revert 68a3056
  1. Check status of repository
git status
  1. Check log of recent commits
git log

# OR, for a simpler view
git log pretty=oneline
  1. To remove a file from the staging area

This will unstage the given file, meaning when you do git commit, the changes to this file will NOT be included

You will only ever need to do this if you accidentally add a file

git rm --cached <filename>

Working with git remotes

  1. To view all the remotes connected to the repository
git remote -v
  1. To clone a repository
git clone <repository URL>
  1. To pull from a remote repository
git pull <remote> <branch>

# Example:
git pull origin master
  1. To push to a remote repository
git push <remote> <branch>

# Example:
git push origin master

Branching and Merging

  1. To view branches
git branch
  1. To create a new branch
git branch <branch name>

# Or to create and switch to a new branch
git checkout -b <branch name>
  1. To switch branches
git checkout <branch name>
  1. To merge a branch into the current branch
git merge <branch name>

Troubleshooting

If something goes wrong, use git status and git log to see if you can figure out what happened. Remember, don't panic, as long as you committed something, its virtually impossible to lose it

For branching issues, try git log --graph --full-history --pretty=format:"%h%x09%d%x20%s"

Since we should all have color set up we can try git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s" for an even prettier view