Graduate Program KB

GIT NOTES


Git Fundamentals

  • git stores compressed data in blobs with metadata in the header
  • Uses SHA1 - 40 digit hex
  • Git stores its data (value) in the .git directory (git init makes .git directory)
  • blobs are stored with an identifier blob in the header
  • tree contains pointers using SHA1 to blobs and to other trees

Commits

  • commit points to a tree and contains metadata (author, committer, tree, parent, message)

  • commits point to parents commits and trees

  • commit is a code snapshot of what the project looked like at that time

  • HEAD is a pointer to the current commit and points to current branch, usually a pointer to the current branch

Note: Viewing Commits via CLI

tree .git/objects
git cat-file -t af11c      (tells us what it is pointing to (ie a tree, commit etc)
git cat-file -p af11c      (tell  us what the file content is) (edited)

Branches

git branch new_branch
git checkout -b new_branch

Git Areas

  1. Working Area (Untracked files, things you are currently working on)
  2. Staging Area (Files that are going to be part of next commit, how git knows what will change between current commit and the next)
  3. Repository (Files git knows about, contains all commits as well)

Moving in and out of the Staging Area

git add <file>              (Add file to next commit)
git rm <file>                (Delete file in the next commit)
git mv <file>                (Rename file in the next commit)

GIT ADD -P

  • Allows you to stage commits in hunks, interactively

  • git add -p

  • NOTE: See the difference of two files

  • git diff --staged (edited)

Git Stash

  • Save un-committed work
  • Helps keep track of what you were working on
  • Stash is safe from destructive operations
  • Example you can stash changes and switch to a new branch

Stashing Example

git ls-files -s       (Views contents of the staging area)
git add -p            (Make changes and stage interactively)
git reset             (Undo staging of your file)

git stash
git stash save "message"            (Stashes changes)
git stash list                      (Shows stashed changes)
git stash show                      (Gives more info on the stash)

git stash apply                     (Automatically apply last stash)
git stash apply stash@{0}           (Apply a specific stashed change)

References, Commits and Branches

References (Three Types)

  • Tags = and annotations
  • branches
  • HEAD

Annotated Tags

  • git tag -a v1.0 -m "Version 1.0 of my blog"
  • git tag (shows the tags)
  • git show v1.0 (git show gives all info of the tag)

Terminolgy

  • branch - the current pointer moves with every commit to the repo
  • tags - snapshot of the release (you dont move a tag to another commit)

Headless / Detached Head

  • when you checkout a specific commit or different branch
  • head moves when you move
  • if you commit in another branch then if you make changes in a detached head state they are lost once you move again
  • we must make a new branch that points to the commit you have just made or the commit in the detached head state is lost forever

References

git show-ref   (See where your head is pointing at)
git cat-file -p 43bbf    (This is the initial commit and the command above shows our master branch is pointing at the initial commit)

git tag my_exercise_3-tag       (makes light weight tag)
git show-ref --tags                     (shows available tags)
git tag -a "exercise 3 annotated tags" -m "this is a message"    (annotated tags) (edited)

Merging and Rebasing

  • merge commit is essentially just a marker for where a feature from a branch has been added to master
  • Fast forward is when you merge a feature but master has not been changed
  • The problem is that during fast forward commit we can lose track of a feature going into master
git checkout master
git merge new_feature --no-ff

Merge Conflict

  • when two branches collide with each other
  • Git rerere (saves how you resolve a conflict)
git config --global rerere.enable true
  • git will now let you know when the conflict has been resolved
git rerere diff        (show merge conflict)

Git Log

git log
git --no-pager log --oneline          (shows the log)
git --no-paper log --graph             (shows the log with a tree format)

git log --since="yesterday"
git log --since="2 weeks ago"
git log --grep <regex>
git log --grep="i18n" --author=nina --since=2.weeks

Git Show

git show <commit>
git show <commit> --stat
  • NOTE: Want to see what branches have been merged
git branch --merged master
git branch --no-merged master

Fixing Mistakes

Git Checkout

  • allows to restore working tree files and switch branches
  • switch branches safely without changing pointer to a commit
git checkout -- <file>    (Takes file from the staging area and overwrites what is inside it)
  • updates staging area files

Restoring

  • checkout a file from a specific commit
git checkout <commit> -- <file_path>
  • restore a deleted file
git checkout <deleted commit>^ -- <file_path>

Git Clean

git clean --dry-run
git clean -d --dry-run
  • Deletes untracked files and directories

Git Reset

git reset --soft HEAD~          (Moves head to previous commit)
git reset --mixed HEAD~       (Moves head pointer to previous commit and moves file from prev commit to the staging area)
git reset --hard HEAD~          (Moves head pointer to previous commit and moves file from prev commit to the staging and working area)

Git Revert

  • safe revert, creates a new commit that introduces the opposite changes from a specified commit
  • the original commit stays in the repository
  • use revert if you are undoing a commit that has already been shared, revert does not change history , git reset does

Rebase and Amend

Git Amend

  • made a mistake to a commit
  • commits can't be changed creates a new commit
git add missing_file
git commit --amend

Rebase

  • give a commit a new parent
  • squash a bunch of commits and give commit a new name
git rebase master
git rebase -i <commit to fix>^

Reviewing Requests

  • First make sure master is up to date with pull
git pull
  • To see all branches both local and remote
git branch -v -a
  • Fetch the remote repository (So that you have the latest changes downloaded)
git fetch
  • Switch to branch of user who's PR you are checking
git checkout branchName
  • Note: Do not use origin/branchName or the head will detach on checkout (edited)

Rebase to keep branch up to date with master (Example)

  • Git pull master and your branch
git rebase master

git status
git add .
git rebase --continue
  • After fixing merge conflicts check that it is still running
git push --force

Git Stash

git stash list
git show stash@{0}
git checkout stash@{0} .vscode/extension.json
git status
git add .
git push
  • Make pull request and merge branch

Deleting remote branches from local storage (Clean up work space)

git fetch --all && git remote prune origin