Git Internals :)

Git

What is git internals CS50 ?

  • designed to be distributed.
  • .git -> folder inside your working directory
    • changes that we make in local are stored in the .git folder --> it will be logged into this git folder

Directory inside Git

  • Working , Staging , Repo
  • git add filename--> put the files from the working to staging
  • git commit -m "any message" --> staging to repo

  • for each commit we make a new snapsot of the repository is created

Types of Object in git

  1. blob --> object are used to store the data or the contents of the file that is being modified, or added or deleted any change
  2. Tree --> used to store the name of the file
  3. commit--> it stores a new hash everytime we make a commit

What is Branch ?

  1. branch will basically point to the latest commit
  2. lets say I have added one commit --> hash is created for it and branch will point to this commit
  3. branch is like a pointer to the latest commit
  4. identifier of new commit --> is unique it is a SHA created
  5. everytime we change something in git, git will not store the changes it will store the new file with the changes
  6. so that it will be easier to compare two files in the future and anytime we are at the latest changes
  7. I know you must be thinking it is stupid to store the new file instead of only changes(subversion do this) , but there is some compression layer which makes this much easier
  8. Size of the repository
    • compression layer so that the size of the repository will not increase since in each commit , what ever changes are there are stored in a new file
    • this compression layer does the job

What is Diff ? --> pending

  1. Diff --> what does it compare git diff -->compare workin with repo
  2. git diff --staged --> comparing staging to repo --> if no chanes are there it will not print anything
  • minus means old file
  • plus means new file or new changes
  • we can also compare any two files using git diff hash1 hash2
  • we can also compare files in different brnach

restore / reset / revert changes in git

Restore
  • we can revert our changes from the working directory
  • git restore filename --> this will revert the changes in our working directory to older file
reset : unstage a single file
  • means movin file from stage to working directory
  • git reset filename.md
reset : unstage all the files
  • git reset HEAD --> read more on this ?
how to trash your changes ?
  1. git reset --hard HEAD
reverting your changes to some older commit --> reverting one file to older commit
  • git diff commithash --> this will compare the commit with the working directory
  • so we can see if there are any file that we want to be same as the older commit
revert the file to older commit
  1. git checkout commit filename --> this will bring the file from that commit to our working directory
distard changes in working direcotory
  1. git restore filename --> will make the changes in working directory to older version

what is head ?

How to checkout older commits ?

  • Back in Time
    • how to go back to the history git log --> to check all the commits git checkout commitHash --> In the above command -> git will search the commit in the log and then point to that commit. so branch does not work like in a linear fashion it basically points to any of the commit

-> then we can go --> git checkout master --> to go to the master branch and all the changes will be there -> this is really cool since we can point to a specific commit message and it will show all the changes what is there in the working directory

What is merging vs rebasing

  • rebase : deleting commit from one branch and add it to another branch

what is forked history ?

What is git merge ?

Staging Area

Stash

  • working in branch A and now there is some issue in branch B so we have to fix the bug in branch B but there are some uncommited changes in branch A that I don't want to commit
  • but I also wanted to work in branch B

  • How to work here

      1. Commit A changes and then push the changes to fix the bug in B and then come back to A and run git reset HEAD to get your changes back
  • git stash use : it saves the local chanes of branch A and then we can switch to branch B perform our git operations

  • and then again reapply the stashed changes you need them,stash is locally scoped

How to use git stash

Here's the sequence to follow when using git stash:

  1. Save changes to branch A.
  2. Run git stash.
  3. Check out branch B.
  4. Fix the bug in branch B.
  5. Commit and (optionally) push to remote.
  6. Check out branch A
  7. Run git stash pop to get your stashed changes back.
Listing stashes
  • git stash list --> to see all the stashes
Use any stashed changes

git stash pop stash@{1}

description to git stash
  • git stash save "some kind of description"
Created new branch from a stash

git stash branch test_2 stash@{0}