Introduction
many colleges in India still don't teach students about real-world solutions instead they stick to the old school books this has been one of the main reasons why many people in tech don't know about Git & GitHub whether you are a developer/tester/QA Engineer / SDET/DevOps Engineer/ SRE/database administrator/system administrator or from some other profile in IT you need to have a slight bit knowledge of version control system used by your company since Git is a popular VSC let's start by understanding it
Understanding Git
"Git is free and open source software for distributed version control: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. "
This is the Wikipedia definition let's break this down in a simple language, speaking in the professional sense Git is a version control system. You can use version control for versioning code, binary files, digital assets, etc Version control systems allow multiple developers, designers, and team members to work together on the same project. It helps them work smarter and faster! A version control system is critical to ensure everyone can access the latest code and that modifications are tracked. As development becomes increasingly complex and teams grow, there's a bigger need to manage multiple versions and components of entire products.
Umm… Okay… But Why Tho?
A Version Control System (VCS) allows you to revert files back to a previous state, revert the entire project back to a previous state, review changes made over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also means that if you screw things up or lose files, you can generally recover easily. And sometimes you just want to know “who wrote this crap”, and having access to that information is worthwhile.
Git version control is open source. It's a distributed free version control software. In fact, Git version control is one of the most popular options. It's open source, so anyone can use it.
Understanding GitHub
GitHub is a code sharing and publishing service, or we could say it’s a social networking site for programmers 😁
GitHub is a Git repository hosting service, but it adds many of its own features. While Git is a command line tool, GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, such as wikis and basic task management tools for every project.
The flagship functionality of GitHub is “forking” – copying a repository from one user’s account to another. This enables you to take a project that you don’t have write access to and modify it under your own account. If you make changes you’d like to share, you can send a notification called a “pull request” to the original owner. That user can then, with a click of a button, merge the changes found in your repo with the original repo.
These three features – fork, pull request, and merge – are what make GitHub so powerful.
Let's understand the technical terms
Before we start working with Git commands, it is necessary that you understand what it represents,and how it works.
Repository A repository a.k.a. repo is nothing but a collection of source code.
Git Workflow Diagram of a simple Git Workflow To learn the fundamentals of Git workflows, there are four main stores to understand (you may hear of a fifth — “stash” — but that’s beyond the scope of this basic introduction).
- Workspace
- Staging Area
- Local Repository
- Remote Repository (optional)
If you consider a file in your Working Directory, it can be in three possible states.
It can be staged. This means the files with the updated changes are marked to be committed to the local repository but not yet committed. It can be modified. This means the files with the updated changes are not yet stored in the local repository. It can be committed. This means that the changes you made to your file are safely stored in the local repository.
Branch A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process discussed in Git Basics, the first module of this series. You can think of them as a way to request a brand new working directory, staging area, and project history. New commits are recorded in the history of the current branch, which results in a fork in the history of the project.
Main The default development branch. Whenever you create a git repository, a branch named "main" is created, and becomes the active branch.
Pull Request Pull requests are a feature that makes it easier for developers to collaborate using Bitbucket. They provide a user-friendly web interface for discussing proposed changes before integrating them into the official project.
Tag A reference is typically used to mark a particular point in the commit chain. In contrast to a head, a tag is not updated by the commit command.
Working Tree The tree of actual checked-out files, normally containing the contents of the HEAD commit's tree and any local changes you've made but haven't yet committed.
HEAD Git’s way of referring to the current snapshot. Internally, the git checkout command simply updates the HEAD to point to either the specified branch or commit. When it points to a branch, Git doesn't complain, but when you check out a commit, it switches into a “detached HEAD” state.
Hook A script that runs automatically every time a particular event occurs in a Git repository. Hooks let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle.
Let's get to Git Commands
git add
Moves changes from the working directory to the staging area. This gives you the opportunity to prepare a snapshot before committing it to the official history.
git branch
This command is your general-purpose branch administration tool. It lets you create isolated development environments within a single repository.
git checkout
In addition to checking out old commits and old file revisions, git checkout is also the means to navigate existing branches. Combined with the basic Git commands, it’s a way to work on a particular line of development.
git clean
Removes untracked files from the working directory. This is the logical counterpart to git reset, which (typically) only operates on tracked files.
git clone
Creates a copy of an existing Git repository. Cloning is the most common way for developers to obtain a working copy of a central repository.
git commit
Takes the staged snapshot and commits it to the project history. Combined with git add, this defines the basic workflow for all Git users.
git config
A convenient way to set configuration options for your Git installation. You’ll typically only need to use this immediately after installing Git on a new development machine.
git fetch
Fetching downloads a branch from another repository, along with all of its associated commits and files. But, it doesn't try to integrate anything into your local repository. This gives you a chance to inspect changes before merging them with your project.
git init
Initializes a new Git repository. If you want to place a project under revision control, this is the first command you need to learn.
git log
Lets you explore the previous revisions of a project. It provides several formatting options for displaying committed snapshots.
git merge
A powerful way to integrate changes from divergent branches. After forking the project history with the git branch, git merge lets you put it back together again.
git pull
Pulling is the automated version of git fetch. It downloads a branch from a remote repository, then immediately merges it into the current branch. This is the Git equivalent of an svn update.
git push
Pushing is the opposite of fetching (with a few caveats). It lets you move a local branch to another repository, which serves as a convenient way to publish contributions. This is like an svn commit, but it sends a series of commits instead of a single changeset.
git remote
A convenient tool for administering remote connections. Instead of passing the full URL to the fetch, pull, and push commands, it lets you use a more meaningful shortcut.
git reset
Undoes changes to files in the working directory. Resetting lets you clean up or completely remove changes that have not been pushed to a public repository.
git revert
Undoes a committed snapshot. When you discover a faulty commit, reverting is a safe and easy way to completely remove it from the code base.
git status
Displays the state of the working directory and the staged snapshot. You’ll want to run this in conjunction with git add and git commit to see exactly what’s being included in the next snapshot.
git stash
Temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.
Outro💙
now reading this blog alone will not be sufficient you have to practice using git in your personal projects and next time you have to share your code with a friend just use GitHub instead of google drive or zip files if you think you need an interactive way to learn git & GitHub instead of these tutorials and blogs check out this GitHub Skills