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 staginggit commit -m "any message"
--> staging to repofor each commit we make a new snapsot of the repository is created
Types of Object in git
blob
--> object are used to store the data or the contents of the file that is being modified, or added or deleted any changeTree
--> used to store the name of the filecommit
--> it stores a new hash everytime we make a commit
What is Branch ?
branch will basically point to the latest commit
- lets say I have added one commit --> hash is created for it and branch will point to this commit
- branch is like a pointer to the latest commit
- identifier of new commit --> is unique it is a SHA created
- everytime we change something in git, git will not store the changes it will store the new file with the changes
- so that it will be easier to compare two files in the future and anytime we are at the latest changes
- 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
- 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
- Diff --> what does it compare
git diff
-->compare workin with repo 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 ?
- 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
git checkout commit filename
--> this will bring the file from that commit to our working directory
distard changes in working direcotory
- 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 commitsgit 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
- how to go back to the history
-> 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
- 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
- Commit A changes and then push the changes to fix the bug in B and then come back to A and run
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:
- Save changes to branch A.
- Run
git stash
. - Check out branch B.
- Fix the bug in branch B.
- Commit and (optionally) push to remote.
- Check out branch A
- 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}