Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is commonly used for open source and commercial software development.
GitHub is a web-based service for version control using Git. Basically, it is a social code-hosting platform for developers. You can look at other people’s code, identify issues with their code and even propose changes. This also helps you in improving your code.
In short, Git is Version Control System and GitHub is a hosting service for Git Repositories.
Installing Git and GitHub: –
The first two things we need to do are install git and create a free GitHub account.
Git Installation on Windows
Download and install ‘Git for Windows’ from Git downloads Page. Once installed, you’ll be able to use Git from the command prompt or Git Bash.
Git for Windows does not automatically update. Update it by downloading the newer version of it.
Git Installation on macOS
Install ‘Homebrew’ and run the following command to install an up to date version of Git on your Mac:
> brew install git
Git Installation on Linux
Use your Linux package management system to install Git.
> sudo apt-get install git
> sudo yum install git
> sudo apt install git
Once you’ve done that, create a GitHub account by going to the github.com site.
Once we install Git, the first thing we should do is setting up your user details only one time. Now go to your terminal for setting your username and type
> git config --global user.name "your_name_here"
Replacing “your name here” with your own name in quotations. You can use any name you want. If you want to set your name for just one repository, leave out the word “global.”
Now you can tell Git your email, and make sure it’s the same email you used when you signed up for GitHub
git config --global user.email "your_email_here"
Checking for settings.
> git config --list
You can also check what git thinks a specific key-value is by typing ‘git config <key>’
> git config user.name
Some Basic Terminology: –
Repository: A Git Repository, or a repo, is a folder that you’ve told Git to help you track file changes.
Branch: A branch is an independent line of development. You can think of it as a brand-new working directory.
Fork: A fork is a personal copy of another user’s repository that lives on your account.
Clone: A clone is simply a copy of a repository that lives on your computer instead of on a server.
Commit: A commit is a set of one or more changes to a file (or a set of files). Every time you save, it creates a unique ID(“hash”) which helps it keep track of the history.
Master: The default development branch. Whenever you create a ‘git repo’, a branch named “master” is created which becomes the default active branch.
Git Repository Structure: –
It consists of 4 parts:
- Working Area: This is your local directory where you make the project (write code) and make changes to it.
- Staging Area: This is an area where you first need to put your project before committing. This is used for code review by other team members.
- Local Repository: This is your local repository where you commit changes to the project before pushing them to central repository on GitHub. This is what is provided by distributed version control system. This corresponds to the ‘.git’ folder in our directory.
- Central Repository: This is the main project on the central server, a copy of which is with every team member as local repository.
All the repository structure is internal to Git and are transparent to developer. The following diagram demonstrate the above areas.
Basic Git Commands: –
With ‘git init’ command, create a new repo from an existing folder on your computer.
> git init
With ‘git add .’ command, move changes from the working directory to the staging area.
> git add
With ‘git commit’ command, Takes the staged files and commits it to the project history. Together with git add, this saves your changes in the main repo.
> git commit -m "commit message"
With ‘git clone’ command, copy the contents of an existing repo to a folder on your computer.
> git clone (URL of the repo)
With ‘git branch’ command, create branches in git.
> git branch "branchname"
With ‘git push’ command, push your changes to server means save your changes on the remote server.
> git push origin "branchname"
With ‘git pull’ command, we can pull the data from remote repo to local repo that means to keep the local repository in sync with the remote repository
> git pull origin master
With ‘git status’ command, we will know the working repository status i.e. working tree is clean or not.
> git status
With ‘git remote’ command, we can add / remove the remote repository URL to the local repository where it pushes / pulls the code.
> git remote add origin “URL” > git remote remove origin
With ‘git log’ command, we can see all the commits ids.
> git log
With ‘git tag’ command, we can create new tags and display all those tags too.
> git tag "tagname" > git tag
There are a lot more commands to learn but these are enough to get you started. You can pick up the rest of the commands as you explore and use git and GitHub more.