In the software development process, Git and GitHub have become industry standards. Here's an overview of how to use both to write code and develop applications.
Basics:
What Exactly Is Git?
Git is a distributed version management system that is free and open source. What is version control and how does it work? It's essentially a mechanism that allows you to track changes to files over time so that you can go back and view specific versions of them later.
Why Should You Use Git?
Git has become a development industry standard over time. Being able to take a snapshot of your code at a certain point in time is extremely useful when your codebase expands and you need to refer to older versions.
Getting Started:
Sign up for a GitHub account at github.com to get started with Git and GitHub.
Then go to desktop.github.com and download GitHub's desktop GUI. This is a GUI version of Git's command-line interface. The Git Shell should be downloaded as well.
Last but not least, you must provide your GitHub credentials. This can be done using the Git Shell or GitHub desktop application.
How it Works?
With Git, you use a command-line tool called the "Git Shell" to track local changes to your code (you can use Git in other command-line tools, but I'll refer to Git Shell in the following sections). Instead of using a graphical user interface, command-line allows you to type instructions to view, change, and manage files and folders via a basic terminal (GUI). Don't worry if you've never used command-line before; once you get the hang of it, it's really simple.
In essence, when you use Git, you make changes to your code files in the same way that you would during the development process. When you've finished a coding milestone or wish to take a snapshot of certain changes, you put them in a staging area and then use Git to commit them to your project's version history (repository). The Git commands you'll need for those tasks are shown below.
Basics
Commands:
While using Git on the command line, you'll almost certainly need some simple terminal commands to navigate your project and system files / folders, such as:
pwd - displays your current location in the file system.
ls - is a command that displays a list of files in the current directory (folder)
cd [directory-name] - navigates to the specified directory.
mkdir [directory-name] is a command that creates a new directory with the specified name.
Creating Repositories:
The first command you must run when you want to use Git for a project is git init, followed by the name of your project:
git init [project-name]
You run this command on the Git Shell command-line in your project's primary directory (folder), which you can get to with the commands given above. When you perform this command, Git creates a hidden.git file in your project's main directory. This file keeps track of your project's version history and converts it into a Git repository so you can run Git commands on it.
Making Changes:
git add [file] or git add *
You must add them to the staging area with git add, either by file name or by including all of the files in your current folder using git add * when you make changes to your files and select to snapshot them to your project's version history.
git commit -m “[message]”
To finally commit the modifications you made to your staging area files to your repository's version history, run git commit with a detailed description of the modifications you made.
Working:
With the fundamental Git commands in place, you may use Git to take a snapshot of your project's version history. Simply execute git init in your project's main directory to create a new repository. You add your changes to the staging area with git add ** or git add* with particular file names. Finally, you can commit your modifications to the repository's version history using git commit.
Github:
In essence, GitHub is a service that allows you to host and collaborate on your Git repositories online. GitHub is accessible via their online portal, the GitHub desktop GUI, and the Git Shell.
GitHub is now utilised by 65million+ developers and businesses as a service, and it has become a widely accepted standard for project collaboration and code open-sourcing.
How It Works:
With GitHub, you have the same local process of adding and committing files to an initialized Git repository on your computer. However, you can utilize GitHub to push your changes to GitHub’s hosting service. This allows other people to similarly work on the same project, pull your changes to their computers, and push their own changes to GitHub. Continue below to see the commands you can use to utilize Git with GitHub.
Repositories:
fork
With Git on your local computer, if you want to create a new repository, you must run git init. However, many times you may work on projects that are hosted on GitHub and have already been initialized.
When you fork a repository, you're effectively copying it to your GitHub account. You must, however, clone the project to work on it on your local computer.
git clone[url]
Cloning a project from GitHub easily copies a Git repository and its version history to your local computer via its url. You can then make and commit your own changes to that repository. Any modifications you make to a project and then push to GitHub (see below) are stored for your copy.
Branches
You may look at the version history of your project's development when you use Git. However, there may be occasions when you want to build features, repair bugs, or experiment in methods that require separating your core project's code from a variant.
You may achieve this using branches, which are effectively parallel versions of your repository's primary code—the "master" branch is where that code is produced. You can split your code into numerous branches for collaboration and other unique development. Changes you make to files in one branch are only saved in that branch's version history.
If you want to merge code from one branch into another, including the master branch, you can do so by submitting a pull request. If different modifications have been made to the same files on more than one branch, you will run into merge conflicts that must be resolved manually, just like when using git pull and git push.
All Coming together (working)
With GitHub, you are enabled to collaboratively use Git to build your own repositories and those of others. You can fork an initialized repository to your own account and clone any repository from GitHub to your computer. As you continue to add and commit changes you make to files on your local computer with Git, you can now additionally push those changes and pull any other commits to and from GitHub.