Basic Git & GitHub for DevOps Engineers.

#90DaysofDeVops

What is Git?

Git is a version control system that allows you to track changes to files and coordinate work on those files among multiple people. It is commonly used for software development, but it can be used to track changes to any set of files.

With Git, you can keep a record of who made changes to what part of a file, and you can revert back to earlier versions of the file if needed. Git also makes it easy to collaborate with others, as you can share changes and merge the changes made by different people into a single version of a file.

What is Github?

GitHub is a web-based platform that provides hosting for version control using Git. It is a subsidiary of Microsoft, and it offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features. GitHub is a very popular platform for developers to share and collaborate on projects, and it is also used for hosting open-source projects.

What is Version Control? How many types of version controls do we have?

Version control is a system that tracks changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more.

There are two main types of version control systems: centralized version control systems and distributed version control systems.

  1. A centralized version control system (CVCS) uses a central server to store all the versions of a project's files. Developers "check out" files from the central server, make changes, and then "check-in" the updated files. Examples of CVCS include Subversion and Perforce.

  2. A distributed version control system (DVCS) allows developers to "clone" an entire repository, including the entire version history of the project. This means that they have a complete local copy of the repository, including all branches and past versions. Developers can work independently and then later merge their changes back into the main repository. Examples of DVCS include Git, Mercurial, and Darcs.

Why do we use distributed version control over centralized version control?

  1. Better collaboration: In a DVCS, every developer has a full copy of the repository, including the entire history of all changes. This makes it easier for developers to work together, as they don't have to constantly communicate with a central server to commit their changes or to see the changes made by others.

  2. Improved speed: Because developers have a local copy of the repository, they can commit their changes and perform other version control actions faster, as they don't have to communicate with a central server.

  3. Greater flexibility: With a DVCS, developers can work offline and commit their changes later when they do have an internet connection. They can also choose to share their changes with only a subset of the team, rather than pushing all of their changes to a central server.

  4. Enhanced security: In a DVCS, the repository history is stored on multiple servers and computers, which makes it more resistant to data loss. If the central server in a CVCS goes down or the repository becomes corrupted, it can be difficult to recover the lost data.

Overall, the decentralized nature of a DVCS allows for greater collaboration, flexibility, and security, making it a popular choice for many teams.

GitHub Basic Commands:
GitHub_Fundamentals-------------------------------------
git init - Initialising the repository
git clone " SSH_URL" - Here i created a ssh key and used it for authentication
git remote add origin "SSH URL" 
git status - to check the status if any file is in which branch and what all needs to be added.
git add . - Adds all files in the current directory
git add <file_name> - adds a specific file to the repository
git commit -m "Message" - Committing the local changes
git push origin <branch_name> - For pushing the local changes to the remote repository
git checkout -b <new_branch_name> - For creating a new branch
git branch - For listing down the branches
git checkout <branch_name> - For switching between branches
git log - shows the commit id, date/time , email id and all the details of the commit.
git log --oneline - Shows the logs in one line which is easier to understand.
git diff - Shows the difference in the files with and without commit.
git branch - Displays the branches which are available also the current branch which we are working.
git revert <Commit_ID>-  Undoes all the changes which are commited til the commit ID.(Commit id can be checked through git log).
git reset <Commit_ID>/HEAD - Undoes all the changes which are uncommited.
git cherry-pick <Commit_ID> - Adding HEAD to a specific commit.
git pull origin <branchname> --rebase - Cleaner project history - oneliner commits
git merge <branchname> - Merging two branches with all the commits.
git stash - temporarily stashes(shelves) the changes you have made to the working directory.
git stash pop - removes the changes from stash and reapply them in the working directory.
git stash reapply - It reapplies the changes changes without removing from the working directory.
git stash -u - Stashing untracked( if we do not use git add then it is untracked it is not in the staging enviornment) files.
git stash show - Shows the list of stashes.
git merge --squash feature-branch - Combining multiple commits to a single commit