Version Control System

 Version Control System

Three measure problems which is solved by Version Control System.
  • Merging work on code done by multiple developers in parallel.
  • Keep track of why a change was done and who did it.
  • Revert to a previous state of code. If any new feature or promotion of code break the application.

Types of VCS

  • Centralized
    • Something that is common and connect everyone.
    • A central server where complete code is present.
    • Example: Google docs
    • Issues or disadvantages
      • Internet is needed. Only online mode is supported.
      • If server is down then entire process stop. Single point of failure.
  • Distributed
    • Same code is present in multiple places.
    • Here server and client architecture is adopted and works.
    • It solve centralized version control system issue.

How to work with distributed version control system?

  1. Connect to server
  2. Download the code from server to laptop
  3. Perform your work on code
  4. Push the code to server so that other developers can see the changes and new version of code.
In distributed version control system they have multiple servers to take care of single point of failure.
No internet is required all the time.
Disadvantage is every developer need to download big projects and huge amount of code.
Git is most widely used product and created by the developer of Linux and his name is Linus Tervalds.

How Git works?

  • commit - change which will be put in git after doing the changes to a code.
  • git repository - changes in the code which is managed by git and even history is maintained.
  • git is using linked list process and datastructure as everytime any new commit is made new node is added to the existing git repo.

Some commands:

  • git init - it will help to initialize the git repo any empty folder present in your OS will become git repo if you have installed the git bash software in your system.
  • git status - It will help you to know what all files are present in the area of git repo.
  • git add - it will help you to add all the files which you need to include to your git repo, you can use . to select all the files and if you want only the specific ones then give there names.
  • git commit -m - It will help you to make the changes commited to your git repo with meaningful message to the code which are promoting to git.
  • git log - it will show all the commit history and that will show you the linked list timeline of your activity or your team acitivity done on your code.

Properties of git commit:

  • It is immutable. Once a change is made then it will be part of commit.
  • Never commit any secret or sensitive in git as it will never get delete and then your code will become vernable.
  • git allows you to go back in history and allow you to see the code version at that time and even the changes are made at that time.
  • so git can commit the changes in two ways one is to have only the difference from old code and new code and even can have complete information as new file.
    • Save all the code at the time of commit.
      • Advantage: It will make visibility of code easy
      • Disadvantage: Storage and speed of data transfer.
    • Store just the change at each commit
      • Advantage: Space is saved.
      • Disadvantage: Cannot see the entire code easily need to build the code.

Git Branch concept:

If two developers need to work on same application and need to build two different features in parallel.
In this case developer need to create a branch from base code.

branch means a developer can make changes to a code in isolated environment.
In this case one branch developer will not be aware of any new changes made to anyother branch as it will have the visibility of only the main branch from which this new branch is created.


git branch <name> will create new branch from the branch where you are present at the moment.
git branch will show you the list of all the branches present in a git repo.
git checkout <branch_name> will help to move to your desired branch to make changes to your code.

Now to reflect new changes to this new branch in repo over remote side you need to push the changes.
Steps: 
  1. git status
  2. git add .
  3. git commit -m "Logical message which will help fellow developer to understand the changes made by you"
  4. git push to push new changes to remote.

Git Merge concept:

This concept help developer to push the code to main branch from a developer has created this branch and made changes and checked it and tested it.
  1. git checkout <main> <provide branch name from where you have created your branch>
  2. git merge <calculator> <provide your branch name where you have made the changes and tested it>

At the time of merging the code merge conflict can take place and in this case only developer can help to solve the issue.
Here git will raise the conflict and you need to fix the conflicts and then only you can short this out and can merge your code to main branch.

Steps to resolve it:
  1. Open file in editer and remove the conflicts
  2. git add . - to add new changes to same existing file after resolving conflicts from that file. 
  3. git commit -m "removed conficts from the file"

Fast forward merge:

Scenario now think that there is a developer who is working on any git repo, where main branch is used to create new feature branch, while he make changes to new feature branch content main branch remains same and he has made multiple commits in this new branch and now when his work is done he want to merge this new feature branch into main branch, in this case main branch pointer will shift new feature branchs latest commit, this process of shifting the main branch pointer to new feature latest commit is known as fast forward merge. It is the most common and most ideal condition which all developer love.




Different ways to manage commit history:

Fast Forward Merge and Three Way Merge which one is better?
Best is FFM.

Issues with TWM?
  1. Conflicts (will happen when merging)
  2. Jumbled up commit history. - reverting back to working code - As will raise the case when to resolve the issue caused by your code you tried to revert back to the running state but in this process you have reverted back other people work as well, as history of git is jumbled and states of your code and other developer codes are entangled with each other, in disproportionate manner and hard to identify and making conflict to revert back the changes.

How to convert TWM to FFM?
  1. More fast forward merge
  2. Clean history - As it will help to revert back to working code mode.
  3. To solve this jumbled history, we need to use rebase command.
  4. Rebase will help to update the main branch from where you have took your feature branch.




Now conflict will occur at the time of rebase. In this case you just need to contact the developer and then need to resolve the conflict at the time of rebase and you solved the issue.

So inshort rebase help to get all the commits from the main branch to your feature branch before going for git merge.

git remote

Backup or mirror of your local git repository. This is the server version of my local git repo which other users/developers can access.

push is the command to send your local commits to central server or remote git repo.

How to create a remote repo?
  1. Link a local repository to a remote repo
    1. git remote add origin <name of the remote> url
    2. git remote -v
    3. git push origin <branch name>
    4. git push -u origin HEAD:<remote-branch> - to push changes to right git remote branch.
    5. git fetch origin - It will fetch all the changes to your local repository. A read only copy from remote and maitained in your system, will wait for your response.
    6. git pull - It will run the git fetch and will do merge as well. This will make the changes to your local repo which is not good. It is not recommended. So in place of it do git fetch and then rebase and then go for merge and then re-push the updated repo to remote.
    7. Whenever you fetch any information from git repo from remote to rebase it before merging it to your local repo. Helps to avoid three way merge issue.
    8. git pull --rebase - it will run
      1. git fetch
      2. git rebase
      3. git merge

Forking Repository:

This is process of copying one repository to another repository, it is used most of the time at the time of OpenSource contribution to any project as a developer.

git remote add upstream <url of the original repo>

git fetch ups
git merge ups/main

git checkout -b <name> - it will create new branch and checkout into it.
git push origin <branch name>

Here whatever branch you have checkout in your local git repo same will be updated over remote by using push command.

git pull command is to push the code from one branch to another which will be the base branch for your branch which is having latest code which need to be reflected to the base branch.

Cherry pick:

Allows you to select and apply individual commits from 1 branch to another.
Steps to do it:
  1. Identify commit hash
  2. Go to target branch
  3. git cherry-pick <commit hash>
  4. resolve the conflicts
  5. git add
  6. git cherry-pick -- continue
  7. push your changes.
solve all the levels of https://learngitbranching.js.org/



Comments

Popular posts from this blog

Introduction to Backend Development