johnny-dreamguns
10/7/2019 - 10:25 PM

How Git Works

How Git Works

Git is a persistent map, the keys are SHA1s and the values are pieces of content

To view blobs use git cat-file -p KEY

A blob is a piece of content stored in git in a compressed format, it has a SHA1 key, it stores them in the objects directory Commits are files of text stored compressed in the objects directory (similar to blob) with a SHA1 key. It contains a tree, a parent, an author, a committer and a message A tree is a root, it points to blobs and other trees Tags are labels pointing at an object

When a new commit is created, previous blobs and trees are reused if their content hasn't changed

Master key is stored at .git/refs/heads/master

HEAD file points to the current branch

Checkout moves HEAD to point at a different branch and updates the working area

Merge creates a new commit that has a tree that points at both the current branch and the one we're merging in

Rebasing makes a branch the parent of another branch so they share the same log, it is an alternative to a merge

Rebasing creates a nice neat log, vs the messy log you get using merging. Can create unwanted side effects

A remote is a clone of the current repo

Origin is the default remote

A fork is a clone of a GitHub repo that is also stored in the cloud. We can then clone that to our local machine

An upstream is a connection to the original forked repo so we can pull any changes to that into the local copy of the code

A pull request is a message to the owner of the original repo, asking them to pull your changes from the forked repo

Mastering Git

Three areas: Working area Index Repository

For each command think about- "How does this command move information across the four areas?" "How does this command change the repository?"

In the clean status the working area and the repository are aligned

The index is a launchpad or staging area, it's a binary file within the .git folder

Alternatively the index can be thought of as containing everything in the repository

HEAD points to the current working branch

Commands: git status | gives you the current status of the project, shows modified files in working area

git diff | compares the working are with the Index

git diff --cached | compares the Index with the Repository

git diff mybranch master | compare two branches

git add filename.txt | Moves changes from Working Area to Index, "git status" will now show the file as modified and staged

git add. | Add files in a local directory or below

git add --all | Add everything

git commit -m "My comment" | Moves modified files from Index to the Repository

git checkout mybranch | Move HEAD to point at mybranch, copy files from that branch to Working Area and Index

git checkout { CommitID } | Checkout a commit

git branch | show the branches

git branch mybranch | Create new branch

git branch --all | Also show remote branches

git rm --cached filename.txt | Remove file from Index but keep in working area (Unstaged the file)

git rm --force filename.txt | Remove file Working area and Index

git mv | rename file in the working area and in the index

git init | create a new repo

git log | Look at comments

git tag -a mytag | Create a tag

git cat-file -p KEY | View compressed data

git show | show changes for a commit

git merge mybranch | merge the current branch with the branch passed in

git rebase mybranch | rebase the branch passed in

git clone | create a clone of a repo on the current machine

git push git push origin master | add objects from master to the origin remote repo

git fetch | get latest changes from origin so they can be merged

git pull | fetch and merge in changes from origin

git reset moves the head to point somewhere else and copies files over the Index and optionally the working area git reset { CommitID } --hard | Move head to commit ID passed in and copies files to Index and Working area git reset HEAD | Move current head to Index only, this uses mixed mode git reset --hard HEAD | Copy current head to Index and Working area git reset HEAD file.txt | Reset a particular file To reset a specific file in the work area as well as index then use git checkout HEAD file.txt

git stash --include-untracked | Move files to the stash area

git stash list | See files in the stash

git stash apply | move latest stash item back into the working area

To Rename a file: Use mv command to rename in working area Use add to move renamed file to Index Use add on original filename to remove original file from index, file will be renamed on index Use commit to add to Repository