GIT & Version Controlling

Version Controlling
Version control systems are a category of software tools that help a software team manage changes to source code over time(But non developers also use git for their text management like research papers). Version control software keeps track of every modification to the code in a special kind of database.Version controlling software use for or version controlling purposes.

Git
Git is a free and open source distributed version control system(VCS) designed to handle everything from small to very large projects with speed and efficiency. 
https://git-scm.com/ (download git from here)

History of Git
                                                        
Linus Torvalds Invented Git, But He Pulls No Patches With GitHub. Linus Torvalds keeps a copy of his Linux kernel project on GitHub, the wildly popular code-hosting website.But then it applies for the normal software version controlling purposes.

Git Vs Github,Bit Bucket,Git Lab

                        Git                                                         Github,Bit Bucket,git Lab
1. It is a software1. It is a service
2. It is installed locally on the system2. It is hosted on the Web
3. It is a command-line tool3. It provides a graphical interface
4. It is a tool to manage different versions of edits made to files in a git repository4. It is a space to upload a copy of your Git repository
5. It provides functionalities like Version Control System Source Code Management

5. It provides functionalities of Git like VCS and Source Code Management, as well as adding few of its own features

Git Bash
This is the command line of the git.There are GUI tools for windows substitute for the command line.But most of time industry use git bash & it creates Linux environment when you use non Linux OS. 

Configuration Git
3 types of user configuration
       git config -- global    used for current loged users.
       git config --system    used for users who are use same OS   
       git config --local        use for local repository 

Eg:-

Configurations

    git config --global --list > Show current configuration list in git

    git config --global --add core.editor notepad >set note pad set default(when type notepad in git bash>popup notepad)

    git config  --global --add user.name codedosefun>add user name

    git config  --global --add user. email codedosefun@ymail.com>add user email

    git config  --global --unset name >remove name configuration(To delete all configuration in git C:\Users\"your machine name" delete .gitconfig file)

git config --global --add credential.helper store>save user name & password(.gitcredential file includes )

Repository Management
(**Go inside project folder using any command line)

1.Commits
git status> current status of your folder.

git init> Start to track our  movements inside the  our project folder.(It create .git file inside project folder)

git add . >Add file to indexing stage

git add test.java Student.java readme.txt>(Add multiple file to the indexing stage)

git commit -m "Initial commit">commit to the repository,inside quotes specify which kind of changes did your file/s.

git commit>Add commit message using core editor
  
git commit --amend>change master commit without creating new commit(only change HEAD->Master commit) & without add indexing stage,(Cant apply for the)

git commit -a "your message here">commit without add to indexing stage,but  that file must present in before in repository(Direct commit)

git commit -am>Add commit message using core editor in direct commit.

2.Retrieve
git checkout command vs git reset command
                                                               git checkout
git checkout  use for get full history of our commits only use for viewing purposes.But we cant do any changes in this method.because HEAD in the detached state(HEAD do not point to the branch).

git checkout d837f3a>checkout specified commit.Then we can visible the previous commits before that commit. 


                                                                                                                                                                    
To reverse that can apply latest commit hash code or git checkout master.(git checkout <Latest commit hash code>,can use git reflog to show HEAD pointed status).If our working tree is not clean cant  perform  checkout command.So this method is called safe.


                                                                    git reset
But using git reset command use to restore project  to previous commit.there are 3 part of this stage..

                      --soft
 git reset       --mixed    <rivision/commit>
                      --hard

--soft used for Squashing
--mixed used for Un-staging
--hard used for get project to previous point

(**to find HEAD log master use git reflog)

                                                     M(Head)
                                                          |
(Repository) 05e60e8--cd01a80--0675925

git reset --soft <hashcode> eg:-git reset --soft cd01a80
HEAD move+branch master point also move.(when you add commit after soft reset next commits linkages will be break)
                                       M(Head)
                                            |
(Repository) 05e60e8--cd01a80--0675925


git reset --mixed <hashcode>/git reset<hashcode>eg:-git reset cd01a80
soft+HEAD pointed master copied to to indexing stage.(default)
                                       M(Head)
                                            |
(Repository) 05e60e8--cd01a80--0675925

(Indexing)                   --cd01a80--


git reset --hard <hashcode>
mixed+HEAD pointed master copied to to working directory.
But if it you have any content without add indexing stage it will be wipe(override) data.So this is not a safe method.If our working tree is not clean or clean can perform  reset command.So this method is unsafe.
                                       M(Head)
                                            |
(Repository) 05e60e8--cd01a80--0675925

(Indexing)                   --cd01a80--

(Working dir.)              --cd01a80--

To reset, git reset command command,you must clearly identify previous master using git reflog  command

git reset --<soft/mixed/hard> 0675925

                                                    use reset with file path
We can specify only path using git checkout & reset,but this is completely change previous checkout function.
When we specified file path with git  checkout equals to reset functionalities.In here HEAD,master do not move to specified commit but,but it create virtual HEAD & point to specified commit.

git reset --soft 05e60e8 -- AppInizializer.java-not working

git reset --mixed 05e60e8 -- AppInizializer.java-working,File copied to to the indexing stage.
git reset --mixed HEAD AppInizializer.java/git restore --staged HEAD AppInizializer.java>reset

git reset --hard 05e60e8 -- AppInizializer.java-not working
                                        =
git checkout 05e60e8 -- AppInizializer.java-working,File copied to to the working directory
git checkout HEAD AppInizializer.java>reset


                                                                    Squash
Squash is technique in which you bundle up some of your last insignificant or less important commits into a single one.Simply we can remove unwanted commits & start project that specified stage.
                                                                        M(Head)
                                                                            |
(Repository) 05e60e8--cd01a80--0675925--4b77dc1
git rest --soft  cd01a80>Then we can commit up to this stage --0675925--4b77dc1 commits are wipe
                                       M(Head)
                                            |
(Repository) 05e60e8--cd01a80

git rest --mixed  cd01a80>If you want copy to Indexing stage & commit up to that.
                                        M(Head)
                                            |
(Repository) 05e60e8--cd01a80                                     

(Indexing)                   --cd01a80--

3.Branching
                Mastering git branches. Branches are one of the most… | by ...
A Git branch is essentially an independent line of development. Generally commits are saved in branches.(By default master branch)
Why we create branches?
1.Add or introduce new feature to existing project.
2.When fix a bug.
3.Testing purposes.
4.Team works.

git branch <branch name> <strarting point>> Create new branch
<strarting point> can be commit,branch(local/remote),tag/by default HEAD
Eg-git branch devTaem1 qw2234>create new branch called "devTeam1" from qw2234 commit.

git checkout -b <branch name> <strarting point>>Create new branch & switch that

git branch>list out all branches & current branch
* Item
   customer
   master

git checkout customer>switch to customer branch from another branch

git branch  -m <Old name> <New name>>Rename a branch
git branch -d <branch name>>delete a branch
git branch -D <branch name>>D=d+f(delete forcefully).If you cant delete use this method to delete forcefully.

git log Item>log details of Item branch

git log customer --graph>log details with graphical view of customer branch

git log all>log of all branches

git log -p>see all changes happen in the each commit

git log --oneline -merges>show all merge commits
git log --oneline -merge>identify conflict between two merge commit

merges
If we want add our branch to main branch we want to pull request to project owner.Then project owner review our branch & he/she satisfy from that accept our request.
To merge,first must switch to parent branch that you are going to merge with child branch.After the merging branches will be delete according to the standards. There are two types of branches.

1.Fast forwarding merge 
Simply merge.Because,there is no any commit in master branch after start first sub branch
                            Fast-forward merges in Bitbucket Cloud - and by default, if you ...
2.Recursive merge
In here considering commit time recursive merge algorithm decide which commit in the which place .But when change same file in same place in the commits,will be cause to the conflicts.If conflict will happen in the commit merging we must resolve that manually & do a merge commit
                                               Merge Strategies in Git - GeeksforGeeks
 
git merge item>item branch merge to master branch
git merge --abort>Cancel the merge

3 way merging
                            Three-Way Merging: A Look Under the Hood | Dr Dobb's
A three-way merge is a merge that is performed after an automated difference analysis between a file "A" and a file "B" while also considering the origin, or common ancestor, of both files "C".

git show <branch name>:<file name>>view content of the file

How to resolve conflict 
1.Manually resolve problem.
2.git add .
3.git commit

Standard branch name
git branch feat(lablel)/1(issue id)/<description>(description title)

4.Stashing

 The "git stash" command can help you to (temporarily but safely) store your uncommitted local changes - and leave you with a clean working copy.                 Git Stash - javatpoint
Use case 0:We are working in a branch A,& you have uncommitted changes in your branch A
but now you want to switch the branch D,If you want to switch you must commit them.but you cant
commit them just now.So the solution will be stashing.you temporally save that content & switch the branch D.
After the finished work in branch D you can switch to the  branch A & restore the stash.
When the stashing working directory & indexing stage both also stashed but apply only for working directory only.

stash is worked as a stack(FIFO) & every single stash has an id
Eg:-stash@{id}

git stash push -m "<your stash messege>">add to the stash
git stash pop {<stashid>}>apply+remove from the stash
git stash list>stashed list

git stash apply [<Stash id>]>apply the stash(If do not specify id it applies top stash in the stash stack),do not pop stash stack
git stash branch <branch name>>create a new branch & apply that ,pop stash stack(branch will create from HEAD(master) pointed commit which you doing you stash)

git stash drop[<stash id>]>drop stash

git stash show [<stash id>]>view stash content
git stash show -p> modification in the stashed file
git stash clear>clear the full stash stack
git stash branch <branch name>>create a new branch & apply that 

5.Tagging
Tags are the point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1.0.1).
There is another advantage of using tag is,We do not want to remember the hash codes for that we can use the tag name,Tag includes metadata(Extra data)

Semantic Versioning System

SVS   includes 3 parts
1.Major version
2.Minor version
3.Patch version
If 

We can represent that in X numbers
X.X.X

Major.Minor.Patch
   (*)  . (^)    .  (~)

Technically starting version of software is 0.0.1.But generally used 1.0.0 that means newly released software.In the every product release include a tag,but it is not compulsory.
  • when fix the bug increase the patch number(~)
  • when introduce new feature increase the minor number(^)
  • But if increase the major number(*) mean software do not have the backward compatibility.(May be old features not work)
Eg1:-
v1.0.0
v1.0.1
  .
  .
  .
  .
v1.0.59
v1.0.60
v1.1.0

Eg2:-
~1.2.5(Most of update will come from patch/accepted only patches after 1.2.5)
^1.2.5(accept releases after 1.2.5)
* 6.1 accept any release
But normally used ~ or ^

When we fix a bug or add a new feature,use the new branch
eg;-1.5(bug)->cretae a new branch

              HEAD 
             |
v1.1-v1.2-v1.3-v1.5-v1.6-v1.7
                     |
                   Bug

git tag> list out the tags
git show <tag name>>details about branch,which include specific tag
git tag -d <tag name>>do not use -D(Forcefully delete:only include extra info.)

git checkout <tag name> observe previous tag
git checkout -b <branch name> [tag name]>Create branch + add tag+enter the branch

git diff <tag name>..<tag name>>diff of 2 tags 
 
If  you add tag in SVS it will be a release.but its is not.So that we want to "Draft a new release" that SVS tag.

Types of tags
1.Light-Weight tags:Do not include meta data
git tag <tag name> [Starting point]
[Starting point]={branch(local/remote),commit,tag} & default will be the HEAD

2.Annotated tags:include meta data
git tag -a <tag name> [Starting point] -m "Tag message"
git show <tag name>>use to show full details about annotated tag

6.Ignoring & keeping
Ignored files are usually build artifacts and machine generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:
  • dependency caches, such as the contents of /node_modules or /packages
  • compiled code, such as .o, .pyc, and .class files
  • build output directories, such as /bin, /out, or /target
  • files generated at runtime, such as .log, .lock, or .tmp
  • hidden system files, such as .DS_Store or Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml
To ignore folder or file must include that folder or file name in .gitignore file inside your repository.
(In the windows must save file as ".gitignore" in All types) 

To keep folder you must include that folder or file name in .gitkeep file inside your folder.
We cant add empty folder to git repository.so git do not track empty folder.If you relay add the empty folder, add file called .gitkeep inside that folder.So we indicate folder with forward slash.


eg:- inside .gitignore file
        test2/build.iml>ignore build.iml
        test2/context.xml>ignore context.xml
        test2/*.xml>ignore all .xml files

use case 1:You commit readme.txt to your repo. but now you want to ignore that from your repo.
                  So you add that to your .gitignore file. But still git track that readme.txt file
               
                solution 1:Unstage the readme.txt file & commit
                
git rm --cached -r test>remove folder from repo.(remove test folder from repo.)
git rm --cached test>If there isany file inside the folder

7. Remoting & Clouding



According to the diagram our local part(computer) includes
    1.working directory.
    2.indexing stage.
    3.Local repository.
    4.Remote repository.
They all stored in .git file in our project folder.

There are so many cloud service in the world,Github,Bitbucket,Git Lab also the special cloud service which integrate project with git.It enables Collaboration with project members collective manner.

All the Remote related commands

To list remotes:
git remote>To list remotes

git remote -v>To list fetch/push urls

git remote get-url <remotename> (fetch url)
git remote get-url --push <remotename> (push url)
*pushing mean uploading /thus fetching mean downloading

git remote set-url <remotename> <url> (fetch url)
git remote set-url --push <remotename> <url> (push url)
*To rename the url can use,"update/replace" instead of set 


To add another url for a remote
git remote set-url --add <remotename> <url> (push url)
==
git remote set-url --add [--push] <remotename> <url> (push url)

To delete a url from a remote
git remote set-url --delete <remotename> <url> (push url)
git remote set-url --delete --push <remotename> <url> (push url)
*You cant delete the fetch url only can replace
*To remove push url must2 url there 

To add a new remote:
git remote add <remotename> <url>
origin <- this is our default remote

To push (upload) data:
git push <remote> <branches>
Eg: git push origin master dev

To push forcefully:
git push -f <remote> <branches>

git push <remote <tag name(s)>>push a specific tag
git push <remote name> --tag>push all tags
git push <remote name --delete <tag(s)>>Delete tags in remote/cloud
git push <remote name --delete <branch name(s)>>to delete a remote branch

pushing means uploading
fetching means downloading

To list remote branches:
git branch -r
To list all (remote/local) branches:
git branch --all/git branch -a

To clone the cloud project:
git clone <url> [file name]
*If do not give file name it will take repo name as file name.When cloning by default "origin" will be the name of the remote repo.

To remove a remote:
git remote remove <remotename>

To get information about specific remote:
git remote show <remotename>

git log --oneline <remote>/<branch>>to see all logs on the remote

Others can join with my project via,
1. Forking >  Pull Request
Create copy in the slave repository.To add slave's changes to original repo,slave must request a "pull request".

2. Collaborating (Involve Directly)
Receive an equal permission.

8. Forking Vs Collaboration
Others can join with already created project via,

1. Forking >  Pull Request
In the forking,requester will fork & clone the project with their working area.If forker made change in original project & he want to commit that to original branch he must done pull request to original project owner.
If original project owner accept  the change it will apply to branch.

2. Collaborating (Involve Directly)
If original owner add a collaborator to project,collaborator can clone a original project & then he can commit & push without original owner permission.

git fetch>to sync from cloud .by default it search for remote repo called "origin".

9.Pull

10. Upstream branch(-u)

For track a branch we use -u in git command.
Tracking branch is a local branch which track a remote branch.If we assign local branch as  tracking branch,that local branch track the remote branch;From that we can add short commands to that remote branch(eg:-git push,git pull,git fetch)
Methods of creating tacking branches

git branch --vv>Show local tracking branch & its tracked remote branch

(a)when the clone a branch it will create a track branch automatically.

(b)git push -u <remote> <branch name>
E.g:-git push -u origin feat/com
feat/com in local branch will track feat/com remote branch which pushed.

(c)git branch -u <remote/branch>>Create new branch to track existing branch.
Eg: Now I am in the feat/Add branch
        git branch -u track/add>create branch to track feat/Add.

(d)git checkout -b <Track branch name> [remote/branch]
 E.g: git checkout -b track/add origin/master
            *create branch called  track/add
            *Switched to track/add
            *merge origin/master 
            *Track

From (d) we can short hand the commands....
    git checkout --track remote/branch name
       ^
        |local branch name must equal to remote branch name,for use this 
        |
    git checkout branch name

Fetching Details
git log> all commit details.,(Head->master;which means latest commit)
               B-backward navigation    
               F-forward navigation
               Q-Quit 
 
(**In the applicable stage we want to narrow-down the log list)


git log --oneline> Narrow down one log detail to one line.

git log -5>latest 5 commit details

time
git log --oneline --since="5 minutes ago"

git log --oneline --since="2 day ago"

git log --oneline --since="2 year before"

git log --oneline --since="2 year after"

git log --before="1 day"

git log --after="5 seconds"

git log --oneline --since="April 1 " --until="yesterday"

author
git log --author="">view all users commits

git log --oneline --author="ch">view all users commits name include with "ch"

git log --grep "some">search commit message include some

git log --name-only>Search changed, files names(Not renamed but all changes)

specify a file
git show>show all details about HEAD->master

git show 78698dc30647019cac510a9b2a0e6db34377e9d8>get all details specify file using hash code 

git show --name-only>HEAD->master file name details.no details

git show --name-status/git log --name-status>All changes in HEAD->master commit file(A-Add,M-Modified,D-Delete,R-Rename)

git log --oneline --name-status -1> status of last commit(AMDR)  

git log --before="10minutes ago" readme.txt> what happen readme file 10 minutes before

git log --oneline --name-status --diff-filter=A/M/D/R 

git log --oneline --name-status --diff-filter=A readme.txt>adding commits of  readme.txt file.

git reflog >HEAD pointed status history.



file name(path)
Must be added at the end of the command.(-- file name must be with)

git log AppInizializer.java

git log AppInizializer.java readme.txt

revision range

Use to get details about 2 commits.
    1.double dot R.R.(<first commit>..<Second commit>)

Eg-      
              "A..F" means All commit that F commit can reach but "C" can't
               F can reach ABEF
               A can't reach BEFCD 
               R.R. BEF

               B..D = {C,D}
               F..D={C,D}


git log fcef022..86bacb7 ex2.txt>specified file commit between R.R.

    2.trple dot R.R.(<first commit>...<Second commit>)
This basically asks Git to resolve a range of commits that specifies all the commits that are reachable by either of two references but not by both of them.
    
E.g-
        F can reach ABEF
        D can reach ABCD
        FD intersect AB
        (FD intersect)`= F...D = {E, C, F, D}

        B...D = {C,D}
        A...F = {B, E, F}


differences
Track all the details differences (Compare) between 3 tree stages.
git diff
git diff>deference between work stage & Indexing stage

git diff --cached/git diff --cached HEAD>deference between Indexing & repository.

git diff HEAD>Deference between working stage & Repository.

git diff e184af4..12337c5/git diff e184af4 12337c5>deference between 2 revision range.

git diff readme.txt>compare specified file in working stage & indexing stage.

git diff  HEAD readme.txt licence.txt>compare specified file in working stage & Repository.
 
git diff --cached readme.txt.

git diff HEAD ~1>comparison master branch with with 1 before that.

git diff HEAD ~2>comparison master branch with with 2 before that.

git diff HEAD..HEAD~1 readme.txt/git diff HEAD HEAD~1 readme.txt>

git diff --no-index  <file 1> <file 2>>Compare 2 files inside the working dir.


Cryptography Behind Git

There are 3 types of Cryptographic methods.
    1. Symmetric function.
    2.asymmetric function.
    3.Hash function.

Decoded: Examples of How Hashing Algorithms Work

Hashing Techniques use for
    1.Password Encrypt & Validate.
    2.Torrent.Validation.
    3.Software validation(os).
    4.Version Controlling Software.

Eg:-MD5,SHA1-512

Git hub also used hashing techniques,for find the deference between commits in version controlling manner by compare defernent unique hashes relevant to each commit.Git use the SHA256.

3-Tree Structure

                                  Git: Unstaging all changes and reset the working tree
Git track project file using 3 perspective,
        1.Working Directory
        2.Indexing Stage:working directory is a not safe place,without commit you can save it temporary                safe place & the commit all added file together. 
        3.Repository 
Running git status> command can find the current status of working tree  

Git Commit Message Guidelines
First line in message will be the Summery (display when type git log --online) & up to 1st line will be description.  Follow this!
    1.Start with commanding verb.
    2.Present tense.
    3.No Singular
  







Comments

Popular posts from this blog

Hibernate-1

Map Interface using Java