Basics of Git

Git:

  • Git is a distributed version control tool ,used for maintaining historical and current versions of source code, enabling multiple developers to work together.

  • It is free and open source.

  • Developers can work collaboratively ,see full history and revert to earlier versions of projects from anywhere in the world.

  • It is scalable and creates backups.

  • It is system-compatible i.e. can be used in Windows , Mac and Linux.

  • It is easy to learn and use for any purpose.

What does Git do?

  • Manage projects with Repositories

  • Clone a project to work on a local copy

  • Control and track changes with Staging and Committing

  • Branch and Merge to allow for work on different parts and versions of a project

  • Pull the latest version of the project to a local copy

  • Push local updates to the main project

  • storing whole history of project requires large space for which git uses object packing

Fundamental Concepts in Git:

  • Commits: Snapshots of code changes.

  • Repositories: Collection of code and history.

  • Working Directory: Where developers make changes.

  • Staging Area: Prepares changes for commits.

  • Push and Pull: Sending and fetching changes.

  • Pull Request: A request to merge changes from one branch into another

  • Merge and Conflict Resolution: Combining changes and handling conflicts.

  • Branches: Separate development paths for features without affecting the main project.

  • Forks: Personal copies for independent development.

How Git works

Here is a basic overview of how Git works:

local file >>>staging area>>>repository

  1. Create a "repository" (project) with a git hosting tool. Git now creates a hidden folder to keep track of changes in that folder.

  2. When a file is changed, added or deleted, it is considered modified

  3. You select the modified files you want to Stage

  4. The Staged files are Committed, which prompts Git to store a permanent snapshot of the files

  5. Copy (or clone) the repository to your local machine

  6. Add a file to your local repo and "commit" (save) the changes

  7. "Push" your changes to your main branch

  8. Make a change to your file with a git hosting tool and commit

  9. "Pull" the changes to your local machine

  10. Create a "branch" (version), make a change, commit the change

  11. Open a "pull request" (propose changes to the main branch)

  12. "Merge" your branch to the main branch

Use cases of git are :

  • Keeping history which helps to inspect and revert to a previous revision if required.

  • All the deleted content remains accessible in the history.

  • Working in team is easier as sharing files and merging changes done by other users is possible,also it ensures that nothing is accidentally overwritten.

  • Helps to handle multiple branch concurrently:

    • main branch :where everyday development happens

    • maintenance branch: to issue bug fix for older release of software

    • feature branch : for a new feature requiring intrusive change in code.

    • merging:when new feature is ready,it can merge back into master branch.

    • release branch: to prepare the next release,code is frozen and only bug fixes are accepted

    • when code is released ,release branch becomes maintenance branch and bug fixes can be added to main branch

Basic git commands

  • git init demo : to initialize empty demo/.git repository

  • git add: to take file to staging area

  • git commit -m"message": to commit file with meaningful message.

  • git rm: remove the file from the working copy

  • git diff:shows the differences between two revisions

  • git reset : cancels the changes

  • git log : shows history of commits

  • git mv: move/rename a file

  • git tag :creating/deleting tag

  • git checkout: switching between branch

  • git merge demo:merge the changes in demo into the current branch

  • git status:provides the details of current working details.

  • git aliases :to create shorter, easier-to- remember names of the commands

To check user configuration:

git config --global user.name "Name surname"

git config --global email.name "xyz@gmail.com"

To check user config list : git config --list